G
Gustaf
I'm writing a macro to sum all values in a selection (constrained to 1
column), except numbers that are displayed in gray. The macro should
work exactly like the built-in Sum button, so that the sum is placed
below the last row in the selection, and it's that part I can't figure
out how to do. How do I select the cell after the last row in the selection?
Public Sub CalculateSum()
' Prepare a sum variable
nsum = 0
' Make sure there is only one column in the range
If Selection.Columns.count > 1 Then
MsgBox "This macro can only be used on 1 column at a time."
Exit Sub
End If
' Loop through selected range
For Each c In Selection.Cells
If c.Font.ColorIndex <> 48 Then
nsum = nsum + c.Value
End If
Next c
MsgBox nsum
End Sub
Many thanks,
Gustaf
column), except numbers that are displayed in gray. The macro should
work exactly like the built-in Sum button, so that the sum is placed
below the last row in the selection, and it's that part I can't figure
out how to do. How do I select the cell after the last row in the selection?
Public Sub CalculateSum()
' Prepare a sum variable
nsum = 0
' Make sure there is only one column in the range
If Selection.Columns.count > 1 Then
MsgBox "This macro can only be used on 1 column at a time."
Exit Sub
End If
' Loop through selected range
For Each c In Selection.Cells
If c.Font.ColorIndex <> 48 Then
nsum = nsum + c.Value
End If
Next c
MsgBox nsum
End Sub
Many thanks,
Gustaf