Need Macro VB Command to copy formula to blank cell

Linda Berardo

New Member
Joined
Mar 19, 2009
Messages
16
I need to start in a given cell and enter a formula, then copy that formula to the next cell up if it is blank. For example, I'll start in cell N33 and want to enter the formula N33-L33, then move to the next cell up N32 and enter the formula N32-L32. I want to continue this until the cell above is filled (which is my header). The starting cell will vary worksheet to worksheet, but I'll always place the cursor in the "starting blank cell", be it N33 or N100 or N12. Once I have the cursor in the starting cell, I'll run the macro.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
starting in cell N33 and want to enter the formula =N33-L33 Causes a Circular Reference; Come Back,,,,
 
Upvote 0
If you enter the formula N33-L33 in N33 you will create a circular reference. This shows you how to enter the formula M33-L33:

Code:
Sub Test()
    Dim FirstBlankRow As Long
    With ActiveCell
        If .Offset(-1, 0) <> "" Then
            FirstBlankRow = .Row
        Else
            FirstBlankRow = .End(xlUp).Offset(1).Row
        End If
        Range(Cells(.Row, .Column), Cells(FirstBlankRow, .Column)).FormulaR1C1 = "=RC[-1]-RC[-2]"
    End With
End Sub
 
Upvote 0
OK; I made an error in my original post. I'll start in cell O33 and want the formula to be N33-L33 and move up to O32 and want that to be N32-L32 and so on until the cell up is not blank (my header row). Sorry for not thinking through my cells before my original post.
 
Upvote 0
Here's another way

Sub Foo()
Nbr = Range("N" & Rows.Count).End(xlUp).Row + 1
Set Rng = Range(Range("N" & Nbr), ActiveCell)
Rng.Cells.FormulaR1C1 = "=RC[-1]-RC[-2]"
End Sub
 
Upvote 0
Re: Need Macro to copy formula even if starting cell different

Assuming that the cell you want to copy is the selected cell try

Code:
Sub test()
With ActiveCell
    .Copy .Offset(, 2)
    .Copy .Offset(, 4)
End With
End Sub
 
Last edited by a moderator:
Upvote 0
Re: Need Macro to copy formula even if starting cell different

VoG, thank you! That worked perfectly. I'm obviously new to this and appreciate your help.
 
Upvote 0

Forum statistics

Threads
1,206,710
Messages
6,074,459
Members
446,071
Latest member
Jolon

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