我有多张纸,以跟踪每个人的抽奖券数量。
主表:
|-----------|------------------|
| Name | # of tickets |
|-----------|------------------|
| Adam | 5 |
|-----------|------------------|
| Michael | 2 |
|-----------|------------------|
| Sara | 4 |
|-----------|------------------|
工作表1:
|-----------|------------------|---------------|
| Name | Activity | # of tickets |
|-----------|------------------|---------------|
| Adam | Registration | 2 |
|-----------|------------------|---------------|
| Michael | Registration | 2 |
|-----------|------------------|---------------|
| Sara | Registration | 2 |
|-----------|------------------|---------------|
表格2:
|-----------|------------------|---------------|
| Name | Activity | # of tickets |
|-----------|------------------|---------------|
| Adam | Registration | 2 |
|-----------|------------------|---------------|
| Sara | Registration | 2 |
|-----------|------------------|---------------|
| Adam | Extra ticket | 1 |
|-----------|------------------|---------------|
问题:我该如何做才能根据工作表1,工作表2 ...工作表n自动更新主工作表?因此,基本上,主要工作表应基于“名称”列获取每个工作表的行,然后获取票证#的值并将其汇总到主要工作表中。
像这样回复您的数据:主数据表和2个数据表:
在我的MainSheet中,B1中使用的公式是:
=SUMIF(Sheet1!$A$1:$A$3;A1;Sheet1!$C$1:$C$3)+SUMIF(Sheet2!$A$1:$A$3;A1;Sheet2!$C$1:$C$3)
您要添加的每个工作表都需要一个SUMIF。
创建代码块宏vba
Sub GetInfoSheet()
Dim WS_Count As Integer
Dim I As Integer
Dim SumAdam As Integer
Dim SumMichel As Integer
Dim SumSara As Integer
WS_Count = ActiveWorkbook.Worksheets.Count
For I = 2 To WS_Count
strTest = ActiveWorkbook.Worksheets(I).Cells(3, 3)
SumAdam = SumAdam + CInt(strTest)
strTest = ActiveWorkbook.Worksheets(I).Cells(4, 3)
SumMichel = SumMichel + CInt(strTest)
strTest = ActiveWorkbook.Worksheets(I).Cells(5, 3)
SumSara = SumSara + CInt(strTest)
Next I
With ActiveWorkbook.Worksheets(1)
.Cells(3, 3).Value = SumAdam
.Cells(4, 3).Value = SumMichel
.Cells(5, 3).Value = SumSara
End With
End Sub