Hi all,
I have a VBA code to find largest value according to the capacity comparison. But if the result of comparison does not get "true", I have to stop the for loop of "k"
and make it start at the beginning of for loop of j.
Sub Compare()
Dim EstMax As Range
Dim RealMax As Range
Sheets("Sayfa2").Activate
Set EstMax = Range("A" & Rows.Count)
For j = 2 To 62
For i = 2 To 62
If Cells(i, 2) > EstMax.Value Then
Set EstMax = Cells(i, 2)
Row = i
End If
Next i
a = 0
If Row < 7 Then
a = 8 - Row
End If
k = Row - 1
For k = Row - 1 To k = (Row - 7 + a) Step -1
If k = 0 Then
Exit For
Else
If Cells(k, 2) = 0 Then
If Cells(Row, 3) + Cells(k, 3) < 8 Then
Set RealMax = EstMax
Else
EstMax = -1
'Must be stopped and should begin at beginning of loop for j
End If
End If
End If
Next k
Next j
MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
End Sub
If I call another macro as a result of false, then I have recursive VBA.
Sub Compare()
Dim EstMax As Range
Dim RealMax As Range
Sheets("Sayfa2").Activate
Set EstMax = Range("A" & Rows.Count)
For j = 2 To 62
For i = 2 To 62
If Cells(i, 2) > EstMax.Value Then
Set EstMax = Cells(i, 2)
Row = i
End If
Next i
a = 0
If Row < 7 Then
a = 8 - Row
End If
k = Row - 1
For k = Row - 1 To k = (Row - 7 + a) Step -1
If k = 0 Then
Exit For
Else
If Cells(k, 2) = 0 Then
If Cells(Row, 3) + Cells(k, 3) < 8 Then
Set RealMax = EstMax
Else
EstMax = -1
Call Compare2
End If
End If
End If
Next k
Next j
MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
End Sub
Sub Compare2()
Dim EstMax As Range
Dim RealMax As Range
Sheets("Sayfa2").Activate
Set EstMax = Range("A" & Rows.Count)
For j = 2 To 62
For i = 2 To 62
If Cells(i, 2) > EstMax.Value Then
Set EstMax = Cells(i, 2)
Row = i
End If
Next i
a = 0
If Row < 7 Then
a = 8 - Row
End If
k = Row - 1
For k = Row - 1 To k = (Row - 7 + a) Step -1
If k = 0 Then
Exit For
Else
If Cells(k, 2) = 0 Then
If Cells(Row, 3) + Cells(k, 3) < 8 Then
Set RealMax = EstMax
Else
EstMax = -1
Call Compare2
End If
End If
End If
Next k
Next j
MsgBox RealMax.Value & " is in cell " & RealMax.Address & " in row " & Row
End Sub
What should I do to deal with this problem?
Thanks in advance,
Oxco
Bookmarks