0

我正在尝试将多个工作表合并到一个摘要表中。
每个 Worksheet 的名称为“Table #number”,例如Table 1等。每个工作表Table 2的布局都是相同的。数据范围是 columns A1 : N13
此功能不起作用:=SUM('Table 1':'Table 25'!$A$1:$N$13)
我如何使用VBA来合并这些数据?

在此处输入图像描述

4

2 回答 2

1
Sub MergeSheet()

'Declaring the Variables
Dim LastRow, ShtCnt As Integer
Dim ShtName As String
Dim NewSht As Worksheet

'Assinging a Sheet Name by UserInput
ShtName:
ShtName = InputBox("Enter the Sheet Name you want to create", "Merge Sheet", "Master Sheet")

'Count of Total Worksheet in the present workbook
ShtCnt = Sheets.Count

'Using For Loop check if the worksheet exists
For i = 1 To ShtCnt
If Sheets(i).Name = ShtName Then
MsgBox "Sheet already Exists", , "Merge Sheet"
GoTo ShtName
End If
Next i

'Create a New Sheet
Worksheets.Add.Name = ShtName

'Assigning NewSht as Current Sheet
Set NewSht = ActiveSheet

'Moving Worksheet to the beginning of this workbook
NewSht.Move before:=Worksheets(1)

'Copying all the data to the New Sheet Using For Loop
For i = 2 To ShtCnt + 1

'If i=2 Then copy all the data from the second sheet including header.
If i = 2 Then
Sheets(i).UsedRange.Copy NewSht.Cells(1, 1)
Else

'If i is grater than 2 then copy all the data excluding Header(1st Row).
Sheets(i).UsedRange.Offset(1, 0).Resize(Sheets(i).UsedRange.Rows.Count - 1, Sheets(i).UsedRange.Columns.Count).Copy NewSht.Cells(LastRow + 1, 1)
End If
LastRow = NewSht.Cells.SpecialCells(xlCellTypeLastCell).Row
Next i

'Displaying the Message after copying data successfully
MsgBox "Data has been copied to " & ShtName, , "Merge Sheet"

End Sub
于 2018-01-29T13:06:38.843 回答
0

这是一个简化的例子:

Option Explicit

Sub amalgamateData()

'initialise result variable
Dim myResult As Double
myResult = 0

'loop through sheets to get the sum
Dim wks As Excel.Worksheet  'loop control variable
For Each wks In Excel.ThisWorkbook.Worksheets
    If Left(wks.Name, 5) = "Table" Then ' only the "Table" sheets
        With wks
            Dim rngTarget As Range
            myResult = myResult + Excel.Application.WorksheetFunction.Sum(.Range("A1:N13"))
        End With
    End If
Next

'add result to sheet "Result"
Excel.ThisWorkbook.Sheets("Result").Range("A1") = myResult

End Sub

我的出发点是这个SO Post:how-to-merge-data-from-multiple-sheets

正如 Siddharth saud - 有大量的参考资料供您参考,所以这里是为您搜索...查看屏幕右上角框中的内容

于 2012-12-31T11:57:39.660 回答