[英]Check if cell value exists in excel sheet, then get the values of the bottom cells and add it
我支持Scott Craner的答案,因為我認為這是最好,最干凈的解決方案。 但是,當您用“ VBA”標記該問題時,我想我也將使用VBA為其提供純編程解決方案。 如果沒有其他說明,它說明了像Scott那樣的好公式多么簡單!
Sub SumCategories()
Const startColumn As Integer = 6 'F
Const endColumn As Integer = 9 'I
Const startRow As Integer = 3
Const endRow As Integer = 14
Const categoryColumn As Integer = 1 'A
Const assignedColumn As Integer = 3 'C
Dim categoryRow As Integer
categoryRow = 2
Dim categoryTotalAssigned As Integer
categoryTotalAssigned = 0
Dim currentCategory As String
currentCategory = ActiveSheet.Cells(categoryRow, categoryColumn)
While Not currentCategory = ""
'Loop through all data to sum totals
For c = startColumn To endColumn
For r = startRow To endRow Step 2 'Look in every other row
If ActiveSheet.Cells(r, c) = currentCategory Then
categoryTotalAssigned = categoryTotalAssigned + ActiveSheet.Cells(r + 1, c)
End If
Next r
Next c
'Write total for category
ActiveSheet.Cells(categoryRow, assignedColumn) = categoryTotalAssigned
'Move to next row
categoryRow = categoryRow + 1
'Reset total for next category
categoryTotalAssigned = 0
'Get next category
currentCategory = ActiveSheet.Cells(categoryRow, categoryColumn)
Wend
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.