Vinod Patil
Vinod Patil

Reputation: 1

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

Upvotes: 0

Views: 249

Answers (1)

Gary's Student
Gary's Student

Reputation: 96753

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.

Upvotes: 1

Related Questions