同一工作表中的表格[关闭]

问题描述 投票:-1回答:2
    Option Explicit

    Sub Macro70()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim sheets_Count As Integer
    Dim sheets_Name() As String
    Dim i As Integer
    sheets_Count = Sheets.Count

    ReDim sheets_Name(0 To sheets_Count - 1)

    For i = 1 To sheets_Count
       sheets_Name(i - 1) = "'" & ActiveWorkbook.Sheets(i).Name & "'!R1C1:R17C2"
    Next i

    Set wb = ThisWorkbook
    For Each ws2 In wb.Sheets
If ws2.Name = "consolidated" Then Exit For
Next

    If ws2 Is Nothing Then
    Set ws2 = wb.Sheets.Add()
    ws2.Name = "consolidated"
    End If

    With ws2
        .Range("A1").Consolidate sheets_Name, xlSum, True, True, False
    End With

    End Sub


    Sub Macro71()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim sheets_Count As Integer
    Dim sheets_Name() As String
    Dim i As Integer
    sheets_Count = Sheets.Count

    ReDim sheets_Name(0 To sheets_Count - 1)

    For i = 1 To sheets_Count
       sheets_Name(i - 1) = "'" & ActiveWorkbook.Sheets(i).Name & "'!R24C1:R35C2"
    Next i

    Set wb = ThisWorkbook
    For Each ws2 In wb.Sheets
If ws2.Name = "consolidated" Then Exit For
Next

    If ws2 Is Nothing Then
    Set ws2 = wb.Sheets.Add()
    ws2.Name = "consolidated"
    End If
With ws2
        .Range("A24").Consolidate sheets_Name, xlSum, True, True, False
    End With

    End Sub


    Sub Macro72()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim ws2 As Worksheet
    Dim sheets_Count As Integer
    Dim sheets_Name() As String
    Dim i As Integer
    sheets_Count = Sheets.Count

    ReDim sheets_Name(0 To sheets_Count - 1)

    For i = 1 To sheets_Count
       sheets_Name(i - 1) = "'" & ActiveWorkbook.Sheets(i).Name & "'!R39C1:R50C2"
    Next i

    Set wb = ThisWorkbook
   For Each ws2 In wb.Sheets
If ws2.Name = "consolidated" Then Exit For
Next

    If ws2 Is Nothing Then
    Set ws2 = wb.Sheets.Add()
    ws2.Name = "consolidated"
    End If
With ws2
        .Range("A39").Consolidate sheets_Name, xlSum, True, True, False
    End With

    End Sub     

我已经替换它现在当我运行宏71和72有运行时错误'1004'源参考重叠目标区域,我不知道我在哪里做错了什么。请协助。

excel vba worksheet
2个回答
1
投票

你可以这样做:

Sub Tester()

    Const SHT_CONS As String = "consolidated"
    Dim wb As Workbook, ws As Worksheet
    Dim wsCons As Worksheet
    Dim colSheets As New Collection

    Set wb = ActiveWorkbook

    'add the consolidation sheet if missing
    On Error Resume Next
    Set wsCons = wb.Worksheets(SHT_CONS)
    On Error GoTo 0
    If wsCons Is Nothing Then
        Set wsCons = wb.Worksheets.Add(after:=wb.Sheets(wb.Sheets.Count))
        wsCons.Name = SHT_CONS
    End If

    'collect the sheet names
    For Each ws In wb.Worksheets
        If ws.Name <> SHT_CONS Then colSheets.Add ws.Name
    Next ws

    'call a sub to create each consolidation
    DoConsolidate colSheets, "R1C1:R17C2", wsCons.Range("A1")
    DoConsolidate colSheets, "R24C1:R35C2", wsCons.Range("A24")
    DoConsolidate colSheets, "R39C1:R50C2", wsCons.Range("A39")

End Sub

Sub DoConsolidate(sheetNames As Collection, rngR1C1 As String, rngDest As Range)
    Dim arr(), i, s
    ReDim arr(0 To sheetNames.Count - 1)
    i = 0
    For Each s In sheetNames
        arr(i) = "'" & s & "'!" & rngR1C1
        i = i + 1
    Next s
    rngDest.Consolidate arr, xlSum, True, True, False
End Sub

0
投票

如果我理解的是正确的,那么用每个宏中的代码替换Set ws2 = wb.Sheets.Add()

For Each ws2 In wb.Sheets
If ws2.Name = "consolidated" Then Exit For
Next

    If ws2 Is Nothing Then
    Set ws2 = wb.Sheets.Add()
    ws2.Name = "consolidated"
    End If
© www.soinside.com 2019 - 2024. All rights reserved.