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'源引用与目标区域重叠时,我现在已将其替换,我不确定我在哪里做错了什么。请协助。
答案 0 :(得分: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
答案 1 :(得分: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