Excel VBA:如果单元格为空,则跳过子例程



好的,所以我有10列,标记为"A"-"J"。

每一行都会有一些这些列的组合,用字符串值填充。

我需要运行一些条件语句,我想知道是否有一种更有效的方法可以在不简单地循环所有条件语句的情况下完成这些语句。

我现在拥有的:

If isempty("A1) then
Else
    if isempty("B1") then
    else
        Sheet2!"B1" = "A1  and B1"
    end if
    if isempty("C1") then
    else
        Sheet2!"A1" = "A1 and C1"
    end if
    [...etc]
end if
If isempty("B1) then
Else
    if isempty("C1") then
    else
        Sheet2!"B1" = "B1 and C1"
    end if
    if isempty("D1") then
    else
        Sheet2!"C1" = "C1 and D1"
    end if
    [...etc]
end if

它又长又笨重,而且不太漂亮。此外,这需要很长时间,因为我们有几百条记录(行)要处理。有没有一种更快的方法来看待X行,比如a,B,C,E,&J有东西,并以此为基础做事。

If A,C,&J are filled Do this.. 
If B is empty do this...
If C Or D is full, do this other thing.

我不完全确定检查单元格的顺序,但这可能会让你开始。

Dim rw As Long, lr As Long
With Cells(1, 1).CurrentRegion
    lr = .Rows.Count
    For rw = 1 To lr
        If Application.CountA(Range("A" & rw & ",C" & rw & ",J" & rw)) = 3 Then
            'If A,C,&J are filled Do this..
        ElseIf IsEmpty(Range("B" & rw)) Then
            'If B is empty do this...
        ElseIf CBool(Application.CountA(Range("C" & rw & ",D" & rw))) Then
            'If C Or D is full, do this other thing.
        End If
    Next rw
End With

最新更新