How use RC formulas when fill three columns

Abdo

Board Regular
Joined
May 16, 2022
Messages
225
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I have this code.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long
    If Target.Address = Range("G2").Address Or Target.Column = 5 Or Target.Column = 6 Then
        Application.EnableEvents = False
        lr = Cells(Rows.Count, "B").End(xlUp).Row

        Range("G2").FormulaR1C1 = "=RC[-2]-RC[-1]"
        Range("G3:G" & lr).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
        Range("G3:G" & lr).Value = Range("G3:G" & lr).Value
        Application.EnableEvents = True
    End If

End Sub
I try when fill column E or F then will fill two formula
in G2=E2-F2
G3=G2+E3-F3
G4=G3+E4-F4
and so on .
the code will show error in G2,G3 =#VALUE
any help guys,please?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
It is because your 2nd formula is looking at the header row which is text due to your 2nd formula overwriting the first formula, as your last row is 2 at the time i.e. it is putting =G1+E2-F2 in cell G2 as the code is saying
VBA Code:
Range("G3:G2").FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
When there isn't an inv number in row 3 of column B

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long
    If Target.Address = Range("G2").Address Or Target.Column = 5 Or Target.Column = 6 Then
        Application.EnableEvents = False
        lr = Cells(Rows.Count, "B").End(xlUp).Row

        Range("G2").FormulaR1C1 = "=RC[-2]-RC[-1]"
      
        If lr < 3 Then lr = 3
      
        Range("G3:G" & lr).FormulaR1C1 = "=R[-1]C+RC[-2]-RC[-1]"
        Range("G3:G" & lr).Value = Range("G3:G" & lr).Value
        Application.EnableEvents = True
    End If

End Sub
 
Last edited:
Upvote 0
VBA Code:
"=R[-1]C+RC[-2]-RC[-1]"
this is what I got when use recorded macro then I set for this formula in G3 , I don't understand what the header is relating !
should start from row2,3
so what I suppose to fix it?
 
Upvote 0
it works.:)
VBA Code:
        Range("G2").FormulaR1C1 = "=RC[-2]-RC[-1]"<br>
I suppose when write in e2 or f2 so in g2=e2-d2 , but will show error
after that when write in e3 or f3 should look to g2 , not g1 . I don't select cells in the headers to start from headers!
so I don't used to state condition as you did it.in some similar cases no need use IF condition and works ,but in here I'm confused!
 
Upvote 0
It isn't
Excel Formula:
Range("G2").FormulaR1C1 = "=RC[-2]-RC[-1]"
that caused the issue, that was writing in the cell correctly.

It is the 2nd formula, when lr is 2 the cells referenced with
VBA Code:
Range("G3:G" & lr)
are
VBA Code:
 Range("G3:G" & 2)
or
VBA Code:
Range("G3:G2")
which is exactly the same range as
VBA Code:
Range("G2:G3")
and so it was overwriting the formula in G2 with
Excel Formula:
=G1+E2-F2

You could also probably do

Rich (BB code):
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim lr As Long
    If Target.Address = Range("G2").Address Or Target.Column = 5 Or Target.Column = 6 Then
        Application.EnableEvents = False
        lr = Cells(Rows.Count, "B").End(xlUp).Row

'        Range("G2").FormulaR1C1 = "=RC[-2]-RC[-1]"
'
'        If lr = 2 Then lr = 3
        
        Range("G2:G" & lr).FormulaR1C1 = "=SUM(R[-1]C,RC[-2])-RC[-1]"
        Range("G3:G" & lr).Value = Range("G3:G" & lr).Value
        Application.EnableEvents = True
    End If

End Sub


it works.:)
Happy it helped
 
Last edited:
Upvote 0
Solution
VBA Code:
        Range("G3:G" &amp; lr).Value = Range("G3:G" &amp; lr).Value<br>
to avoid show formulas I suppose this
Rich (BB code):
        Range("G2:G" &amp; lr).Value = Range("G2:G" &amp; lr).Value
I prefer the last answering .:)
thanks for your time and help.;)
 
Upvote 0

Forum statistics

Threads
1,223,939
Messages
6,175,533
Members
452,652
Latest member
eduedu

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