1

任何人都可以帮助我解决在使用变量名时尝试访问外部工作簿/工作表时遇到的问题。

我需要打开一个外部工作簿并在对它工作的 Tabname 进行硬编码时选择一个特定的工作表名称,但是当我使用变量时,我得到一个 Subscript out of range 错误。

我已经包含了下面的子程序,我用

 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
4

0 回答 0