如果单元格为空,则转到代码中的下一行 – VBA Excel

AE说,我的VBA脚本中有一个问题,它执行这些步骤。 我想要做的是如果E2的最后一个字段不包含任何内容 ,那么它应该只执行AD

D2也是一样,如果这个单元格没有内容,那么只有AC等等,直到AB,因为这些都不是空的。 我希望你能帮我find一个解决scheme,我不知道如何实现它,因为我还是很新的VBA。 先谢谢你!

下面是对于每一种可能性的脚本摘录。

Sub Variations() Dim rngAList, rngBList, rngCList, rngDList, rngEList As Range Dim rngA, rngB, rngC, rngD, rngE As Range Dim strVariationList As String Set rngAList= Tabelle3.Range(Tabelle3.Range("B2"), Tabelle3.Range("B2").End(xlDown)) Set rngBList= Tabelle3.Range(Tabelle3.Range("D2"), Tabelle3.Range("D2").End(xlDown)) Set rngCList= Tabelle3.Range(Tabelle3.Range("F2"), Tabelle3.Range("F2").End(xlDown)) Set rngDList= Tabelle3.Range(Tabelle3.Range("G2"), Tabelle3.Range("G2").End(xlDown)) Set rngEList= Tabelle3.Range(Tabelle3.Range("H2"), Tabelle3.Range("H2").End(xlDown)) Tabelle3.Range("I2").Select For Each rngA In rngAList For Each rngB In rngBList For Each rngC In rngCList For Each rngD In rngDList For Each rngE In rngEList ActiveCell = "" & rngA.Value & " " & rngB & " " & rngC & " " & rngD & " " & rngE If strVariationList = "" Then strVariationList = ActiveCell Else strVariationList = strVariationList & ", " & ActiveCell End If ActiveCell.Offset(1, 0).Select Next Next Next Next Next Tabelle3.Range("J2").Select For Each rngA In rngAList For Each rngB In rngBList For Each rngC In rngCList For Each rngD In rngDList ActiveCell = "" & rngA.Value & " " & rngB & " " & rngC & " " & rngD If strVariationList = "" Then strVariationList = ActiveCell Else strVariationList = strVariationList & ", " & ActiveCell End If ActiveCell.Offset(1, 0).Select Next Next Next Next 

所以我build议使用:

 Dim testEmpty As String 'before reading cell value clear string testEmpty = "" If(IsEmpty(testEmpty) = true) Then GoTo OverStepCode End If ' here code that executes when cell is not empty OverStepCode: ' here code that executes always but You overstep code that should not be executed when cell is empty