VBA Error if more than 1x spreadsheet open

tlc53

Active Member
Joined
Jul 26, 2018
Messages
399
Hi,

I have code in my main spreadsheet which works fine, no problems at all, until I open another spreadsheet. If two or more spreadsheets are open and I do anything in any of the spreadsheets (select from drop down menu for example) it produces "Run time error 9 - Subscript out of range" referring to my main spreadsheet. It highlights the first line in the below code. Originally I had it referring to ActiveSheet but changed it to the actual sheet name "Q2" but that has not stopped this error from occurring. Does anyone know why this keeps happening and how I can fix it? Thank you

VBA Code:
Private Sub Worksheet_Calculate()

    If Sheets("Q2").Range("D8").Value = 1 Then
        Sheets("Q2").Shapes("Check Box 2").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 2").Visible = True
    End If
    
    
    If Sheets("Q2").Range("D8").Value = 3 Then
        Sheets("Q2").Shapes("Check Box 4").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 4").Visible = True
    End If
            
            
    If Sheets("Q2").Range("D8").Value = 5 Then
        Sheets("Q2").Shapes("Check Box 6").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 6").Visible = True
    End If
        
        
    If Sheets("Q2").Range("D8").Value = 7 Then
        Sheets("Q2").Shapes("Check Box 8").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 8").Visible = True
    End If
        
        
    If Sheets("Q2").Range("D8").Value = 9 Then
        Sheets("Q2").Shapes("Check Box 10").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 10").Visible = True
    End If
        
        
    If Sheets("Q2").Range("D8").Value = 11 Then
        Sheets("Q2").Shapes("Check Box 12").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 12").Visible = True
    End If
    
        
    If Sheets("Q2").Range("D8").Value = 13 Then
        Sheets("Q2").Shapes("Check Box 14").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 14").Visible = True
    End If
    
        
    If Sheets("Q2").Range("D8").Value = 15 Then
        Sheets("Q2").Shapes("Check Box 16").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 16").Visible = True
    End If
    
        
    If Sheets("Q2").Range("D8").Value = 17 Then
        Sheets("Q2").Shapes("Check Box 18").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 18").Visible = True
    End If
    
        
    If Sheets("Q2").Range("D8").Value = 19 Then
        Sheets("Q2").Shapes("Check Box 20").Visible = False
    Else
        Sheets("Q2").Shapes("Check Box 20").Visible = True
    End If
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Make Sheets("Q2") the activesheet, run the code below then copy & paste the result that appears in the Immediate window in the thread (please copy and paste the result, do not retype it).

VBA Code:
Sub testit()
Debug.Print "| &"; ActiveSheet.Name; "& |", Len(ActiveSheet.Name)
End Sub
 
Upvote 0
What you have posted is correct. When you stated that the first line is highlighted did you mean
This line
VBA Code:
Private Sub Worksheet_Calculate()
or
This line
Code:
If Sheets("Q2").Range("D8").Value = 1 Then
 
Upvote 0
Sorry, the second line;

VBA Code:
    If Sheets("Q2").Range("D8").Value = 1 Then
 
Upvote 0
Try changing the line to
VBA Code:
If ThisWorkbook.Sheets("Q2").Range("D8").Value = 1 Then
 
Upvote 0
Solution
You're welcome, obviously if you haven't already then you will have to append ThisWorkbook to everywhere else that you have Sheets("Q2").
 
Upvote 0
You're welcome, obviously if you haven't already then you will have to append ThisWorkbook to everywhere else that you have Sheets("Q2").
Yes. Originally I just applied it to the first line but I soon gathered it needed to added to every line referring to Sheets("Q2"). Works a charm now.
 
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