Next loop do not work

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
I
I hope somebody can help me with my code.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
Idea is very simple: I have two columns of data to compare. It is montly results of some measurement. <o:p></o:p>
I need to find out non-logical results and with all corresponding combinations, e.g. in column E (were results from month ago) and in column F (were results from current month).
Non-logical results are all combinations in the way that results from one month do not follow results in the current month.
Correct, logical results for measurement results should be:
in month before is positive, and in current month is positive
in month before is negative, and in current month is negative

My code is as follows:
Sub SearchForProblemThenExit()
'Page 112
FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
ProblemFound = False
For i = 2 To FinalRow
If Cells(i, 5).Value < 0 Then
If Cells(i, 6).Value > 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i

For i = 2 To FinalRow
If Cells(i, 5).Value > 0 Then
If Cells(i, 6).Value < 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i

For i = 2 To FinalRow
If Cells(i, 5).Value = 0 Then
If Cells(i, 6).Value < 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i

For i = 2 To FinalRow
If Cells(i, 5).Value = 0 Then
If Cells(i, 6).Value > 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i

For i = 2 To FinalRow
If Cells(i, 5).Value < 0 Then
If Cells(i, 6).Value = 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i

For i = 2 To FinalRow
If Cells(i, 5).Value > 0 Then
If Cells(i, 6).Value = 0 Then
Cells(i, 8).Value = "Service Revenue"
Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
Exit For
End If
End If
Next i
End Sub


Credits for original code going to the book VBA & Macros: Microsoft Excel 2010, Chapter 5 - Looping

My main problem is, after i rewrite original code to fullfill my need that, code done just first row of data, and do not find all combination to the end of data.

Any tips?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Do not use variable 'i' several times, but use different characters for the variables.
 
Upvote 0
Revised logic.
Code:
[COLOR="Blue"]Sub[/COLOR] SearchForProblemThenExit()
     
     [COLOR="Blue"]Dim[/COLOR] FinalRow [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
     [COLOR="Blue"]Dim[/COLOR] ProblemFound [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR]
     
     
     FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
     ProblemFound = [COLOR="Blue"]False[/COLOR]
     
     [COLOR="Blue"]For[/COLOR] i = 2 [COLOR="Blue"]To[/COLOR] FinalRow
     
        [COLOR="Blue"]Select[/COLOR] [COLOR="Blue"]Case[/COLOR] Cells(i, 5)
            
            [COLOR="Blue"]Case[/COLOR] [COLOR="Blue"]Is[/COLOR] < 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value > 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value = 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                
            [COLOR="Blue"]Case[/COLOR] 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value < 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value > 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            
            [COLOR="Blue"]Case[/COLOR] [COLOR="Blue"]Is[/COLOR] > 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value < 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value = 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR="Blue"]Exit[/COLOR] [COLOR="Blue"]For[/COLOR]
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Select[/COLOR]
        
     [COLOR="Blue"]Next[/COLOR]
 
 [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Last edited:
Upvote 0
Revised logic.
Code:
[COLOR=blue]Sub[/COLOR] SearchForProblemThenExit()
 
     [COLOR=blue]Dim[/COLOR] FinalRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
     [COLOR=blue]Dim[/COLOR] ProblemFound [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR]
 
 
     FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
     ProblemFound = [COLOR=blue]False[/COLOR]
 
     [COLOR=blue]For[/COLOR] i = 2 [COLOR=blue]To[/COLOR] FinalRow
 
        [COLOR=blue]Select[/COLOR] [COLOR=blue]Case[/COLOR] Cells(i, 5)
 
            [COLOR=blue]Case[/COLOR] [COLOR=blue]Is[/COLOR] < 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value > 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value = 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
            [COLOR=blue]Case[/COLOR] 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value < 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value > 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
            [COLOR=blue]Case[/COLOR] [COLOR=blue]Is[/COLOR] > 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value < 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value = 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR]
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
        [COLOR=blue]End[/COLOR] [COLOR=blue]Select[/COLOR]
 
     [COLOR=blue]Next[/COLOR]
 
 [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]

tried, it do not work, just find first non-logical combination
any idea why not
 
Upvote 0
Ah, yes...
Code:
[COLOR="Blue"]Sub[/COLOR] SearchForProblemThenExit()
     
     [COLOR="Blue"]Dim[/COLOR] FinalRow [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Long[/COLOR]
     [COLOR="Blue"]Dim[/COLOR] ProblemFound [COLOR="Blue"]As[/COLOR] [COLOR="Blue"]Boolean[/COLOR]
     
     
     FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
     ProblemFound = [COLOR="Blue"]False[/COLOR]
     
     [COLOR="Blue"]For[/COLOR] i = 2 [COLOR="Blue"]To[/COLOR] FinalRow
     
        [COLOR="Blue"]Select[/COLOR] [COLOR="Blue"]Case[/COLOR] Cells(i, 5)
            
            [COLOR="Blue"]Case[/COLOR] [COLOR="Blue"]Is[/COLOR] < 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value > 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value = 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                
            [COLOR="Blue"]Case[/COLOR] 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value < 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value > 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
            
            [COLOR="Blue"]Case[/COLOR] [COLOR="Blue"]Is[/COLOR] > 0
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value < 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                [COLOR="Blue"]If[/COLOR] Cells(i, 6).Value = 0 [COLOR="Blue"]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]If[/COLOR]
                
        [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Select[/COLOR]
        
     [COLOR="Blue"]Next[/COLOR]
 
 [COLOR="Blue"]End[/COLOR] [COLOR="Blue"]Sub[/COLOR]
 
Upvote 0
Ah, yes...
Code:
[COLOR=blue]Sub[/COLOR] SearchForProblemThenExit()
 
     [COLOR=blue]Dim[/COLOR] FinalRow [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
     [COLOR=blue]Dim[/COLOR] ProblemFound [COLOR=blue]As[/COLOR] [COLOR=blue]Boolean[/COLOR]
 
 
     FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
     ProblemFound = [COLOR=blue]False[/COLOR]
 
     [COLOR=blue]For[/COLOR] i = 2 [COLOR=blue]To[/COLOR] FinalRow
 
        [COLOR=blue]Select[/COLOR] [COLOR=blue]Case[/COLOR] Cells(i, 5)
 
            [COLOR=blue]Case[/COLOR] [COLOR=blue]Is[/COLOR] < 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value > 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value = 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
            [COLOR=blue]Case[/COLOR] 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value < 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value > 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
            [COLOR=blue]Case[/COLOR] [COLOR=blue]Is[/COLOR] > 0
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value < 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
                [COLOR=blue]If[/COLOR] Cells(i, 6).Value = 0 [COLOR=blue]Then[/COLOR]
                    Cells(i, 8).Value = "Service Revenue"
                    Cells(i, 1).Resize(1, 8).Interior.ColorIndex = 4
                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
        [COLOR=blue]End[/COLOR] [COLOR=blue]Select[/COLOR]
 
     [COLOR=blue]Next[/COLOR]
 
 [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]


yes, yes it works now:rofl:. thanks.
(I figure out by myself - Exit for was mistake). Thanks again
 
Upvote 0

Forum statistics

Threads
1,224,541
Messages
6,179,418
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top