在具有多个变量的 For 循环中时,如何编写 如果变量 1 和变量 2 在 samt 时间出现,请跳到第一个 for 循



我有一个打开文件的代码,当我想打开所有文件时,它效果很好。但是,如果我不想打开某些文件,具体取决于文件名中的变量组合,我不知道该怎么做。我也不知道要搜索什么,所以我在万维网上的研究并不成功。

我知道附加的代码很长,但它并不复杂,但我陷入困境的地方是我不知道该怎么做。我尝试了如果..但是"下一个X"行给出错误消息"下一个没有For"。如果我将 If 更改为"For Z = 2 到 2",我会收到"for 变量已被使用"的错误。

所以问题是:如何防止打开具有特殊组合的文件,例如 X = 2 和 Z = 11。(其余的组合可以在下面的代码中看到)

Sub OpenFiles_Specified()
'opens all desired files in a folder
Dim MyFolder As String
Dim AllFiles As String
Dim X As Integer
Dim Y As Single
Dim Z As Integer
Dim i As Integer

Application.ScreenUpdating = False
For X = 2 To 6                          '2-12       :Folders(BeamLength)
    For Y = 1 To 1 Step 0.5             '1, 1.5, 2  :Bracings
        For Z = 0 To 3                  '0-3        :Load Cases
            For i = 1 To 1              '1-5        :Iterations
            If Z = 2 Then
                If X = 11 Or X = 10 Or X = 8 Or X = 7 Or X = 5 Or X = 4 Or X = 2 Then
                    Next X
                Else
                    'Do nothing
                End If
            ElseIf Z = 3 Then
                If X = 11 Or X = 9 Or X = 7 Or X = 5 Or X = 3 Then
                    Next X
                Else 
                    'Do nothing
                End If
            Else
                'ok combination
            End If
            ResultFolder = "C:Beamresultsresults"
            AllFiles = Dir(ResultFolder & "" & "Length" & X & "_Bracing" & Y & "_LoadCase" & Z & "_Iteration" & i & "_.xls")
            Do While AllFiles <> ""
            Workbooks.Open Filename:=ResultFolder & "" & AllFiles
            AllFiles = Dir
            Loop
            Next i
        Next Z
    Next Y
Next X
End Sub

这就是我解决问题的方式,我使用了GoTo。我已经看到它不是最优选使用的命令,但它非常适合此任务。

Sub Exclude_unvalid_loadcases()
'opens all desired files in a folder
Dim MyFolder As String
Dim AllFiles As String
Dim X As Integer
Dim Y As Single
Dim Z As Integer
Dim i As Integer

Application.ScreenUpdating = False
For X = 2 To 6                          '2-12       :Folders
    For Y = 1 To 1 Step 0.5             '1, 1.5, 2  :Bracings
        For Z = 0 To 3                  '0-3        :Load Cases
            For i = 1 To 1              '1-5        :Iterations

            If Z = 2 And X = 2 Then GoTo LastLine
            If Z = 2 And X = 4 Then GoTo LastLine
            If Z = 2 And X = 5 Then GoTo LastLine
            If Z = 2 And X = 7 Then GoTo LastLine
            If Z = 2 And X = 8 Then GoTo LastLine
            If Z = 2 And X = 10 Then GoTo LastLine
            If Z = 2 And X = 11 Then GoTo LastLine
            If Z = 3 And X = 3 Then GoTo LastLine
            If Z = 3 And X = 5 Then GoTo LastLine
            If Z = 3 And X = 7 Then GoTo LastLine
            If Z = 3 And X = 9 Then GoTo LastLine
            If Z = 3 And X = 11 Then GoTo LastLine
            ResultFolder = "C:Beamresultsresults"
        AllFiles = Dir(ResultFolder & "" & "Length" & X & "_Bracing" & Y & "_LoadCase" & Z & "_Iteration" & i & "_.xls")
            Do While AllFiles <> ""
            Workbooks.Open Filename:=ResultFolder & "" & AllFiles
            AllFiles = Dir

LastLine:
Loop
            Next i
        Next Z
    Next Y
Next X
End Sub

最新更新