简体   繁体   中英

Loops though columns to create sums

Hope you can help me. I have a list of quantities such as this:

example

I need an autosum of everything that is above in the grey rows.

So far my code:

Dim source As Range
Dim iCol As Long
Dim nCol As Long
Dim Cell As Range

Set source = Selection

For iCol = 1 To 5

With source.Columns(iCol)

If Cell.Font.Bold = False Then
i = i + Cell.Value

Else: Cell.Value = i
i = 0


End If

End With
Next iCol

Thanks in advance!

I believe that's what you're after

Dim source As Range, col As Range, cell As Range
Dim res As Double

Set source = Selection

With source
    For Each col In .Columns
        For Each cell In col.Cells
            If cell.Font.Bold Then
                cell.Value = res
                res = 0
            Else
                res = res + cell.Value
            End If
        Next cell
    Next col
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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