Nested for Each loops not working

lukeois

New member
Joined
Jun 25, 2017
Messages
6
Reaction score
0
Points
0
Hi all,

I have a workbook that contains lots of worksheets including 12 names after each month. I have a named range which refers to this list called Months. I am trying to run a macro that cycles only through the worksheets listed in 'Months' and then hides rows where the value = 0. The issue is it does not seem to cycle through the worksheets. any help would be appreciated:

Code:
Sub months()


Application.ScreenUpdating = False


Dim rl As Range
Dim v As Range


    For Each v In Range("Months")
    
       For Each rl In Range("B15:B44")
        
           If rl.Value = "" Then
            
               rl.EntireRow.Hidden = True


           End If


       Next rl
            
    Next v


Application.ScreenUpdating = True


End Sub
 
Update

Hi again all,

as an update (and after doing a little more checking) I have modified my code. Again it works on the worksheet selected but does not loop through. From the forums I have looked at am I supposed to be qualifying the the first For Each statement?

Code:
Sub months()


Application.ScreenUpdating = False


Dim ws As Worksheet
Dim cell As Range


    For Each ws In Worksheets
        
        For Each cell In Range("B15:B44")
        
           If cell.Value = "" Then
            
               cell.EntireRow.Hidden = True
           
           End If
        
        Next cell
    
    Next ws


Application.ScreenUpdating = True


End Sub
 
Try this way
Code:
Sub months_altered()

Dim rl As Range
Dim v As Range

Application.ScreenUpdating = False

For Each v In Range("Months")
    With Sheets(v.Value)    '<~~~ the sheet to work on
        For Each rl In .Range("B15:B44")
            If rl.Value = "" Then
                rl.EntireRow.Hidden = True
            End If
        Next rl
    End With
Next v

Application.ScreenUpdating = True

End Sub
 
RESOLVED - Nested for Each loops not working

NoS - Worked perfectly, thanks for the assist. Have a great day..........


Try this way
Code:
Sub months_altered()

Dim rl As Range
Dim v As Range

Application.ScreenUpdating = False

For Each v In Range("Months")
    With Sheets(v.Value)    '<~~~ the sheet to work on
        For Each rl In .Range("B15:B44")
            If rl.Value = "" Then
                rl.EntireRow.Hidden = True
            End If
        Next rl
    End With
Next v

Application.ScreenUpdating = True

End Sub
 
Back
Top