MS Excel"下标超出范围"将工作表复制到新工作簿时

时间:2016-06-07 06:33:56

标签: excel vba worksheet office-2013

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

用户在其中一个工作表中输入数据,然后单击运行VBA代码的按钮。根据用户输入数据的条件,我需要创建一个包含某些工作表副本的新工作簿。

然后代码通过源工作簿中的工作表进行迭代(循环),并将相关工作表的名称添加到字符串中......

Dim worksheetsToCopy As String

worksheetsToCopy = worksheetsToCopy & """" & "Admin Tab" & """" & ", " 

循环完成后,我检查变量     Debug.Print工作表托盘 它在立即窗口中给出了以下输出: "管理员标签","主页标签","信息中心",

然后我去掉了最后一个","与

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

然后,当我尝试使用以下代码行复制变量worksheetsToCopy中包含的工作表时,我得到运行时错误9 - 下标超出范围:

Sheets(Array(worksheetsToCopy)).Copy 

但是,当我运行相同的指令但手动输入引号中的选项卡名称并使用逗号如下所示时,它可以正常工作:

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

为什么我得到运行时错误9 - "下标超出范围"第一个语句出错。

我做错了什么或错过了什么? 任何帮助将不胜感激......

2 个答案:

答案 0 :(得分:0)

看起来您正在创建双引号字符串。尝试创建一个名称用逗号分隔的普通字符串,并将其拆分为实际数组,例如:
"管理员标签,主页标签,信息中心"
获得字符串后,删除最后一个逗号并将其用于表格数组副本:

'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

答案 1 :(得分:0)

以下是本主题中描述的错误的解决方案。

The worksheet in MS Excel 用户单击"创建新工作簿"时执行的代码按钮:

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