vba - Set variables equal to Cell addresses to set print area

Kenn

Board Regular
Joined
Sep 23, 2009
Messages
195
My code should first find Cell1 & store the address to a variable, then find cell2 & store the address to another variable, then set the print area to the range between these 2 variables. A little help would be much appreciated.

Code:
Sub SetPrintArea()
    Dim Cell1 As Range
    Dim Cell2 As Range
    
    ActiveSheet.PageSetup.PrintArea = ""
    
    Range("A3").Select
    Selection.End(xlToRight).Select
    Selection.End(xlUp).Select
    Set Cell1 = ActiveCell
    
    Range("A9").Select
    Selection.End(xlDown).Select
    Set Cell2 = ActiveCell
    
    ActiveSheet.PageSetup.PrintArea = Range(Cell1 & ":" & Cell2)   ' Getting error - [B]Method 'Range' of object'_Global' failed[/B]
End Sub

Thank you.
Kenn
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
I think you're looking for this:

Rich (BB code):
ActiveSheet.PageSetup.PrintArea = Range(Cell1.Address & ":" & Cell2.Address)

' or 

ActiveSheet.PageSetup.PrintArea = Range(Cell1, Cell2)
 
Upvote 0
Thanks iliace. Your suggestions got me thinking. I've been missing the Address part all along.

This is what worked.

Code:
ActiveSheet.PageSetup.PrintArea = Cell1.Address & ":" & Cell2.Address

Thanks a lot.
Kenn.
 
Upvote 0
Another method.

Code:
[color=darkblue]Sub[/color] SetPrintArea()
    
    ActiveSheet.PageSetup.PrintArea = Range(Range("A3").End(xlToRight), Range("A9").[color=darkblue]End[/color](xlDown)).Address
    
End [color=darkblue]Sub[/color]
 
Upvote 0
Thanks AlphaFrog. That's much simpler.
I wonder if could help me a little further please. My code hides empty columns. So sometimes there may be 5 columns visible & some times over 100. How would I specify to print a maximum of 30 columns per page & to print columns A & B on all pages?

Thank you
Kenn.
 
Upvote 0
Another method.

Code:
[color=darkblue]Sub[/color] SetPrintArea()
    
    ActiveSheet.PageSetup.PrintArea = Range(Range("A3").End(xlToRight), Range("A9").[color=darkblue]End[/color](xlDown)).Address
    
End [color=darkblue]Sub[/color]
If the A3 and A9 cell references are fixed (as appears to be the case from the OP's first message), we can take advantage of that fact and employ a shortcut notation in order to compact your code line...
Code:
Sub SetPrintArea()  
  ActiveSheet.PageSetup.PrintArea = Range([A3].End(xlToRight), [A9].End(xlDown)).Address
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,826
Messages
6,121,795
Members
449,048
Latest member
greyangel23

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