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

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
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,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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