如何在每次碰到一个空白时插入一个循环并插入上面的行总和公式?

时间:2017-07-17 03:34:15

标签: excel-vba vba excel

我遇到了创建循环的问题,我可以将" delivery qty"在每个部分下面的黄点。

我想要做的是循环整个表格,并做出"交付数量"的总和。并将其插入我在每个供应商编号后插入的黄色空白区域。

question

Sub SumOfDeliveryQty()
    Dim ws As Excel.Worksheet
    Dim lRow As Long
    Dim lStartSection As Long

    Set ws = Application.Sheets("PO Copy")
    lRow = 2
    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("F" & lRow).Value <> "" Then
            lStartSection = lRow
        End If

        'Check if we are at the bottom of a section
        If ws.Range("F" & lRow).Value = "Total" Then
            ws.Range("F" & lRow).Value = "=SUM(F" & lStartSection & ":F" & 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
End Sub

0 个答案:

没有答案