在另一个Excel VBA中运行One For循环

时间:2016-02-19 11:19:50

标签: excel vba excel-vba

我正在尝试运行两个for循环,一个在另一个循环中。每次外循环运行时,内循环应该一直运行。我附上了我的代码,但目前还没有。还有代码将结果写入外部csv文件,如果任何人都可以看到任何明显的错误,那么我不知道如何做到这一点,我将非常感激。提前谢谢。

Public Sub Practice1()
Dim UpLim1 As Double, UpLim2 As Double, LowLim1 As Double, LowLim2 As Double
Dim outcome As String, FilePath As String, MtchUIDs As String
Dim i As Long, j As Long
Dim SRCUID As String, SNKUID As String

MtchUIDs = ""

FilePath = Application.DefaultFilePath & "\ffpstage1.csv"


Open FilePath For Output As #2

For i = 2 To 91
    For j = 2 To 90

        UpLim1 = Range("d" & i).Value
        LowLim1 = Range("c" & j).Value
        UpLim2 = Range("j" & i).Value
        LowLim2 = Range("i" & j).Value
        SRCUID = Range("a" & i).Value
        SNKUID = Range("g" & j).Value


        If UpLim2 >= LowLim1 And LowLim1 >= LowLim2 Then
           MtchUIDs = SRCUID & SNKUID

        ElseIf UpLim1 > LowLim2 And LowLim2 >= LowLim1 Then
            MtchUIDs = SRCUID & SNKUID

             Write #2, MtchUIDs
             MtchUIDs = ""
        Else
             Next j

        End If

Next i

End Sub

1 个答案:

答案 0 :(得分:2)

End If循环中需要For j,请尝试循环播放:

For i = 2 To 91
    For j = 2 To 90

        UpLim1 = Range("d" & i).Value
        LowLim1 = Range("c" & j).Value
        UpLim2 = Range("j" & i).Value
        LowLim2 = Range("i" & j).Value
        SRCUID = Range("a" & i).Value
        SNKUID = Range("g" & j).Value

        If UpLim2 >= LowLim1 And LowLim1 >= LowLim2 Then
           MtchUIDs = SRCUID & SNKUID
        ElseIf UpLim1 > LowLim2 And LowLim2 >= LowLim1 Then
            MtchUIDs = SRCUID & SNKUID
            Write #2, MtchUIDs
            MtchUIDs = ""
        End if

    Next j

Next i