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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26263434/
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-12 04:46:54  来源:igfitidea点击:

Excel-VBA : Skip subroutine if cell is empty

excelvbaexcel-vba

提问by Jongscx

Ok, so I have 10 columns, labelled "A"-"J".

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

Each Row will have some combination of these columns filled in with string values.

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

I need to run some conditional statements and I'm wondering if there is a more efficient method of doing them without simply looping through them all.

我需要运行一些条件语句,我想知道是否有更有效的方法来执行它们而无需简单地循环遍历它们。

What I have now:

我现在所拥有的:

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

It's long, cumbersome, and not very pretty. Moreover, it takes a long time because we have a few hundred records (rows) to go through. Is there a faster way to look at X Row, say A,B,C,E,&J have things, and do stuff based on that.

它很长,很麻烦,而且不是很漂亮。而且,这需要很长时间,因为我们有几百条记录(行)要通过。有没有一种更快的方式来查看 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.

回答by

I'm not entirely sure of the order in which cells should be checked but perhaps this will get you started.

我不完全确定应该检查单元格的顺序,但也许这会让你开始。

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