对不同数量的工作表求和相同的 table
Sum the same table across different number of worksheets
所以我有一个 excel 文件,我可以在其中输入要分析的不同项目和文件的位置。然后一个代码去获取文件并为每个输入的项目生成 2 sheets(从我创建的模板)并填充数据。这些项目可以更改名称和数量。
当我尝试总共 table 时,我的问题出现了。我会去哪里从不同 sheets 中的同一个单元格中获取值并将它们相加。 sheet 的数量可以改变,所以我没有设法使用 sheets.count,但是与此操作相关的 sheet 的名称都以“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`
然后对于太阳,我尝试了不同的选项,但 none 似乎对整个 table 都有效。我设法通过使用以下方法获得了一个单元格的好结果:
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)"
虽然这给出了我想要的方程式,因为它是 运行 在一个循环中,它对每个 sheet 计算相同,并在最后一个确定的地方停止...所以不做我想:对名为 'Total_XXXX' 的不同 sheet 中的同一个单元格求和,并在 'Récapitulatif' sheet.
中显示该值
我一直在网上四处寻找,但我真的想不出办法。
你有什么想法?
非常感谢你
example of the table
合并工作表
链接(Microsoft 文档)
描述
- 在包含此代码 (
ThisWorkbook
) 的工作簿中,以下将合并(在本例中为总和 (xlSum
))所有相同范围 (srcRange
)将名称以指定字符串 (srcLead
) 开头的工作表导入另一个工作表 (tgtName
),从指定单元格 (tgtFirst
) 开始。
代码
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
所以我有一个 excel 文件,我可以在其中输入要分析的不同项目和文件的位置。然后一个代码去获取文件并为每个输入的项目生成 2 sheets(从我创建的模板)并填充数据。这些项目可以更改名称和数量。
当我尝试总共 table 时,我的问题出现了。我会去哪里从不同 sheets 中的同一个单元格中获取值并将它们相加。 sheet 的数量可以改变,所以我没有设法使用 sheets.count,但是与此操作相关的 sheet 的名称都以“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`
然后对于太阳,我尝试了不同的选项,但 none 似乎对整个 table 都有效。我设法通过使用以下方法获得了一个单元格的好结果:
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)"
虽然这给出了我想要的方程式,因为它是 运行 在一个循环中,它对每个 sheet 计算相同,并在最后一个确定的地方停止...所以不做我想:对名为 'Total_XXXX' 的不同 sheet 中的同一个单元格求和,并在 'Récapitulatif' sheet.
中显示该值我一直在网上四处寻找,但我真的想不出办法。 你有什么想法? 非常感谢你
example of the table
合并工作表
链接(Microsoft 文档)
描述
- 在包含此代码 (
ThisWorkbook
) 的工作簿中,以下将合并(在本例中为总和 (xlSum
))所有相同范围 (srcRange
)将名称以指定字符串 (srcLead
) 开头的工作表导入另一个工作表 (tgtName
),从指定单元格 (tgtFirst
) 开始。
代码
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