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

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Your code worked for me, but you could try removing the $ signs and ensuring that the CommandButton's TakeFocusOnClick property is False.
 
Upvote 0
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!
 
Upvote 0

Forum statistics

Threads
1,214,944
Messages
6,122,391
Members
449,080
Latest member
Armadillos

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