Why is windows.activate subscript out of range (error 9)?

sharonng

New Member
Joined
Jul 14, 2021
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
I am trying to activate a window that I have just opened. My code is as follows:
VBA Code:
Dim quarter As Integer
    Dim yr As Integer
    
    yr = InputBox("Enter the year:", "Year")
    quarter = InputBox("Enter the quarter (just the number):", "Quarter")
    
    If quarter <> 1 Then
    
            ' open new quarter file
            Workbooks.Open Filename:= _
                "J:\recoding\report_002\Report002_" & yr & "Q" & quarter & ".xls", UpdateLinks:=0
                
            ' open old quarter file
            Workbooks.Open Filename:= _
                "J:\recoding\report_002\Report002_" & yr & "Q" & (quarter - 1) & ".xls", UpdateLinks:=0
                
            'insert a row and a column at the most upper LHS
            Windows("Report002_" & yr & "Q" & (quarter - 1) & ".xls").Activate
When it reaches the last line, the error subscript out of range appears. How to fix that?
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try:

VBA Code:
Windows("Report002_" & yr & "Q" & (quarter - 1)).Activate
 
Upvote 0
Solution found. Save it in Modules instead of worksheet.
 
Upvote 0
Solution
I understand why you would put code into a module as opposed to a sheet.
I was keen to understand why when you did that, it fixed your subscript out of range problem?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,239
Members
452,621
Latest member
Laura_PinksBTHFT

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