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

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
1,459
Office Version
  1. 2013
  2. 2007
Platform
  1. Windows
Try:

VBA Code:
Windows("Report002_" & yr & "Q" & (quarter - 1)).Activate
 

sharonng

New Member
Joined
Jul 14, 2021
Messages
18
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. Web
Solution found. Save it in Modules instead of worksheet.
 
Solution

welshgasman

Well-known Member
Joined
May 25, 2013
Messages
909
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?
 

Forum statistics

Threads
1,144,162
Messages
5,722,847
Members
422,460
Latest member
VBA_Noob01

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
Top