请帮助
我正在尝试按照以下示例在突出显示的框中使用autosum功能(这些以黄色显示,仅用于说明目的)
在A栏中,单词" Total"出现,这表明需要在L,M和L列中使用autosum。 Ñ
每个部分的数据长度不一,所以遗憾的是我不能只记录一个宏来实现这一目标。
我需要使用VBA编码,所以如果你能够提供帮助,我们将非常感激。
由于
此致 史蒂夫
答案 0 :(得分:0)
Dim ws As Excel.Worksheet
Dim lRow As Long
Dim lStartSection As Long
Set ws = Application.Sheets("Sheet1")
lRow = 3
lStartSection = 0
'Loop through the used range in the spreadsheet
Do While lRow <= ws.UsedRange.Rows.Count
'If we are at the start of a section record the row.
If lStartSection = 0 And ws.Range("A" & lRow).Value <> "" Then
lStartSection = lRow
End If
'Check if we are at the bottom of a section
If ws.Range("A" & lRow).Value = "Total" Then
ws.Range("L" & lRow).Value = "=SUM(L" & lStartSection & ":L" & lRow - 1 & ")"
ws.Range("M" & lRow).Value = "=SUM(M" & lStartSection & ":M" & lRow - 1 & ")"
ws.Range("N" & lRow).Value = "=SUM(N" & lStartSection & ":N" & lRow - 1 & ")"
'Set this back to zero so we know that we are no longer in a section
lStartSection = 0
End If
lRow = lRow + 1
Loop