Hide sheets macro - "Run-time error '9': Subscript out of range" error

Cflaursen

New Member
Joined
Apr 6, 2011
Messages
17
Hi all

I need to program a macro that will hide certain sheets on workbook startup depending on a certain cell and if that certain cell is changed while the workbook is open. To accomplish this I have written the following macro.


Private Sub Workbook_Open()
'Macro to hide either Clarity or CTR related sheets'
If Sheets("Sheet2").Range("BD2") = "CTR" Then
Sheets("Sheet7").Visible = xlVeryHidden
Sheets("Sheet17").Visible = xlVeryHidden
Sheets("Sheet16").Visible = xlVeryHidden
Sheets("Sheet26").Visible = xlVeryHidden
Sheets("Sheet27").Visible = xlVeryHidden

ElseIf Sheets("Sheet2").Range("BD2") = "Clarity" Then
Sheets("sheet8").Visible = xlVeryHidden
Sheets("sheet19").Visible = xlVeryHidden
Sheets("sheet18").Visible = xlVeryHidden
Sheets("sheet23").Visible = xlVeryHidden
Sheets("sheet22").Visible = xlVeryHidden
Else
Exit Sub
End If
End Sub


However, when I run the macro I get the following error "Run-time error '9': Subscript out of range". I can't figure out why - hope someone can help me out?

I have tried using the "Auto_open" instead but gets the same problem. Can it be because I just use the generic sheet names or the total (real) sheet names?

Br, CFL
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Use this:

On Error Resume Next

before "if" statement and:

On Error Goto 0

after "end if" if the sheet names are changing.
 
Upvote 0
Hi all

Thank you for the replies.

I have double checked that sheets exist.

Furthermore sheet names are static!

WhoCanDo: What exactly does your suggestion do? I have implemented your solution are don't get the error anymore but the macro doesn't perform the wanted action...

Br, CFL
 
Last edited:
Upvote 0
"On Error Resume Next" stops Excel telling you there is an error and continues on.

"On Error goto 0" activates error reports again for the rest of your code.

It is not a solution to why there is an error but the error is not causing a problem so why not ignore it.

I agree with jkpieterse but if you say that all names are correct and you can't solve it then, dare I say, ignore it.
 
Upvote 0
Maybe there is a space in some name. Something like
Sheet 7 instead of Sheet7

M.
 
Upvote 0
Can it be because I just use the generic sheet names or the total (real) sheet names?

Br, CFL

Sorry Cflaursen, just read your question properly.

Yes it is because you need to use the names of the sheets when you use the "Sheets" function or you can use "Sheets(1)" as Excel creates them but that can be confusing. eg. Sheet1 (test) - Sheets("test") or Sheets(1)

Regards
 
Upvote 0

Forum statistics

Threads
1,224,534
Messages
6,179,390
Members
452,909
Latest member
VickiS

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