VBA - 如果 A 列中的单元格不是空白,则 B 列等于

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19368277/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:54:11  来源:igfitidea点击:

VBA - If a cell in column A is not blank the column B equals

excelvbaexcel-vba

提问by Boba Fett

I'm looking for some code that will look at Column A and as long as the cell in Column A is not blank, then the corresponding cell in Column B will equal a specific value.

我正在寻找一些代码来查看 A 列,只要 A 列中的单元格不为空,那么 B 列中的相应单元格将等于特定值。

So if Cell A1 <> "" then Cell B1.Value = "MyText" And repeat until a cell in Column A is blank or empty.

所以如果 Cell A1 <> "" 那么 Cell B1.Value = "MyText" 并重复直到 A 列中的单元格为空白或空。

To add a little more clarification, I have looked through the various loop questions asked and answered here. They were somewhat helpful. However, I'm unclear on how to get the loop to go through Column A to verify that each cell in Column A isn't blank AND in the corresponding cell in Column B, add some text that I specify.

为了进一步澄清,我浏览了此处提出和回答的各种循环问题。他们有点帮助。但是,我不清楚如何让循环遍历 A 列以验证 A 列中的每个单元格都不是空白,并且在 B 列中的相应单元格中添加一些我指定的文本。

Also, this will need to be part of a VBA macro and not part of a cell formula such as =IF

此外,这需要是 VBA 宏的一部分,而不是单元格公式的一部分,例如 =IF

回答by Portland Runner

If you really want a vba solution you can loop through a range like this:

如果你真的想要一个 vba 解决方案,你可以循环遍历这样的范围:

Sub Check()
    Dim dat As Variant
    Dim rng As Range
    Dim i As Long

    Set rng = Range("A1:A100")
    dat = rng
    For i = LBound(dat, 1) To UBound(dat, 1)
        If dat(i, 1) <> "" Then
            rng(i, 2).Value = "My Text"
        End If
    Next
End Sub

*EDIT*

*编辑*

Instead of using varients you can just loop through the range like this:

您可以像这样循环遍历范围,而不是使用变体:

Sub Check()
    Dim rng As Range
    Dim i As Long

    'Set the range in column A you want to loop through
    Set rng = Range("A1:A100")
    For Each cell In rng
        'test if cell is empty
        If cell.Value <> "" Then
            'write to adjacent cell
            cell.Offset(0, 1).Value = "My Text"
        End If
    Next
End Sub

回答by Siddharth Rout

Another way (Using Formulas in VBA). I guess this is the shortest VBA code as well?

另一种方式(在 VBA 中使用公式)。我想这也是最短的 VBA 代码?

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        .Range("B1:B" & lRow).Formula = "=If(A1<>"""",""My Text"","""")"
        .Range("B1:B" & lRow).Value = .Range("B1:B" & lRow).Value
    End With
End Sub

回答by excelVBAmaster.com

A simpler way to do this would be:

一个更简单的方法是:

Sub populateB()

For Each Cel in Range("A1:A100")
    If Cel.value <> "" Then Cel.Offset(0, 1).value = "Your Text"
Next

End Sub

回答by Jonysuise

Use the function IF :

使用函数 IF :

=IF ( logical_test, value_if_true, value_if_false )

=IF ( logical_test, value_if_true, value_if_false )