VBA For loop question

Donai

Well-known Member
Joined
Mar 28, 2009
Messages
543
Could someone please explain what the -1 step 1 means

For a = 6 To LR - 1 Step 1

For i = 6 To LR
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
Maybe this is clearer (but the brackets are not needed)

For a = 6 To (LR - 1) Step 1

It loops from 6 to the value in LR less one, in steps of 1. So 6, 7, 8,... LR-1.
 
Upvote 0
Maybe this is clearer (but the brackets are not needed)

For a = 6 To (LR - 1) Step 1

It loops from 6 to the value in LR less one, in steps of 1. So 6, 7, 8,... LR-1.

So would i use the step on the below, i am getting the code to add Error or ok if there is a difference between Col 2 and 3

Code:
LR = ShtSummary.Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To LR
        If ShtSummary.Cells(i, 2) <> ShtSummary.Cells(i, 3) Then
                ShtSummary.Cells(i, 4) = "ERROR"
            Else
                ShtSummary.Cells(i, 4) = "OK"
        End If
    Next i
 
Upvote 0
You don't need Step in that case because Step 1 is the default.

You use Step when you want for example

For i = 2 to 10 Step 2

gives 2,4,...10

Or loop backwards as when deleting rows

For i = LR to 2 Step -1
 
Upvote 0
You don't need Step in that case because Step 1 is the default.

You use Step when you want for example

For i = 2 to 10 Step 2

gives 2,4,...10

Or loop backwards as when deleting rows

For i = LR to 2 Step -1

Ok i get ya. Peter one more thing how can i add a conditional format to (i,4) if there is an Error then fill cell with red
 
Upvote 0
Try

Code:
LR = ShtSummary.Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To LR
        If ShtSummary.Cells(i, 2) <> ShtSummary.Cells(i, 3) Then
                ShtSummary.Cells(i, 4) = "ERROR"
                ShtSummary.Cells(i, 4).Interior.ColorIndex = 3
            Else
                ShtSummary.Cells(i, 4) = "OK"
        End If
    Next i
 
Upvote 0
Awesome thanks

Try

Code:
LR = ShtSummary.Cells(Rows.Count, 1).End(xlUp).Row
For i = 6 To LR
        If ShtSummary.Cells(i, 2) <> ShtSummary.Cells(i, 3) Then
                ShtSummary.Cells(i, 4) = "ERROR"
                ShtSummary.Cells(i, 4).Interior.ColorIndex = 3
            Else
                ShtSummary.Cells(i, 4) = "OK"
        End If
    Next i
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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