Reputation: 37
I'm trying to run two for loops, one inside another. The inner loop should run all the way through every time the outer loop runs. I have attached my code but it's not working at the moment. There is also code to write the results to an external csv file, something which I have no idea how to do so if anyone can see any obvious mistakes then that would be much appreciated. Thank you in advance.
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
Upvotes: 2
Views: 107
Reputation: 4514
You need an End If
within the For j
loop, try this for the loops:
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
Upvotes: 2