VBA: for loop unexpectedly ignoring first row....

BenGee

Board Regular
Joined
Mar 5, 2016
Messages
196
Hi - the below code does as expected for rows 3 to 21. But for some reason, consistently ignores row 2.

Even if I change the range the range to;
VBA Code:
For i = 1 to 21

row 2 is still ignored. The value in "U" is the same as all other ranges.

Full code;
VBA Code:
    For Each ws In ThisWorkbook.Worksheets
        If ws.Name <> "Rota" And ws.Name <> "Key" And ws.Name <> "Template" Then
            For i = 2 To 21
                    If ws.Range("U" & i).Value = "E" Then
                        ws.Range("B" & i).Resize(, 18).Value = ""
                        ws.Range("D" & i).Value = "Break"
                        ws.Range("N" & i).Value = "Break"
                        ws.Range("H" & i).Resize(, 2).Value = "Lunch"
                        ws.Range("R" & i).Resize(, 2).Value = "Unstaffed"
                    End If
                    If ws.Range("U" & i).Value = "L" Then
                        ws.Range("B" & i).Resize(, 18).Value = ""
                        ws.Range("G" & i).Value = "Break"
                        ws.Range("Q" & i).Value = "Break"
                        ws.Range("B" & i).Resize(, 2).Value = "Unstaffed"
                        ws.Range("L" & i).Resize(, 2).Value = "Lunch"
                    End If
                    If ws.Range("U" & i).Value = "MS" Then
                        ws.Range("B" & i).Resize(, 18).Value = ""
                        ws.Range("F" & i).Value = "Break"
                        ws.Range("O" & i).Value = "Break"
                        ws.Range("B" & i).Value = "Unstaffed"
                        ws.Range("S" & i).Value = "Unstaffed"
                        ws.Range("J" & i).Resize(, 2).Value = "Lunch"
                    End If
            Next i
        End If
    Next ws

Thanks in advqance
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The value in "U" is the same as all other ranges
Are you sure? Same case, no leading/trailing spaces etc? Have you tried stepping through the code to see which lines are processed for row 2?
 
Upvote 0

Forum statistics

Threads
1,216,075
Messages
6,128,657
Members
449,462
Latest member
Chislobog

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