how fix error #VALUE! & #REF! when using FormulaR1C1

Alaa mg

Active Member
Joined
May 29, 2021
Messages
375
Office Version
  1. 2019
Hi experts,
I try subtracting using FormulaR1C1 should be E2= C2-D2 when fill values in column C or D and after E2 the E3=E2+C3-D3
but it gives error in E1=#VALUE! and E2:E3= #REF! .
how can I fix it, please?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Or Target.Row < 1 Then Exit Sub
Application.EnableEvents = False
If Target.Column = 3 And Target.Row > 1 Or Target.Column = 4 And Target.Row > 1 Then
lr = Range("a" & Rows.Count).End(xlUp).Row
Range("E2").FormulaR1C1 = "=RC[-2]-RC[-1]"
Range("E2").Value = Range("E2").Value
Range("E3:E" & lr).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
Range("E3:E" & lr).Value = Range("E3:E" & lr).Value
End If
Application.EnableEvents = True
End Sub
 
just curiosity , I wanted do that by Formula R1C1 because I use record macro and try to learn how work this formula .
In that case But I do not recommend you complicate yourself with the RC formula. But for illustration purposes:

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target.CountLarge > 1 Or Target.Row < 2 Then Exit Sub
  Dim lr As Long
  
  Application.EnableEvents = False
  If Not Intersect(Target, Range("C:D")) Is Nothing Then
    lr = Range("A:E").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
    With Range("E2")
      .FormulaR1C1 = "=RC[-2]-RC[-1]"
      .Value = .Value
    End With
    If lr > 2 Then
      With Range("E3:E" & lr)
        .FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
        .Value = .Value
      End With
    End If
  End If
  Application.EnableEvents = True
End Sub
 
Upvote 0

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.

Forum statistics

Threads
1,223,911
Messages
6,175,333
Members
452,636
Latest member
laura12345

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