PDA

View Full Version : [SOLVED:] Set Print Area conditional



SpiffyMasta
12-09-2011, 12:45 PM
I'm trying to write some code for setting up a print area dependent on whether a certain range has data or no data input. I'm still learning VBA code and this seems like no matter what I try it's out of my league.

The code I want needs to
//
IF there is any data (in this case it will be numbers and maybe text, the cells are otherwise completely empty) in cells AB6:AU19

Then

set the .PrintArea = "A3:BA44" .FitToPagesWide = 2 .FitToPagesTall = 1

Else If the same cells are empty then

set the .PrintArea = "A3:AA44" .FitToPagesWide = 1 .FitToPagesTall = 1

//

Thank you for any help you can provide!

mdmackillop
12-09-2011, 01:49 PM
Welcome to VBAX,
Give this a try

Sub Macro1()
With ActiveSheet.PageSetup
If Application.CountA(Range("AB6:AU19")) > 0 Then
.PrintArea = "$A$3:$BA$44"
.FitToPagesWide = 2
Else
.PrintArea = "$A$3:$AA$44"
.FitToPagesWide = 1
End If
.FitToPagesTall = 1
End With
End Sub

SpiffyMasta
12-09-2011, 02:12 PM
It works thank you. I guess I just did not know what I needed to use to make this work. Could you explain to me how the Application.CountA works?

mdmackillop
12-09-2011, 02:16 PM
COUNTA is a standard worksheet function which is called in this way to use it in VBA.