例如: ‘A’栏
abcdeQQQ
1234
2345
3456
4567
11602
汤米
abcdeZZZ
第456章
第767章
1223
汤米
abcdeDDD
4354
654
564355
678
8787
67
5
86778
第456章
7654
673788
汤米
等等…..
这是VBA代码。
Sub SearchSum()
'
' SearchSum Macro
'
'
Dim startIndex As Double, endIndex As Double
startIndex = endIndex = 0
Dim startingValue As String, endingValue As String, relativeStartIndex As String, relativeEndIndex As String
startingValue = "12504" 'Sample Value
endingValue = "12510" 'Sample Value
Dim rowIterator As Integer
rowIterator = 2
Do While endIndex = 0
If Worksheets("Sheet4").Cells(rowIterator, 1).Value = startingValue Then
startIndex = rowIterator
End If
If Worksheets("Sheet4").Cells(rowIterator, 1).Value = endingValue Then
endIndex = rowIterator
End If
rowIterator = rowIterator + 1
Loop
relativeStartIndex = startIndex - Range("C2").Row
relativeEndIndex = endIndex - Range("C2").Row
Range("C2").Select
ActiveCell.Formula = "=SUM(R[" + relativeStartIndex + "]C[-2]:R[" + relativeEndIndex + "]C[-2])"
End Sub
这是运行宏后工作表的外观。