Looping through all WS in WB not working using For

Prevost

Board Regular
Joined
Jan 23, 2014
Messages
198
Hi There. My code below is supposed to highlight all the rows of the cells in a specified range (in column B) that begin with a letter. I am trying to loop through all the worksheets in the workbook and am using the FOR counter from 2 to the end of the WS. However, it only works on the active sheet. Does anyone know why? I also cannot get it to work by using 'For Each WS in ActiveWorkbook.Worksheets', even after declaring WS as Worksheet. Any help would be greatly appreciated!


Code:
Sub Highlight()
    Dim WS As Worksheet
    Dim Cell As Range, NewRange As Range
    Dim i As Long, j As Long
        For j = 2 To Worksheets.Count
            With Sheets(j)
                i = Cells(Rows.Count, "B").End(xlUp).Row
                Set NewRange = Range(Cells(2, 2), Cells(i, 2))
            End With
                For Each Cell In NewRange
                    If IsNumeric(Left(Cell, 1)) = False Then
                        Cell.EntireRow.Interior.ColorIndex = 8
                    End If
                Next Cell
        Next j
End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi There. My code below is supposed to highlight all the rows of the cells in a specified range (in column B) that begin with a letter. I am trying to loop through all the worksheets in the workbook and am using the FOR counter from 2 to the end of the WS. However, it only works on the active sheet. Does anyone know why? I also cannot get it to work by using 'For Each WS in ActiveWorkbook.Worksheets', even after declaring WS as Worksheet. Any help would be greatly appreciated!


Code:
Sub Highlight()
    Dim WS As Worksheet
    Dim Cell As Range, NewRange As Range
    Dim i As Long, j As Long
        For j = 2 To Worksheets.Count
            With Sheets(j)
                i = Cells(Rows.Count, "B").End(xlUp).Row
                Set NewRange = Range(Cells(2, 2), Cells(i, 2))
            End With
                For Each Cell In NewRange
                    If IsNumeric(Left(Cell, 1)) = False Then
                        Cell.EntireRow.Interior.ColorIndex = 8
                    End If
                Next Cell
        Next j
End Sub
Try qualifying the ranges in your With-End With construct like this:
Rich (BB code):
With Sheets(j)
    i = .Cells(Rows.Count, "B").End(xlUp).Row
    Set NewRange = .Range(.Cells(2, 2), .Cells(i, 2))
End With
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

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