VBA code to run till the last row with value

RAJESH1960

Banned for repeated rules violations
Joined
Mar 26, 2020
Messages
2,313
Office Version
  1. 2019
Platform
  1. Windows
Hello Guys,
In the code, the formula is copied from G1 & H1 and when I double click it, the line fills the formula till the G1:H22 range which is correct in this case. But the range could be different in other cases. When I copy the whole range and paste it below the data the formula is again calculated till G1:H22 which is not right. How do I change the code so that it will work till the last value of the cell?

Option Explicit

Sub AutoFill()
Range("G1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
Range("H1").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
Range("G1:H1").Select
Selection.AutoFill Destination:=Range("G1:H23")
Range("G1:H22").Select
End Sub
Book1
ABCDEFGHIJ
1130-06-2021Receipt100(as per details)4040
2230-06-2021Receipt100Sunday1015.00
3330-06-2021Receipt100Monday2025.00
4430-06-2021Receipt100Tuesday1000.50
5530-06-2021Receipt100Round Off1
6804-07-2021Payment103(as per details)1001.00
7904-07-2021Payment103January100
81004-07-2021Payment103February200
91104-07-2021Payment103March300
101204-07-2021Payment103April400
111304-07-2021Payment103Round Off1
121405-07-2021Receipt104(as per details)4040
131505-07-2021Receipt104Sunday1015.00
141605-07-2021Receipt104Monday2025.00
151705-07-2021Receipt104Tuesday1000.50
161805-07-2021Receipt104Round Off1
172109-07-2021Payment107(as per details)1001.00
182209-07-2021Payment107January100
192309-07-2021Payment107February200
202409-07-2021Payment107March300
212509-07-2021Payment107April400
222609-07-2021Payment107Round Off1
A
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
If someone can please explain the code in a easy way it would be helpful as there are 3 more places in the code where I need to select selected rows and columns containing data.
 
Upvote 0
Assuming that column A determines the last filled row, you could try the following.

VBA Code:
Sub AutoFill()
    Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
    Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
    Range("G1:H1").AutoFill Destination:=Range("G1:H" & Cells(Rows.Count, "A").End(xlUp).Row)
End Sub
 
Upvote 0
Solution
How about
VBA Code:
   With Range("G1:G" & Range("A" & Rows.Count).End(xlUp).Row)
      .FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
      .Offset(, 1).FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
   End With
 
Upvote 0
Assuming that column A determines the last filled row, you could try the following.

VBA Code:
Sub AutoFill()
    Range("G1").FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
    Range("H1").FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
    Range("G1:H1").AutoFill Destination:=Range("G1:H" & Cells(Rows.Count, "A").End(xlUp).Row)
End Sub
That worked perfectly in 2 different sheets. Thanks GWteb
 
Upvote 0
You are welcome. Glad we could help.
 
Upvote 0
How about
VBA Code:
   With Range("G1:G" & Range("A" & Rows.Count).End(xlUp).Row)
      .FormulaR1C1 = "=IF(RC[2]="""","""",-RC[2])"
      .Offset(, 1).FormulaR1C1 = "=IF(RC[2]="""","""",RC[2])"
   End With
Thanks Fluff.
 
Upvote 0

Forum statistics

Threads
1,214,968
Messages
6,122,506
Members
449,089
Latest member
RandomExceller01

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