任何人都可以帮我解决在使用变量名时尝试访问外部工作簿/工作表时遇到的问题。
我需要打开外部工作簿并选择特定的工作表名称 当我对Tabname进行编码时它会起作用,但是当我使用变量时,我会得到一个超出范围的下标错误。
我已经包含了下面的子项,我用
调用例程 GetDatafromWB FullPandF, strDataImport, "ImportData"
Sub GetDatafromWB(OtherWB As String, TabName As String, strUser As String)
Dim customBook As Workbook
Dim customFilename As String
Dim customWorkbook As Workbook
Dim targetWorkbook As Workbook
Dim targetSheet As Worksheet
Dim sourceSheet As Worksheet
' Tabname in this instance is "Aug 2013"
strImpSheet = TabName
strUserSheet = strUser
' set active workbook as the target
Set targetWorkbook = Application.ActiveWorkbook
Set targetSheet = targetWorkbook.Worksheets(strUserSheet)
' get the custom workbook
customFilename = OtherWB
Set customWorkbook = Application.Workbooks.Open(customFilename)
' ****************************** This needs to be changed to a variable ****************************
' ************************************ Currently Hardcoded *****************************************
Set sourceSheet = customWorkbook.Worksheets("Aug 2013")
' Set sourceSheet = customWorkbook.Worksheets(strImpSheet) generates a subscript out of range error
targetSheet.Range("A3", "I400").Value = sourceSheet.Range("A3", "I400").Value
' Close customer workbook
customWorkbook.Close
End Sub