Excel:跨多个工作表填充数据

时间:2013-04-10 16:42:26

标签: excel excel-vba excel-formula vba

不幸的是,对于我的雇主,我的网络工程课程都没有包括高级Excel公式编程。毋庸置疑,我对基本的SUM和COUNT公式命令的Excel保存一无所知。

我的雇主有一个Excel工作簿,其中包含多个工作表,代表日历年的每个月。我们希望能够在工作簿中有一个“总”工作表,它反映了每个列/行中整个工作簿中的所有数据。

为清晰起见的一个例子:

  • 在工作表“May_2013”​​中,A列标记为“DATE”。单元格A2包含数据“MAY-1”。

  • 在工作表“June_2013”​​中,A列标记为“DATE”。单元格A2包含数据“JUNE-1”。

  • 在工作表“总计”中,A列标记为“DATE”。我们希望单元格A2反映“MAY-1”,A3则反映“JUNE-1”。

我们希望对所有工作表,A-Q列,第2-33行执行此操作,并在最后填充主表,其中包含相应列中所有工作表中的所有数据。

这可能吗?

3 个答案:

答案 0 :(得分:2)

这是两个VBA解决方案。第一个做到了这一点:

  1. 检查是否存在“总计”表。如果不是
  2. 则创建它
  3. 将第一张图纸的第一行(A到Q)复制到“总计”
  4. 将块A2:Q33复制到从第2行开始的“总计”表
  5. 对所有其他工作表重复,每次下降32行
  6. 第二个显示如何在复制之前对列数据进行一些操作:对于每个列,它应用WorksheetFunction.Sum(),但您可以将其替换为您要使用的任何其他聚合函数。然后它将结果(每张一行)复制到“总计”表。

    这两种解决方案都在您可以下载的工作簿中from this site。使用,运行宏,并从显示的选项列表中选择适当的宏。您可以通过使用。

    调用VBA编辑器来编辑代码
    Sub aggregateRaw()
    Dim thisSheet, newSheet As Worksheet
    Dim sheetCount As Integer
    Dim targetRange As Range
    
    sheetCount = ActiveWorkbook.Sheets.Count
    
    ' add a new sheet at the end:
    If Not worksheetExists("totals") Then
      Set newSheet = ActiveWorkbook.Sheets.Add(after:=Sheets(sheetCount))
      newSheet.Name = "totals"
    Else
      Set newSheet = ActiveWorkbook.Sheets("totals")
    End If
    
    Set targetRange = newSheet.[A1]
    
    ' if you want to clear the sheet before copying data, uncomment this line:
    ' newSheet.UsedRange.Delete
    
    ' assuming you want to copy the headers, and that they are the same
    ' on all sheets, you can copy them to the "totals" sheet like this:
    ActiveWorkbook.Sheets(1).Range("1:1").Copy targetRange
    
    Set targetRange = targetRange.Offset(1, 0) ' down a row
    ' copy blocks of data from A2 to Q33 into the "totals" sheet
    For Each ws In ActiveWorkbook.Worksheets
      If ws.Name <> newSheet.Name Then
        ws.Range("A2", "Q33").Copy targetRange
        Set targetRange = targetRange.Offset(32, 0) ' down 32 rows
      End If
    Next ws
    
    End Sub
    
    Sub aggregateTotal()
    Dim thisSheet, newSheet As Worksheet
    Dim sheetCount As Integer
    Dim targetRange As Range
    Dim columnToSum As Range
    
    sheetCount = ActiveWorkbook.Sheets.Count
    
    ' add a new sheet at the end:
    If Not worksheetExists("totals") Then
      Set newSheet = ActiveWorkbook.Sheets.Add(after:=Sheets(sheetCount))
      newSheet.Name = "totals"
    Else
      Set newSheet = Sheets("totals")
    End If
    
    ' assuming you want to copy the headers, and that they are the same
    ' on all sheets, you can copy them to the "totals" sheet like this:
    Set targetRange = newSheet.[A1]
    ActiveWorkbook.Sheets(1).Range("A1:Q1").Copy targetRange
    
    Set targetRange = targetRange.Offset(1, 0) ' down a row
    
    For Each ws In ActiveWorkbook.Worksheets
      ' don't copy data from "total" sheet to "total" sheet...
      If ws.Name <> newSheet.Name Then
        ' copy the month label
        ws.[A2].Copy targetRange
    
        ' get the sum of the coluns:
        Set columnToSum = ws.[B2:B33]
        For colNum = 2 To 17 ' B to Q
          targetRange.Offset(0, colNum - 1).Value = WorksheetFunction.Sum(columnToSum.Offset(0, colNum - 2))
        Next colNum
        Set targetRange = targetRange.Offset(1, 0) ' next row in output
      End If
    
    Next ws
    
    End Sub
    
    Function worksheetExists(wsName)
    ' adapted from http://www.mrexcel.com/forum/excel-questions/3228-visual-basic-applications-check-if-worksheet-exists.html
    worksheetExists = False
    On Error Resume Next
    worksheetExists = (Sheets(wsName).Name <> "")
    On Error GoTo 0
    End Function
    

    最终(?)编辑 如果希望每次有人对工作簿进行更改时都自动运行此脚本,则可以通过向工作簿添加代码来捕获SheetChange事件。您可以按如下方式执行此操作:

    1. 打开Visual Basic编辑器()
    2. 在项目浏览器(屏幕左侧)中,展开VBAProject
    3. 右键单击“ThisWorkbook”,然后选择“查看代码”
    4. 在打开的窗口中,复制/粘贴以下代码行:
    5. Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) ' handle errors gracefully: On Error GoTo errorHandler ' turn off screen updating - no annoying "flashing" Application.ScreenUpdating = False ' don't respond to events while we are updating: Application.EnableEvents = False ' run the same sub as before: aggregateRaw ' turn screen updating on again: Application.ScreenUpdating = True ' turn event handling on again: Application.EnableEvents = True Exit Sub ' if we encountered no errors, we are now done. errorHandler: Application.EnableEvents = True Application.ScreenUpdating = True ' you could add other code here... for example by uncommenting the next two lines ' MsgBox "Something is wrong ... " & Err.Description ' Err.Clear End Sub

答案 1 :(得分:0)

请使用RDBMerge加载项,它将组合来自不同工作表的数据并为您创建主表。有关详细信息,请参阅以下链接。

http://duggisjobstechnicalstuff.blogspot.in/2013/03/how-to-merge-all-excel-worksheets-with.html

Download RDBMerge

答案 2 :(得分:0)

您可以使用间接功能来引用工作表名称。在下图中,此功能采用标题名称(B37)并将其用作图纸参考。你所要做的就是选择正确的“总细胞”,我在“MAY_2013”​​中制作了“A1”。我在下面放了一个图像,显示我的参考名称和标签名称

Formula