Limit formatting in imported Macro to row numbers

Alex501

Board Regular
Joined
Dec 11, 2015
Messages
59
Office Version
  1. 365
  2. 2019
Hi All

I recorded a macro to format some data I use regularly. The original sheet I recorded the macro with had 161 rows.

When I've next run the macro on a second sheet with 80 rows the formatting extends beyond the rows to row 161. The column formatting was to add a country code of 44 to every line, however i now have 44 in 81 extra rows where the rest of the row is blank.

How do I edit the VBA code so it limits the formatting to the number of rows?

I assume I need to edit the following somehow;

ActiveCell.FormulaR1C1 = "Country Code"
Range("E2").Select
ActiveCell.FormulaR1C1 = "44"
Range("E2").Select
Selection.AutoFill Destination:=Range("E2:E161")

Thanks
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Without seeing your data, this is the best guess I can make:

VBA Code:
Range("E1") = "Country Code"
'Find last row with data in column D
Dim lr as Long
lr = Cells(Rows.Count, "D").End(xlUp).Row
'Populate column E down to last row
Range("E2:E" & lr) = "44"
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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