Format macro

kizzie37

Well-known Member
Joined
Oct 23, 2007
Messages
533
I have the below macro i recorderd to format sheets in excel, I get an error on the second line of this sheet - 1004 Object defined error. Its no different to any of the other sheets, so i dont understand what the error is.

Sheets("ASSUMPTIONS").Select
Range("A1:H1").Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$H$78"


Can anyone help?
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Jon von der Heyden

MrExcel MVP, Moderator
Joined
Apr 6, 2004
Messages
10,808
Office Version
  1. 365
Platform
  1. Windows
How about:

Sheets("ASSUMPTIONS").PageSetup.PrintArea = "$A$1:$H$78"
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,219
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
With Sheets("ASSUMPTIONS")
 
     LastRow = .Range("A" & Rows.Count).End(xlUp).Row
 
     Set rng = Sheets("ASSUMPTIONS").Range("A1:H" & LastRow)
 
     .PageSetup.PrintArea = rng.Address
 
End With
Using the macro recorder is a good idea, especially to get the general syntax, but the generated code always needs a tweak or two.
 

kizzie37

Well-known Member
Joined
Oct 23, 2007
Messages
533
Thanks, both work great, it woul be helpful to udnerstand why a recorded macro, doesnt work when you run it back?. Would like to understand so I know for the future
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,219
Office Version
  1. 365
Platform
  1. Windows
Well I suppose you could say it's because it's a recorded macro, also it does what it says - ie records whatever you fo.

It's a great tool but it can't perform magic, it's very rarely that the generated code will be your final code.

Use it as a guide to get the right syntax etc.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,158
Messages
5,600,052
Members
414,357
Latest member
Gemma_R

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
Top