Macro to set print area and print out

DrPips

New Member
Joined
Aug 14, 2009
Messages
35
Hi All,

Can anyone advice the code I'd need to use to set the print area to columns G:J and as many rows as have text in them? Once print area is set, then print as well.

Many Thanks,

Dom
 

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.
Re: Macro to set print area and print

This is not clear but maybe right click the Excel logo just to the left of File on the menu bar and paste in

Code:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
Dim ws As Worksheet, LR As Long
For Each ws In ActiveWindow.SelectedSheets
    With ws
        LR = .Range("G" & Rows.Count).End(xlUp).Row
        With ws.PageSetup
            .PrintArea = .Range("G1:I" & LR).Address
            .Orientation = xlPortrait
            .Zoom = 100
            .FitToPagesTall = 1
        End With
    End With
Next ws
End Sub

then press ALT + Q.
 
Upvote 0
Re: Macro to set print area and print

Sorry, I don't understand what you mean by right click the excel logo, i just get a selection of 3 things - Customise quick access, show quick access and minimise the ribbon.

I will add the macro to a form control button when finished.

If I'm not giving enough info, please let me know.

Many Thanks,

Dom
 
Upvote 0
Re: Macro to set print area and print

Sorry, I don't understand what you mean by right click the excel logo, i just get a selection of 3 things - Customise quick access, show quick access and minimise the ribbon.

I will add the macro to a form control button when finished.

If I'm not giving enough info, please let me know.

Many Thanks,

Dom


You forgot to mention that you are using Excel 2007.

Press ALT+F11 to open the Visual Basic Editor then double click ThisWorkBook then paste in the code.
 
Upvote 0
Re: Macro to set print area and print

Sorry, forgot to mention I'm inserting this code into a current macro. The first line is coming up with an error. I've tried taking out the sub, but it;s not working.


Thanks,

Dom
 
Upvote 0
Re: Macro to set print area and print

It doesn't go in another macro. It is standalone event code. If you placed it in the ThisWorkbook module it will fire whenever printing is initiated.
 
Upvote 0
Re: Macro to set print area and print

Sorry, I think I'm being stupid! When I do alt F11, it comes up with all the macros that are in the sheet, but I can't see anything that says this workbook to double click.

Thanks,

Dom
 
Upvote 0
Re: Macro to set print area and print

Look here

20090906151455.png
 
Upvote 0

Forum statistics

Threads
1,215,327
Messages
6,124,290
Members
449,149
Latest member
mwdbActuary

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