Newbie needs your help inserting formulas!

inigomontoya

New Member
Joined
Apr 24, 2009
Messages
15
I need a VBA code that looks for an empty row and assigns a formula to certain cells on that row which total all the cells above it up to the previous empty row.

I need to do this for quite a few contiguous cells (columns D-AA).

Here's my code that is not working:

Code:
Sub Totals()
Dim LR As Long
Dim i As Long
Dim j As Long
Dim x As Long
x = 4
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 5 To LR
    If Range("A" & i).Value = Empty Then
        For j = 4 To 27 Step 1
            Range(i, j).Formula = "=SUM(i-1, j : x, j)"
        Next j
        x = i
    End If
Next i
End Sub

Also, I would like to format the entire row as well. I need to shade it a certain color.

Please help!

IM
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
here is a macro I use for adding totals or sub-totals

Code:
Sub AutoSum_Formula()
    Dim x As String
    Dim y As String
    
        x = ActiveCell.Offset(-1, 0).End(xlUp).Address(False, False)
        y = ActiveCell.Offset(-1, 0).Address(False, False)
        
        ActiveCell = "=SUM(" & x & ":" & y & ")"
        
End Sub

Sub AutoSubtotal_Formula()
    Dim x As String
    Dim y As String
    
        x = ActiveCell.Offset(-1, 0).End(xlUp).Address(False, False)
        y = ActiveCell.Offset(-1, 0).Address(False, False)
        
        ActiveCell = "=SUBTOTAL(9," & x & ":" & y & ")"
        
End Sub
 
Upvote 0
Thanks! This works well on a cell by cell basis but I forgot to mention that I will have have to run this many times per row and there will be many rows so I'd like to automate as much as possible. Any suggestions?
 
Upvote 0

Forum statistics

Threads
1,223,234
Messages
6,170,891
Members
452,366
Latest member
TePunaBloke

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