当在具有多个变量的For循环中时,如何写入如果变量1和变量2在samt时发生,则跳转到1st for next中的下一个变量?

时间:2014-12-08 14:10:41

标签: vba excel-vba excel

我有一个打开文件的代码,当我想打开所有文件时它很有用。但是,如果我不想打开某些文件,根据文件名中的变量组合,我不知道该怎么做。我不太清楚要搜索什么,所以我对广告网的研究并不成功。

我知道附带的代码很长,但它并不复杂,但我遇到的问题是我不知道该怎么办。我尝试过If ..但是行#34; Next X"提供错误消息"接下来没有For" 。如果我将If更改为" For Z = 2 To 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:\Beamresults\results"
            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

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法,我使用了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:\Beamresults\results"
        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