简体   繁体   English

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

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

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. 因此,如果单元格A1 <>“”,则单元格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这样的单元格公式的一部分

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

Another way (Using Formulas in VBA). 另一种方法(在VBA中使用公式)。 I guess this is the shortest VBA code as well? 我想这也是最短的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

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

使用功能IF:

=IF ( logical_test, value_if_true, value_if_false )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果A列中的单元格不是空白,则B列等于C列等于 - If a cell in column A is not blank then column B equals and column C equals Excel VBA-如果B列中的单元格包含一个值,则A列等于“值”,但不要覆盖现有的A列数据 - Excel VBA - If a cell in column B contains a value then column A equals “value” BUT do not overwrite existing column A data 使用Excel vba查找并选择B列中的第一个空白单元格 - Find and select first blank cell in column B with Excel vba 如果 A 列中的单元格等于 B 列中的单元格,则 Excel 宏将删除一行 - Excel Macro to delete a row if cell in Column A equals cell in Column B Select 单元格 C,其中 A 列中的值等于 a,B 列中的值等于 b - Select cell C where value in column A equals a and value in column B equals b 在VBA的列中选择可变的空白单元格范围 - Select variable blank cell range in column in VBA 如果A列中的单元格为空,则针对不同工作表中的数据集进行vlookup列B - if cell in column A is blank then vlookup column B against dataset in different sheet Excel公式匹配A列中的值,并且不返回空白列B - Excel formula match value in column A and return not blank cell column B 如果 A 列等于某个值,则在 B 列中生成一个值。检查 A 列中的每个单元格 --&gt; 在 B 列中的每个单元格中生成一个值 - If column A equals a certain value, generate a value in column B. Check each cell in column A --> Generate a value in each cell in column B Excel 如果 A 列中的单元格和 B 列中的单元格为空白而不是 G 列中的突出显示单元格,则条件格式 - Excel Conditional Formatting if cell in Column A AND the cell in Column B are blank than highlight cell in Column G
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM