繁体   English   中英

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

[英]Excel-VBA : Skip subroutine if cell is empty

好的,所以我有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 Row,比如说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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM