使用选项卡名称的变量访问另一个工作簿时,下标超出范围

时间:2013-09-23 11:13:06

标签: vb.net excel-vba vba excel

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

我需要打开外部工作簿并选择特定的工作表名称 当我对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

0 个答案:

没有答案