Whats wrong with my loop? Only a beginner!!!

rob_sheeds

Board Regular
Joined
Dec 9, 2010
Messages
57
Hi, my loop is red and wont work. Can you suggest a change?
Thanks in advance!!!!!

Public Sub Hide_Sheets_If_Zero_Closing_Bal()
Worksheets("Summary").Activate
Range("E2").Select
Do While ActiveCell.Value <> ""
If ActiveCell.Value = "0" Then
Worksheets(ActiveCell.Offset(0, -4)).Visible = False
Else
Worksheets(ActiveCell.Offset(0, -4)).Visible = True
End If
Range(ActiveCell.Offset(1, 4)).Select
Loop Until (ActiveCell.Value = "")
End Sub
 
Last edited:
Tried that but getting subscript out of range error on the line
Worksheets(cell.Offset(0, -4).Value).Visible = cell.Value <> 0

My guess is one of the cells in column A doesn't have a worksheet with the exact name in the cell. So when the line of code tries to Show\Hide that worksheet, the sheet doesn't exist.

You can try and fix the sheet name in column A or use this code which will ignore a sheet name from column A that it can't find a matching worksheet.

<font face=Courier New><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Hide_Sheets_If_Zero_Closing_Bal()<br>    <SPAN style="color:#00007F">Dim</SPAN> cell <SPAN style="color:#00007F">As</SPAN> Range<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">With</SPAN> Worksheets("Summary")<br>        <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> cell <SPAN style="color:#00007F">In</SPAN> .Range("E2", .Range("E2").End(xlDown))<br>            <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>            Worksheets(cell.Offset(0, -4).Value).Visible = cell.Value <> 0<br>            <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>        <SPAN style="color:#00007F">Next</SPAN> cell<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>
 
Upvote 0

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I copied that in and now pressing the button doesnt do anything.
Macro is assigned properly and security settings checked....
Checked without the Screenupdating lines too and still nothing...
Appreciate your time on this!!!
 
Upvote 0
Change this line...
Worksheets(cell.Offset(0, -4).Value).Visible = cell.Value <> 0

To this...
Worksheets(CStr(cell.Offset(0, -4).Value)).Visible = cell.Value <> 0

It converts the numeric values from column A into Text to then be used as the worksheet name.
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,831
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