我希望根据另一列包含某些单词来计算总和。 说A1-A2 Ben B1-B2有1,3。 A3-A5的Bob B3-B5有2,5,6。 A6-A8比尔B6-B8有3,9,1。然后,我将如何为包含ben或bob或Bill的所有单元格添加这些总计。列入C1 Ben 4,C2 Bob 13,C3 Bill 13.
说我是否有更多名称可以添加如何在同一张纸上添加更多名称。
这是我到目前为止所做的,我已经放入了第二个小宏,因为我需要使用它来确定最后一个用Ben的单元格,而不是使用1Row,这是列中的最后一个单元格。
Sub SumValues()
Dim I As Integer
Dim lRow As Long
Dim lCol As Long
Dim first As Variant
Dim last As Variant
Dim Sum1 As Variant
Dim Sum2 As Variant
Range("A1").Select
lRow = Cells(Rows.Count, 1).End(xlUp).Row
For I = 1 To 1
Cells.Find(What:="ben", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
first = ActiveCell.Row
Next I
For lCol = first To (lRow - 1)
Cells.Find(What:="ben", After:=ActiveCell, LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False).Activate
Next lCol
last = ActiveCell.Row
Range("G1").Value = Application.Sum(Range(Cells(first, 2), Cells(last, 2)))
End Sub
Sub SelectBetween()
Dim findrow As Long, findrow2 As Long
On Error GoTo errhandler
findrow = Range("A:A").Find("bob", Range("A1")).Row
findrow2 = Range("A:A").Find("ben", Range("A" & findrow)).Row
Range("B" & findrow & ":B" & findrow2 - 1).Select
errhandler:
MsgBox "No Cells containing specified text found"
End Sub
任何帮助或建议表示赞赏!!