简体   繁体   English

如何从另一个工作簿引用源数据 - 下标超出范围

[英]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

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然后desiredSheet是对用户选择的工作表的引用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将数据从工作簿复制到另一工作簿时,下标超出范围错误 - Subscript out of range error when copying data from a workbook to another 从变量引用另一个工作簿中的工作表时,下标超出范围 - Subscript out of range when referencing a worksheet in another workbook from a variable 将范围复制到另一个工作簿时下标超出范围 - Subscript out of range when copying range to another workbook 将单元格从工作簿复制到工作簿将导致“下标超出范围” - Copying Cells from Workbook to Workbook Results in “Subscript Out of Range” 按路径和名称错误引用工作簿:下标超出范围 - Reference workbook by path and name error: Subscript out of range 使用用户表单输入工作簿作为参考时下标超出范围 - Subscript out of range when using Userform input workbook as reference 打开工作簿下标超出范围 - Open Workbook subscript out of range 将数据从一个Excel文件复制到另一个-下标超出范围 - Copying data from one excel file to another - Subscript out of range VBA错误:运行时错误:9-从另一个工作簿复制工作表时,下标超出范围 - VBA Error: Runtime Error: 9 - Subscript out of range when copying a worksheet from another workbook 从一个工作簿粘贴到另一个工作簿时,Excel VBA:错误9(下标超出范围) - Excel VBA: Error 9 (Subscript out of Range) when pasting from one workbook to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM