首页 > 解决方案 > 在不同数量的工作表中对同一个表求和

问题描述

所以我有一个 excel 文件,我可以在其中输入我想要分析的不同项目和文件的位置。然后是一个代码去获取文件并为每个输入的项目生成 2 张工作表(从我创建的模板)并填充数据。这些项目可以改变名称和数量。

当我尝试做一个总表时,我的问题出现了。我会去哪里从不同工作表中的同一个单元格中获取值并将它们求和。工作表的数量可以改变,所以我没有设法使用 sheet.count,但与此操作相关的工作表的名称都以“Total_”开头。

所以到目前为止我的代码的开头是:

`Sub refresh()

 Parametre

    Dim nbOnglet As Integer
    Dim nbProjet As Integer
    
    Dim name As String
    Dim nametot As String
    Dim A As String
    Dim B As String
            
    Dim idx As Integer
    Dim iDebut As Integer
    
    Dim values As Variant
    Dim rng As Range
    
    Dim xRng As Range
    Dim x As Long
    Dim vArray As Variant
    Dim dSum As Double
  
 Initialisation
    iDebut = 9

 Déterminer le nombre d'onglets du Classeur
    nbOnglet = Sheets.Count

 Déterminer le nombre de projet à traiter
    folderpath = Range("C3").Value
    Sheets("Sommaire").Select
    nbLigne = Cells(10, "A").Value

x = 0

For idx = 1 To nbLigne
     activate Récapitulatif
        Sheets("Récapitulatif").Select
        
     Define the variable name - tab name
        A = "Total_"
        B = Sheets("Sommaire").Cells(iDebut + idx, "D").Value
        name = B
        nametot = A & B`

然后对于太阳,我尝试了不同的选择,但似乎没有一个适用于整个桌子。通过使用以下方法,我设法为一个单元格获得了良好的结果:

x = x + sheets(nametot).range("F7").Value2

但无法在所有范围内都做到这一点(F7:CI31)。

我尝试过的其他公式是:

Set xRng = ThisWorkbook.Sheets(nbLigne).Range("K7:CI31")
xRng.FormulaR1C1 = "=SUM('" & ThisWorkbook.Sheets(nametot).name & "'!RC+'" & ThisWorkbook.Sheets(nametot).name & "'!RC)"

虽然这给出了我想要的方程,因为它在循环中运行,但它为每张纸计算相同的值并在最后一个识别出的纸上停止......所以不要做我想做的事:在不同的纸上求和相同的单元格命名为“Total_XXXX”并在“Récapitulatif”表中显示该值。

我一直在寻找互联网,我真的无法找到一种方法来做到这一点。你有什么想法?非常感谢你提前

表格示例

标签: excelvbaformula

解决方案


合并工作表

链接 (Microsoft Docs)

描述

  • 在包含此代码(ThisWorkbook_ ( )。xlSumsrcRangesrcLeadtgtNametgtFirst

编码

Option Explicit

Sub consolidateWorksheets()
    
    ' Define constants.
    Const srcRange As String = "K7:CI31"
    Const srcLead As String = "Total_"
    Const tgtName As String = "Récapitulatif"
    Const tgtFirst As String = "A1"
    
    ' Define workbook.
    Dim wb As Workbook
    Set wb = ThisWorkbook
    
    ' Define Target Worksheet.
    Dim tgt As Worksheet
    Set tgt = wb.Worksheets(tgtName)
    
    ' Define R1C1-Style Source Ranges Address.
    Dim rcRng As String
    rcRng = tgt.Range(srcRange).Address(ReferenceStyle:=xlR1C1)
    
    ' Define Consolidation Array.
    Dim Data As Variant
    ReDim Data(1 To wb.Worksheets.Count)
    
    ' Declare variables.
    Dim ws As Worksheet         ' Current Source Worksheet
    Dim CurrentName As String   ' Current Source Worksheet Name
    Dim n As Long               ' Current Element in Consolidation Array
    
    ' Write full paths of Source Worksheets to Consolidation Array.
    For Each ws In wb.Worksheets
        CurrentName = ws.Name
        If InStr(1, CurrentName, srcLead, vbTextCompare) = 1 Then
            n = n + 1
            Data(n) = "'" & ws.Name & "'!" & rcRng
        End If
    Next ws
    
    ' Validate and resize Consolidation Array.
    If n = 0 Then
        MsgBox "No worksheets to consolidate.", vbCritical, "Fail"
        Exit Sub
    End If
    ReDim Preserve Data(1 To n)
    
    ' Consolidate.
    tgt.Range(tgtFirst).Consolidate Sources:=Data, _
                                    Function:=xlSum
    
    ' Inform user.
    MsgBox "Data consolidated.", vbInformation, "Success"

End Sub

推荐阅读