繁体   English   中英

Excel VBA-根据另一个单元格的值选择单元格,然后选择“自动求和”-是否可以

[英]Excel VBA - Select cells based on the value of another cell and then Autosum - Is it possible

请你帮忙

我正在尝试按照以下示例在突出显示的框中使用自动求和功能(这些以黄色显示,仅出于说明目的)

在A列中出现“总计”一词,这表明在L,M和N列中需要使用自动求和

每个部分中的数据长度各不相同,因此很遗憾,我不能仅记录一个宏来实现此目的。

我需要使用VBA编码,因此,如果您能提供帮助,将不胜感激。

OTIF示例文件

谢谢

问候史蒂夫

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM