Formula =R[-1]C-RC[-1] by vba subtracting gives error

MKLAQ

Active Member
Joined
Jan 30, 2021
Messages
387
Office Version
  1. 2016
Platform
  1. Windows
hello experts
I need help about this formula I have this data but when I run it gives me error I try modified but I failed
the subtracting should begins c2-b3 and you can see the picture1 how subtracting but it gives error in picture to so I need help
picture1
itemBc
5000
14004600
23004300
35003800
46003200


picture2
itemBc
#VALUE!
1400#VALUE!
2300#VALUE!
3500#VALUE!
4600#VALUE!

thanks
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
The formula in cell C2 throws an error because there is text above in cell C1. The other formulas throw an error because the cell in above has an error.
 
Upvote 0
thanks I know what you said , can you guide me how I can modify
VBA Code:
Sub Macro()

    Dim lr As Long

    lr = Cells(Rows.Count, "B").End(xlUp).Row
    Range("C2:C" & lr).FormulaR1C1 = "=R[-1]C-RC[-1]"

End Sub
 
Upvote 0
I think you want to begin your formula on row 3, not row 2, right (as C2 contains the hard-coded value 5000)?
If that is the case, your formula in your code shoudl be:
Rich (BB code):
    Range("C3:C" & lr).FormulaR1C1 = "=R[-1]C-RC[-1]"
 
Upvote 0
Solution
@Joe4 I'm not sure if I did it , but I think I did it , anyway many thanks for solve my problem ;)
 
Upvote 0
You are welcome.
Glad we were able to help.
 
Upvote 0
@Joe4 may you mode the code ,please? I added another column it should the add column b to the balance in columnd and subtracting from column c can you follow the formula as in my picture
itemBc
3000
11002002900
22004002700
33007002300
41008001600
 
Upvote 0
If you are finding the RC reference confusing, there is a little trick you can do to make Excel figure it out for you.
Simply turn on your Macro Recorder, and record yourself typing in one of the formula you want on the worksheet in the normal format you are used to (i.e. =D2+B3-C3).
Then stop the Macro Recorder and view the code that you just recorded. The formula part is the VBA code you need to insert into your existing code. So just copy and paste that part into your code.

Note that if you inserted a column to move your formula over one column, that you will them need to adjust that, i.e. this part:
VBA Code:
Range("C3:C" & lr)
will probably need to change to this:
VBA Code:
Range("D3:D" & lr)
 
Upvote 0
I record the macro and I got the formula RC1 and I modified my code but it gives me error method range of object global failed in the line contain the formula
this is my code
VBA Code:
Sub Macro()

    Dim lr As Long

    lr = Cells(Rows.Count, "D").End(xlUp).Row
    Range("D3:D & lr").FormulaR1C1 = "=R[-1]C+RC[-1]-RC[-1]"
End Sub
 
Upvote 0
Why did you move the quotes on the range part?
You have:
VBA Code:
Range("D3:D & lr")
It should be:
VBA Code:
Range("D3:D" & lr)
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,525
Members
448,969
Latest member
mirek8991

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