如果为空

时间:2015-11-05 13:10:02

标签: excel vba if-statement

我正在尝试制作一个用特定文字填充A列的程序。该文本将取决于B43以后的单元格是否填充了我希望在A列中显示的文本。

这就是我的想法:

Dim freecell As Range
Dim inforcells As Range

Set infocells = Range("A61:A90")

If Range("B43") <> "" Then
        For Each freecell In infocells.Cells
            If freecell = "" Then
               freecell.Value = "some text"
            End If
        Exit For
        Next
End If

If Range("B45") <> "" Then
        For Each freecell In infocells.Cells
            If freecell = "" Then
               freecell.Value = "some text"
            End If
        Exit For
        Next
End If

如何格式化if语句之外的for循环,以便它不仅检查A列中的下一个空单元格,而且还用我想要的文本填充它?

1 个答案:

答案 0 :(得分:0)

我认为这段代码符合您的要求:

Sub bla()
    Dim colAstart As Integer
    Dim colAend As Integer
    Dim totalRows As Integer
    Dim colBstart As Integer

    colAstart = 61
    colAend = 90

    Dim i As Integer
    For i = 1 To (colAend - colAstart + 1)
        If Cells(i + 42, 2) <> "" Then
            Cells(i + 60, 1) = Cells(i + 42, 2)
        End If
    Next i
End Sub

您不需要范围对象,只需使用Cells属性并检查“B”(2)列中的内容。