VBA - posting a formula to use the next row

Evapar18

Board Regular
Joined
Aug 3, 2018
Messages
54
I hope this makes sense. I am want to be able to copy a formula to a cell, but I need it to use the next row number.

See below:
If TextBox1 = Enabled And TextBox2 = Enabled Then

With Sheets("Sheet1")

rw = .Range("B" & .Rows.Count).End(xlUp).Row + 1

.Range("P" & rw).Value = TextBox1
.Range("Q" & rw).Value = TextBox2
.Range("E" & rw).Value = "=C9-P9"
.Range("F" & rw).Value = "=C9-Q9"

End With

The Column won't change, but the row number will change. How can I copy this simple formula to use the active row this is copied too?

Thanks,
SB
 

Some videos you may like

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,230
Office Version
  1. 365
Platform
  1. Windows
Do you mean you want the row number in the formula to be the same as the row number the formula is going in?

e.g. if it was going in row 10 the first formula would be =C10-P10?

Code:
With Sheets("Sheet1")

    rw = .Range("B" & .Rows.Count).End(xlUp).Row + 1

    .Range("P" & rw).Value = TextBox1
    .Range("Q" & rw).Value = TextBox2
    .Range("E" & rw).FormulaR1C1 = "=RC[-2]-RC[11]"
    .Range("F" & rw).FormulaR1C1 = "=RC[-3]-RC[11]"

End With
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,287
Office Version
  1. 365
Platform
  1. Windows
You could use this to replace both lines:

Code:
.Range(.Cells(rw, "E"), .Cells(rw, "F")).FormulaR1C1 = "=RC3-RC[11]"
 

Watch MrExcel Video

Forum statistics

Threads
1,123,310
Messages
5,600,879
Members
414,413
Latest member
Sinbin

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
Top