Error in Loop

Stan101

New Member
Joined
Sep 2, 2016
Messages
25
I am trying to make a small VBA script that looks at sheet names and if a sheet matches the criteria, sets a range.
I want it to then look in that range for an empty cell. If it finds an empty cell, I want it to hide the cell.

After finishing the range it then looks for the next suitable sheet in the workbook. It does this until no more sheets are available.

VBA Code:
Sub HideUnusedRowsFloorRoof()

Dim cel As Range, rng As Range, ws As Worksheet

For Each ws In ActiveWorkbook.Sheets
If ws.Name Like "*Floor" Or ws.Name Like "*Roof" Then


Set rng = Range("M5:M200")
    

    
    For Each cel In rng
    
        If cel.Value = "" Then
            
           cel.EntireRow.Hidden = True
            
      End If
        
    Next cel
    
    End If
    
Next ws


  
End Sub

The code is not looking for the empty cells. What am I doing wrong?

I wrote this for just a single sheet and used to actuate it from a button and it worked fine. I think when I have a nested if statement, I am doing something wrong.
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Don't need to loop cells :
VBA Code:
Sub HideUnusedRowsFloorRoof()
Dim ws As Worksheet
On Error Resume Next
For Each ws In ActiveWorkbook.Sheets
    If ws.Name Like "*Floor" Or ws.Name Like "*Roof" Then _
        ws.Range("M5:M200").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
Next ws
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,979
Members
449,276
Latest member
surendra75

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