VBAvariables工作簿参考运行时错误9

我试图用两个独立的工作簿做一个VlookUp。 主要的工作手册就像这样在开始时打开支持工作簿:

Public Sub GetFile() Dim MySourceSheet As Variant, wb As Workbook 'Opens window to choose file from MySourceSheet = Application.GetOpenFilename(FileFilter:= _ "Excel Files (*.XLS; *.XLSX), *.XLS; *.XLSX", Title:="Select file to retrieve data from.") If MySourceSheet = False Then Exit Sub Set wb = Workbooks.Open(MySourceSheet) End Sub 

在整个模块中,我引用文档几次,然后有这个部分是发生错误。

  Private Sub VlookupToBeReviewed() Dim LastColumn As Long Workbooks("09_PR Status Custodians.xlsm").Activate 'change name 'inserts new column With Sheets("Prio_Custodians") LastColumn = Cells(2, .Columns.Count).End(xlToLeft).Column 'change row! 'defines last column For i = LastColumn To LastColumn Step -2 'moves two columns to the left .Columns(i).Insert .Columns(i).ColumnWidth = 10 Next End With 'puts in the date Cells(3, LastColumn).Value = Date 'sets up the variables for the vlookup Dim rw As Long, col As Range Dim twb As Workbook 'sets up the workbooks Set twb = ThisWorkbook Set col = Workbooks(MySourceSheet).Sheets("Documents to be reviewed").Range("A:B") 'vlookup function With twb.Sheets("Sheet1") For rw = 3 To .Cells(Rows.Count, 1).End(xlUp).Row .Cells(rw, LastColumn) = Application.VLookup(.Cells(rw, 2).Value2, col, 2, False) Next rw End With End Sub 

错误:

 Set col = Workbooks(MySourceSheet).Sheets("Documents to be reviewed").Range("A:B") 

吐出来

运行时错误“9”(下标超出范围)

但是,当我将我的论点移动到MySourceSheet它将显示模块中前面定义的正确的工作簿。 如果我手动使用.xls扩展名工作簿的名称,它的工作完全罚款。我试图寻找一个答案遍布networking,但没有什么可以完全解决它。 我非常感谢一些帮助! 🙂