0

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
1
  • You need to specify a workbook. Like how you set Ws, create a variable for workbook and set that.
    – findwindow
    Commented Jul 1, 2022 at 19:07

1 Answer 1

1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.