当复制工作表到新的工作簿MS Excel的“下标超出范围”

我有一个包含许多工作表的MS Excel工作簿。

用户在其中一个工作表中input数据,然后单击一个运行VBA代码的button。 根据用户input的数据导出的条件,我需要创build一个新的工作簿,其中包含一些工作表的副本。

然后代码遍历(循环)源工作簿中的工作表,并将相关工作表的名称添加到string中。

Dim worksheetsToCopy As String worksheetsToCopy = worksheetsToCopy & """" & "Admin Tab" & """" & ", " 

完成循环后,我用Debug.Print worksheetsToCopy检查variables,它在立即窗口中给我以下输出:“pipe理选项卡”,“主页选项卡”,“仪表板”

然后我把最后一个“,”带出去

 worksheetsToCopy = Mid(worksheetsToCopy, 1, InStrRev(worksheetsToCopy, ",") - 1) 

然后,当我尝试复制variablesworksheetsToCopy中包含的工作表与下面的代码行时,我得到运行时错误9 – 下标超出范围:

 Sheets(Array(worksheetsToCopy)).Copy 

但是,当我运行相同的指令,但手动input标签名称用引号和逗号如下,它工作正常:

 Sheets(Array("Admin Tab", "Home Tab", "Dashboard")).Copy 

为什么在第一条语句中出现运行时错误9 – “下标超出范围”错误。

我做错了什么或失踪? 任何帮助将不胜感激…

这里是解决主题中描述的错误。

MS Excel中的工作表用户单击“创build新工作簿”button时执行的代码:

 Option Explicit Sub ButtonCreateWorkbook_Click() ' ' ButtonCreateClientWorkbookk_Click Macro ' This Macro creates a new workbook containing specific worksheets based on the selection ' that are set in the "Control" worksheet of the Workbook ' Dim strWorksheetsToCopy As String Dim wbSourceWorkbook As Workbook Dim strControlSheet As String Dim iRowStart As Integer Dim iRowEnd As Integer Dim strWorksheetColumn As String Dim strSwitchColumn As String Dim iRow As Integer Dim strSheetArray() As String Set wbSourceWorkbook = Application.ActiveWorkbook strControlSheet = "Control" wbSourceWorkbook.Sheets(strControlSheet).Activate strWorksheetColumn = "B" 'this variable indicates which iRow contains the Worksheet names iRowStart = 2 'this value must set to the iRow number where the FIRST Worksheet Name occurs in Column A 'this value must be set to the iRow number where the LAST Worksheet Name occurs in Column B iRowEnd = Cells(Rows.Count, 2).End(xlUp).Row strSwitchColumn = "C" 'this column must be set to the value of the "On/Off Switch" column on the "Control_ShowHide" worksheet. 'This section of code controls which worksheets must be copied and add the name of the worksheet to the string For iRow = iRowStart To iRowEnd 'Check if the worksheet must be copied If wbSourceWorkbook.Sheets(strControlSheet).Range(strSwitchColumn & iRow).Value > 0 Then strWorksheetsToCopy = strWorksheetsToCopy & wbSourceWorkbook.Sheets(strControlSheet).Range(strWorksheetColumn & iRow).Value & "," End If Next iRow Debug.Print strWorksheetsToCopy 'Remove the last , from the string strWorksheetsToCopy = Mid(strWorksheetsToCopy, 1, InStrRev(strWorksheetsToCopy, ",") - 1) 'Transpose the String to a string array strSheetArray = Split(strWorksheetsToCopy, ",") 'Create the new Workbook with the relevant sheets wbSourceWorkbook.Sheets(strSheetArray).Copy End Sub 

看起来你正在创build一个双引号的string。 尝试创build一个名称以逗号分隔的普通string,并将其分割为实际的数组,例如:
“pipe理员选项卡,主页选项卡,仪表板”
一旦你得到你的string,删除最后的逗号,并像这样使用它为张数组副本:

 'Add a string array variable Dim sheetArray() As String 'Remove the last , from the string of Worksheets to copy worksheetsToCopy = Mid(worksheetsToCopy, 1, InStrRev(worksheetsToCopy, ",") - 1) 'Create the array from your string sheetArray = Split(worksheetsToCopy, ",") 'And copy the sheets Sheets(sheetArray).Copy