VBA /macro to print only visible data

thorpyuk

Well-known Member
Joined
Mar 14, 2006
Messages
1,453
Hi Guys,

I have a sheet thats created automatically from a load of filtered data. Unfortunately, this does mean that there's loads of 'empty' cells that seem to be picked up as valid, meaning that any print preview will show my data bunched up in the corner!

Does anyone have any code that will 1) auto-set the print area to only cells that have data in them, and 2) automatically select landscape layout + fit to 1 page? (all my data should go on 1 page always)

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.
Try

Code:
Sub test()
    With ActiveSheet.PageSetup
        .PrintArea = ActiveSheet.UsedRange.Address
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    ActiveSheet.PrintOut
End Sub
 
Upvote 0
Hi Vog,

Thanks for your reply.

It was a partial successs..... the page does indeed orientate itself correctly, however it still tries to print loads of blank pages.....

Do you have code to print selection? If so, I can use:

Code:
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select

And print using that method....

Also, is there any way I can select a printer other than the default printer?
Thanks!
 
Upvote 0
Code:
    Selection.PrintOut

but I don't think that this will do anything different.

To select a different printer:

Code:
Dim bresponse as Boolean
bResponse = Application.Dialogs(xlDialogPrinterSetup).Show
If bResponse = False Then
    MsgBox "User cancelled"
    Exit Sub
End If
 
Upvote 0
I'm so close! I think it just needs a bit of re-jigging....

Code:
Private Sub CommandButton1_Click()
    With ActiveSheet.PageSetup
        '.PrintArea = ActiveSheet.UsedRange.Address
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    ActiveSheet.PageSetup.PrintArea = Selection.Address
    ActiveSheet.PrintOut
Range("A1").Select
End Sub

This works! The only thing it doesn't do is print on 1 page. It now prints on 2 pages width-wise, as if it's not picking up the .fittopageswide = 1 properly...

I've probably got stuff in there I dont need now as well!
Any ideas what I need to change? Thanks for your help by the way, it's nice to see a fellow brit on here!
 
Upvote 0
Perhaps change the order:

Code:
Private Sub CommandButton1_Click()
    Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    With ActiveSheet.PageSetup
        .PrintArea = Selection.Address
        .Orientation = xlLandscape
        .FitToPagesWide = 1
        .FitToPagesTall = 1
    End With
    ActiveSheet.PrintOut
    Range("A1").Select
End Sub
 
Upvote 0
No, same thing :/ prints landscape but wont fit correctly on 1 page, spreads over 3 wide, and if there's too much going down, prints only a set amount of rows too....
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,893
Members
449,097
Latest member
dbomb1414

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