VBA - Add SUM to every empty cell in column, but make the sum range the range of cells below the formula.

Royzer

New Member
Joined
Jun 22, 2010
Messages
48
Hi.

I need to find a way to search a specific column for blank cells and put a variable-range SUM formula in the blank cell above the range to be summed. This also includes cases where there is only one cell with data. I will still need a sum formula above that cell that contains the amount in the cell.

I've included an image example of the data I'll be working with, along with several examples of what I mean about the sum range. (I also have a workbook with the data in it, but i couldn't figure out how to upload it).

I would really appreciate any help I can get. Thank you.

data example.jpg
data example.jpg
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Try this:

VBA Code:
Sub SumCells()
  Dim ar As Range
  For Each ar In Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants).Areas
    ar.Offset(-1).Resize(1).Value = WorksheetFunction.Sum(ar)
  Next
End Sub
 
Upvote 0
try this code
VBA Code:
Sub test()
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
inarr = Range(Cells(1, 3), Cells(lastrow + 1, 3))
brow = lastrow
For i = lastrow To 3 Step -1
   If inarr(i, 1) = "" And inarr(i + 1, 1) <> "" Then
    Cells(i, 3).Formula = "=Sum(C" & i & ":C" & brow & ")"
    brow = i
   End If
Next i

End Sub
 
Upvote 0
Thanks so much, DantaAmor. The code works perfectly!:)

Thank you as well, offthelip. For a reason I don't understand, the formulas went into the correct cells and the sum ranges are correct, but formula result for each of them is zero.
 
Upvote 0
Try this:

VBA Code:
Sub SumCells()
  Dim ar As Range
  For Each ar In Range("C3:C" & Range("C" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants).Areas
    ar.Offset(-1).Resize(1).Value = WorksheetFunction.Sum(ar)
  Next
End Sub
Deal sir. I use your code but ít not works. Can you write for 1 excel file end send me
 
Upvote 0

Forum statistics

Threads
1,224,829
Messages
6,181,218
Members
453,024
Latest member
Wingit77

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top