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

如果单元格为空,请转到代码-VBA Excel中的下一行,vba,excel,Vba,Excel,我的VBA脚本中有一个问题,它执行步骤,例如a-E。 我希望它做的是,如果E2的最后一个字段不包含任何内容,那么它应该只执行A-D 同样适用于D2,如果该单元格没有内容,则只有A-C,依此类推,直到A-B,因为这些单元格从不为空。 我希望你能帮我找到一个解决方案,我不知道如何实现它,因为我还是VBA的新手。 提前谢谢你 下面是到目前为止针对各种可能性的脚本摘录 Sub Variations() Dim rngAList, rngBList, rngCList, rngDList, rngELi

我的VBA脚本中有一个问题,它执行步骤,例如a-E。 我希望它做的是,如果E2的最后一个字段不包含任何内容,那么它应该只执行A-D

同样适用于D2,如果该单元格没有内容,则只有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
因此,我建议使用:

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

您可以使用Exit For提前终止循环,这就是您要寻找的吗?您实际上想在这里实现什么?使用if语句检查单元格是否为空,然后使用Exit For在发现单元格为空时离开循环。首先感谢您的快速反馈!这是一个我试图用变量输入构建的置换工具,就像我说的,如果列单元格X是空置换列1-4,如果列4是空置换列1-3等等:)你的源区域中有多少个单元格?这一定要花很长时间才能完成!还有,为什么要做两次呢?为什么不直接将列
J
设置为列
I