【问题标题】:merge multiple worksheets into one将多个工作表合并为一个
【发布时间】:2012-12-30 23:19:13
【问题描述】:

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

【问题讨论】:

  • 将多个工作表合并为一个已在 SO 中多次介绍。你搜索过吗?我建议搜索 SO,然后自己尝试,然后发布您尝试的代码以及错误消息(即如果有的话),然后我们将从那里获取它? :)

标签: excel vba


【解决方案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

【讨论】:

    【解决方案2】:

    这是一个简化的例子:

    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 - 在 SO HERE IS A SEARCH FOR YOU ... CHECK OUT WHAT IS IN THE BOX IN THE TOP RIGHT OF THE SCREEN 上有大量参考资料供您参考

    【讨论】:

      猜你喜欢
      • 2019-07-26
      • 1970-01-01
      • 2022-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多