2014-10-08 270 views
0

好的,所以我有10列,标记为“A” - “J”。Excel-VBA:跳过子程序,如果单元格为空

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

我需要运行一些条件语句,我想知道是否有一个更有效的方法来做到这一点,而不仅仅是循环遍历它们。

我现在拥有的一切:

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. 
+0

您正在查找逻辑运算符(“和”和“或”运算符)。例如如果isempty(“c1”)和isempty(b1)然后...看到:http://www.excel-easy.com/vba/examples/logical-operators.html – Kevin 2014-10-08 18:05:21

+0

我可能只是用自己的努力这是一张脸掌。这很明显,只是把我凝视在脸上。 >。< Isempty(A)= A; Isempty(B)= B; IF A和B DO ... – Jongscx 2014-10-08 18:27:14

回答

1

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

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 
相关问题