excel 尝试使用数组时,下标超出范围

kmbjn2e3  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(110)

请你能帮助-我目前得到一个下标超出范围的错误时,vba得到“tb.sheets(shName)。复制”,我不能弄清楚为什么-任何支持将不胜感激!谢谢你

Sub Create_Consolidated_File()

Dim tB As Excel.Workbook
Dim wB As Excel.Workbook
Dim ExportArray As Variant
Dim ShName As Variant
Dim ExportName As String

Dim varResult As Variant

Dim Attachments As Range
Set Attachments = Range("D3:E28")


For Each Row In Attachments
    For Each cell In Row

Set tB = ThisWorkbook
ExportArray = Array(cell.Value) 'work on this bit to define as the sheet names in columns D & E

For Each ShName In ExportArray
    tB.Sheets(ShName).Copy
omtl5h9j

omtl5h9j1#

如果工作表名称为数字,则使用CStr将其转换为字符串。我还添加了一些语法来完成您提供的代码。

Sub Create_Consolidated_File()
    
    Dim tB As Excel.Workbook
    Dim wB As Excel.Workbook
    Dim ExportArray As Variant
    Dim ShName As Variant
    Dim ExportName As String
    
    Dim varResult As Variant
    
    Dim Attachments As Range
    Set Attachments = Range("D3:E28")
    
    
    
    For Each Row In Attachments
        For Each cell In Row
    
        Set tB = ThisWorkbook
        ExportArray = Array(cell.Value) 'work on this bit to define as the sheet names in columns D & E
    
        For Each ShName In ExportArray
            Debug.Print (ShName)
            tB.Sheets(CStr(ShName)).Copy
        Next
    Next
    Next
    
    End Sub

相关问题