在 excel 中列出风味名称并给出风味量的总和
Tabulate flavour names in excel and give sum of flavour amounts
我一直在到处寻找如何做到这一点,我已经尝试过 vlookup 并且我可以手动合并数据,但是我想要做的是采用一堆垂直对齐的风味名称sheets,最后 sheet 获取所有这些口味,合并它们,并给我所需的调味总量。我也试图让它自动更新,因为我更新了我需要制作的果汁量。
我会尝试做一个示例,因为我无法在此处上传 sheet。
一个 sheet 可能看起来像这样
French Vanilla 20
Pineapple 16
Sweet Cream 10
Mango 8
Coconut 4
0
Strawberry 30
NY Cheesecake 20
0
0
0
0
第二个sheet可能看起来像这样
Passion Fruit 20
Mango 10
Coconut 10
Koolada 2
0
0
Blueberry Candy 10
Banana Split 6
Strawberry 12
Mango 12
0
0
我想做的是让最后一个 sheet 提取所有这些名称和数字,将匹配的名称放在一起,然后将匹配名称中的数字相加。我不需要按字母顺序排列我只需要它自动更新,因为我在第一个 sheets.
输入不同的金额
你没有说是否还有其他 sheet 没有数据,或者数据出现在哪些列中。我假设数据总是在 A:B 列中,并且有除了 sheets.
之外,工作簿中没有其他任何内容
此代码每次都会创建一个新报告 sheet - 运行 它会崩溃两次,因为它会尝试创建第二个 sheet 称为 'Report'(因此删除报告 sheet 每次)。
可能有更好的方法,并且肯定可以改进此代码以满足您的需求。
Public Sub Report()
Dim wrkSht As Worksheet
Dim wrkSht_rpt As Worksheet
Dim lLastRow As Long
Dim lLastRow_rpt As Long
'Create a new sheet to put the report on.
Set wrkSht_rpt = Worksheets.Add
wrkSht_rpt.Name = "Report"
'Get the data from each sheet and paste into columns A:B on the report sheet.
For Each wrkSht In ThisWorkbook.Worksheets
lLastRow = 0
With wrkSht
If .Name <> wrkSht_rpt.Name Then
'If there's no data in column A this will throw an error.
On Error Resume Next
lLastRow_rpt = wrkSht_rpt.Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
lLastRow = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
On Error GoTo 0
If lLastRow > 0 Then
'There's data on the sheet, so copy it to the report sheet.
.Range(.Cells(1, 1), .Cells(lLastRow, 2)).Copy _
Destination:=wrkSht_rpt.Cells(lLastRow_rpt + 1, 1)
End If
End If
End With
Next wrkSht
lLastRow = 0
lLastRow_rpt = 0
With wrkSht_rpt
'Find last row in column A on report.
lLastRow_rpt = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
'Copy all flavours to column D.
.Range(.Cells(1, 1), .Cells(lLastRow_rpt, 1)).Copy _
Destination:=.Cells(1, 4)
'Sort the column, this will place all blanks at the bottom.
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)), Order:=xlAscending
.Sort.SetRange .Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4))
.Sort.Header = xlNo
.Sort.SortMethod = xlPinYin
.Sort.Apply
'Remove duplicates and find new last row in column D.
'The new last row is stored in a separate variable as we need to original to use
'within the SUMIF formula.
.Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)).RemoveDuplicates 1, xlNo
lLastRow = .Columns(4).Find("*", , , , xlByColumns, xlPrevious).Row
'Add a formula to add everything up, then copy paste values.
With .Range(.Cells(1, 5), .Cells(lLastRow, 5))
.FormulaR1C1 = _
"=SUMIF(R1C1:R" & lLastRow_rpt & "C1,RC4,R1C2:R" & lLastRow_rpt & "C2)"
.Copy
.PasteSpecial xlPasteValues
End With
.Range("A1:C1").EntireColumn.Delete
.Columns(1).AutoFit
End With
End Sub
我将其添加为单独的答案,因为它与我的第一个答案完全不同。
在您的工作簿中的某处创建一个包含 sheet 个姓名的列表。例如在 A1 中放置 'Norse Temple 30ML',在 A2 中放置 'Norse Temple 15ML',等等。命名这个范围 - 我在示例中将其命名为 SheetList。
创建这个公式:
=SUMPRODUCT(SUMIF(INDIRECT("'" & SheetList & "'!B:B"),"Nicotine 100mg",INDIRECT("'" & SheetList & "'!C:C")))
'Nicotine 100mg' 可以替换为单元格引用,以便您可以向下拖动。
此方法需要您在报告页面上手动创建成分列表,但系统会为您完成添加。它也会在您更新数字时更新 - VBA 方法需要手动执行代码。
如果你想要一个 VBA 解决方案,我可以创建一个 - 不过需要找时间。
Link 到我找到解决方案的地方:http://www.mrexcel.com/forum/excel-questions/119020-sumif-multiple-sheets.html
我一直在到处寻找如何做到这一点,我已经尝试过 vlookup 并且我可以手动合并数据,但是我想要做的是采用一堆垂直对齐的风味名称sheets,最后 sheet 获取所有这些口味,合并它们,并给我所需的调味总量。我也试图让它自动更新,因为我更新了我需要制作的果汁量。
我会尝试做一个示例,因为我无法在此处上传 sheet。
一个 sheet 可能看起来像这样
French Vanilla 20
Pineapple 16
Sweet Cream 10
Mango 8
Coconut 4
0
Strawberry 30
NY Cheesecake 20
0
0
0
0
第二个sheet可能看起来像这样
Passion Fruit 20
Mango 10
Coconut 10
Koolada 2
0
0
Blueberry Candy 10
Banana Split 6
Strawberry 12
Mango 12
0
0
我想做的是让最后一个 sheet 提取所有这些名称和数字,将匹配的名称放在一起,然后将匹配名称中的数字相加。我不需要按字母顺序排列我只需要它自动更新,因为我在第一个 sheets.
输入不同的金额你没有说是否还有其他 sheet 没有数据,或者数据出现在哪些列中。我假设数据总是在 A:B 列中,并且有除了 sheets.
之外,工作簿中没有其他任何内容
此代码每次都会创建一个新报告 sheet - 运行 它会崩溃两次,因为它会尝试创建第二个 sheet 称为 'Report'(因此删除报告 sheet 每次)。
可能有更好的方法,并且肯定可以改进此代码以满足您的需求。
Public Sub Report()
Dim wrkSht As Worksheet
Dim wrkSht_rpt As Worksheet
Dim lLastRow As Long
Dim lLastRow_rpt As Long
'Create a new sheet to put the report on.
Set wrkSht_rpt = Worksheets.Add
wrkSht_rpt.Name = "Report"
'Get the data from each sheet and paste into columns A:B on the report sheet.
For Each wrkSht In ThisWorkbook.Worksheets
lLastRow = 0
With wrkSht
If .Name <> wrkSht_rpt.Name Then
'If there's no data in column A this will throw an error.
On Error Resume Next
lLastRow_rpt = wrkSht_rpt.Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
lLastRow = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
On Error GoTo 0
If lLastRow > 0 Then
'There's data on the sheet, so copy it to the report sheet.
.Range(.Cells(1, 1), .Cells(lLastRow, 2)).Copy _
Destination:=wrkSht_rpt.Cells(lLastRow_rpt + 1, 1)
End If
End If
End With
Next wrkSht
lLastRow = 0
lLastRow_rpt = 0
With wrkSht_rpt
'Find last row in column A on report.
lLastRow_rpt = .Columns(1).Find("*", , , , xlByColumns, xlPrevious).Row
'Copy all flavours to column D.
.Range(.Cells(1, 1), .Cells(lLastRow_rpt, 1)).Copy _
Destination:=.Cells(1, 4)
'Sort the column, this will place all blanks at the bottom.
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)), Order:=xlAscending
.Sort.SetRange .Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4))
.Sort.Header = xlNo
.Sort.SortMethod = xlPinYin
.Sort.Apply
'Remove duplicates and find new last row in column D.
'The new last row is stored in a separate variable as we need to original to use
'within the SUMIF formula.
.Range(.Cells(1, 4), .Cells(lLastRow_rpt, 4)).RemoveDuplicates 1, xlNo
lLastRow = .Columns(4).Find("*", , , , xlByColumns, xlPrevious).Row
'Add a formula to add everything up, then copy paste values.
With .Range(.Cells(1, 5), .Cells(lLastRow, 5))
.FormulaR1C1 = _
"=SUMIF(R1C1:R" & lLastRow_rpt & "C1,RC4,R1C2:R" & lLastRow_rpt & "C2)"
.Copy
.PasteSpecial xlPasteValues
End With
.Range("A1:C1").EntireColumn.Delete
.Columns(1).AutoFit
End With
End Sub
我将其添加为单独的答案,因为它与我的第一个答案完全不同。
在您的工作簿中的某处创建一个包含 sheet 个姓名的列表。例如在 A1 中放置 'Norse Temple 30ML',在 A2 中放置 'Norse Temple 15ML',等等。命名这个范围 - 我在示例中将其命名为 SheetList。
创建这个公式:
=SUMPRODUCT(SUMIF(INDIRECT("'" & SheetList & "'!B:B"),"Nicotine 100mg",INDIRECT("'" & SheetList & "'!C:C")))
'Nicotine 100mg' 可以替换为单元格引用,以便您可以向下拖动。
此方法需要您在报告页面上手动创建成分列表,但系统会为您完成添加。它也会在您更新数字时更新 - VBA 方法需要手动执行代码。
如果你想要一个 VBA 解决方案,我可以创建一个 - 不过需要找时间。
Link 到我找到解决方案的地方:http://www.mrexcel.com/forum/excel-questions/119020-sumif-multiple-sheets.html