简体   繁体   中英

VBA Error Run time error 9 Subscript Out of range

I am getting Run time error 9

Sub Conway()
Dim aRng As Range
Dim Data As Range, c As Collection
Dim v As String, i As Long, ary
Set c = New Collection
    Path = "D:\InTransit DR Report\"
    RawFile = Application.GetOpenFilename(, , "Please Select InTransit Raw WorkBook:")
    Application.DisplayAlerts = False
    If RawFile = "False" Then
        MsgBox "Please select appropriate Final Output file", vbCritical, "Nike InTransit DR Automation"
        Exit Sub
    Else
        Workbooks.Open RawFile, True, False
    Application.Wait (10)
    Application.Visible = False
    Application.Visible = True
    Application.WindowState = xlMaximized
    ActiveWindow.WindowState = xlMaximized
    End If
    Sheets("In-Transit").Select 'Getting error here

We are dealing with two workbooks:

  1. the workbook containing the macro (wb1)
  2. the workbook opened by the macro (wb2)

Once wb2 has been opened, it becomes the Active workbook and the line:

Sheets("In-Transit").Select

will only work if that sheet is in wb2 . If the sheet is in the original workbook (wb1) , that workbook must be re-activated beforre the sheet can be Selected . Also make sure that there are no extra spaces in the sheetname.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM