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

时间:2017-12-14 10:07:46

标签: excel vba excel-vba

我在VBA脚本中遇到问题,执行步骤,比如 A-E 。 我想要做的是如果 E2的最后一个字段不包含,那么它应该只执行A-D

如果此单元格没有内容,只有A-C ,则相同,直到 A-B ,因为它们永远不会为空。 我希望你能帮助我找到一个解决方案,我不知道如何实现它,因为我还是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

1 个答案:

答案 0 :(得分:1)

所以我建议使用:

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