Open and print sheets in a workbook using vba

Ivn68

Board Regular
Joined
Aug 21, 2015
Messages
82
I want to be able to click a command button and the macro will open another work book and print the first 3 sheets in the new workbook

This is what I got so far

Private sub commandbutton_click()
If Range("A1").value=4 then
Workbooks.open "c:\work\april\ivan\dinner\reciepts.xlsm"
Workbooks(c:\work\april\ivan\dinner\reciepts.xlsm").worksheets(array("1","2","3","4")).printout

End sub

I get runtime error '9'
Subscript out of range
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Try this:

Code:
Private Sub commandbutton_click()
    Dim wb As Workbook
    
    If Range("A1").Value = 4 Then
        Set wb = Workbooks.Open("c:\work\april\ivan\dinner\reciepts.xlsm")
        wb.PrintOut 1, 3
    End If
End Sub

If you're finding that the code doesn't work and you might not have the printout method properly set up, put the cursor on it (e.g., the word "printout") and hit F1 to go to the MS documentation for it.
 
Upvote 0
Susbscript out of range usually means that the sheet does not exist, if you want the first three sheets, try
Code:
Workbooks(c:\work\april\ivan\dinner\reciepts.xlsm").worksheets(array(1,2,3)).printout
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,196
Members
452,616
Latest member
intern444

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