Search code examples
excelvbareferencefilepath

How to reference source data from another workbook - subscript out of range


I have a dashboard (Sheet "Dashboard") with set of pivot tables that rely on a dynamic data set on a specific sheet of the same workbook (Sheet "Dashboard_Data"). Currently the user is prompted to select a cell on the sheet with the raw data (due to possible different sheet name), the macro then combines the necessary data on the Dashboard Data sheet and the pivot tables are updated. This works without any problems as long as the raw data is on a sheet in the same workbook.

I am trying to find a solution so that that the user can select the raw data on a sheet that is not in the same workbook. When I run it as is I am getting a subscript out of range error.

Option Explicit
Sub HB_Run_Dashboard()

Dim Ws As Worksheet, lastRow As Long
Dim myNamedRange As Range, Rng As Range, c As Range, destrange As Range
Dim myRangeName As String
Dim desiredSheetName As String
Dim SearchRow As Long
Dim StartatRow As Long
Dim pvt As PivotTable
Dim S As String

'user prompt to locate cell in the sheet with the raw data to capture sheet name

desiredSheetName = Application.InputBox("Select any cell inside the source sheet: ", _
"Prompt for selecting target sheet name", Type:=8).Worksheet.Name

'currently a test code to also have the user find the location of the sheet

'Inputsheetpath = Application.GetOpenFilename(FileFilter:= _
"Excel Workbooks (*.xls*),*.xls*,*.xlsb,*.xlsm", Title:="Open Database File")

'this is where i am stuck. how to set the WS with the correct path info to avoid subscript out of range
Set Ws = Workbook.Worksheets(desiredSheetName)

SearchRow = InputBox("Row # where Reference 'Dashboard' is entered.", "Row Input")
StartatRow = InputBox("Row # where Headers are located.", "Row Input")

'Const SearchRow As Long = SRow
'Const StartAtRow As Long = StRow
Const RangeName As String = "Dashboard_Data_Raw"

lastRow = Ws.Cells(Ws.Rows.Count, "A").End(xlUp).Row

'loop cells in row to search...
For Each c In Ws.Range(Ws.Cells(SearchRow, 1), _
                       Ws.Cells(SearchRow, Columns.Count).End(xlToLeft)).Cells
    If LCase(c.Value) = "dashboard" Then 'want this column
        'add to range
        BuildRange myNamedRange, _
            Ws.Range(Ws.Cells(StartatRow, c.Column), Ws.Cells(lastRow, c.Column))
       
    End If
Next c

Debug.Print myNamedRange.Address
Worksheets(desiredSheetName).Names.Add Name:=RangeName, RefersTo:=myNamedRange

Solution

  • Instead of:

    Dim desiredSheetName As String
    desiredSheetName = Application.InputBox("Select any cell inside the source sheet: ", _
                       "Prompt for selecting target sheet name", Type:=8).Worksheet.Name
    

    you can do this:

    Dim desiredSheet As Worksheet
    Set desiredSheet = Application.InputBox("Select any cell inside the source sheet: ", _
                       "Prompt for selecting target sheet name", Type:=8).Worksheet
    

    Then desiredSheet is a reference to the user-selected worksheet