VBA - posting a formula to use the next row

Evapar18

Board Regular
Joined
Aug 3, 2018
Messages
86
Office Version
  1. 2019
Platform
  1. Windows
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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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
 
Upvote 0
You could use this to replace both lines:

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

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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