Variable printarea (resolved)

opopanax666

New Member
Joined
Sep 27, 2006
Messages
18
Hi guys & gals,

Here's 'my' code :

Private Sub CommandButton1_Click()
Dim MyValue As Integer
Sheets("Blad1").Select
MyValue = Range("ab1").Value
Range("$A$1:$Z$" & MyValue).Select
ActiveSheet.PageSetup.PrintArea = "$A$1:$Z$" & MyValue
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Registers").Select
End Sub

So, the button is placed on sheet "Registers", the area to print on "Blad1". I'm using a formula to give me the # of rows to print in cell AB1 on "Blad1" (can only be integer, btw).
I'm new to VB, but this ought to be pretty straightforward, so why does it give me an error in "Range("$A$1:$Z$" & MyValue).Select"?

Thank you for any feedback.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Your code worked for me, but you could try removing the $ signs and ensuring that the CommandButton's TakeFocusOnClick property is False.
 

opopanax666

New Member
Joined
Sep 27, 2006
Messages
18
Private Sub CommandButton1_Click()
Dim MyValue As Integer
Sheets("Blad1").Select
MyValue = Range("ab1").Value
Range("A1:Z" & MyValue).Select
ActiveSheet.PageSetup.PrintArea = "A1:Z" & MyValue
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
Sheets("Registers").Select
End Sub

And I set the TakeFocusOnClick flag to False
-----------------------------------------------------
In the meantime someone over at VBforums came up with this solution, and it did the trick :

Private Sub CommandButton1_Click()
Dim MyValue As Integer
MyValue = Sheets("Blad1").Range("ab1").value
Sheets("Blad1").PageSetup.PrintArea = "A1:Z" & MyValue
Sheets("Blad1").PrintOut Copies:=1, Collate:=True
End Sub

Thanks very much for the feedback!
 

Forum statistics

Threads
1,136,649
Messages
5,676,990
Members
419,667
Latest member
MegEri

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