Printing multiple worksheets from multiple files

sam88

Board Regular
Joined
Nov 1, 2006
Messages
80
I have an excel project that involve multiple worksheets from multiple workbooks and files. For example, file1: I need to print 5 worksheets out of 30 , file 2 3 worksheet out of 10 and file 3 2 worksheet out of 5. I do it manually by printing one by one and manually page number them to stitch a report which is very cumbersome process. Is there an automated way to print this that will self numbering them ?

Thanks for your help.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Build a macro that opens each workbook, copies the needed pages into a new Temporary workbook. Then when the temp workbook is ready, set the header/footer to use page numbers, then print it and close it.
You can record each step using the macro recorder. Then study the code and combine it into one smooth process.
 
Upvote 0
Make a sheet that looks like this:
Print Macro2.xls
ABCD
1PathFilenameSheet
2C:\Path\Filename1.XLSSheet1
3C:\Path\Filename2.XLSSheet1
4C:\Path\Filename3.XLSSheet1
5C:\Path\Filename4.XLSSheet1
FileList



And put this code in a module and run OpenPrint

Code:
Private Function FileExists(fname) As Boolean
   FileExists = (Dir(fname)<> "")
   
End Function
   
Sub OpenPrint()

Dim i As Integer
Dim Filename_i As String
Dim TabName As String
Dim Filename_n As String
Dim EndNum As Integer


EndNum = Range("A1:A" & Range("A65536").End(xlUp).Row).Rows.Count

Range("A2").Select

'Confirm Filename
For i = 2 To EndNum
    Filename_i = Range("A" & i).Value & Range("B" & i).Value
    
    If FileExists(Filename_i) = False Then
        MsgBox "The filename in row " & i & " on tab " & ActiveSheet.Name & " does not exist.", vbExclamation, "FILENAME ERROR"
        GoTo ending
    End If
Next i


'Format/Print
For n = 2 To EndNum
   Filename_n = Range("A" & n).Value & Range("B" & n).Value
    TabName = Range("C" & n).Value
    
    Workbooks.Open Filename:=Filename_n, UpdateLinks:=0
    Sheets(TabName).Select
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    ActiveWorkbook.Close
Next n

Range("A2").Select

ending:

End Sub
 
Upvote 0
You didn't ask but I added some error handling for bad sheet names. Also includes the page numbering.

Code:
Private Function FileExists(fname) As Boolean
   FileExists = (Dir(fname) <> "")
End Function
Sub OpenPrint()

Dim i As Integer
Dim Filename_i As String
Dim TabName As String
Dim Filename_n As String
Dim EndNum As Integer

EndNum = Range("A1:A" & Range("A65536").End(xlUp).Row).Rows.Count

Range("A2").Select

'Confirm Filename
For i = 2 To EndNum
    Filename_i = Range("A" & i).Value & Range("B" & i).Value
    If FileExists(Filename_i) = False Then
        MsgBox "The filename in row " & i & " on tab " & ActiveSheet.Name & " does not exist.", vbExclamation, "FILENAME ERROR"
        Exit Sub
    End If
Next i

'Open & Print
For n = 2 To EndNum
On Error GoTo BadSheet
ReSheet:
   Filename_n = Range("A" & n).Value & Range("B" & n).Value
    TabName = Range("C" & n).Value
    Workbooks.Open Filename:=Filename_n, UpdateLinks:=0
    Sheets(TabName).Select
        
    With ActiveSheet.PageSetup
        .LeftFooter = ""
        .CenterFooter = n - 1
        .RightFooter = ""
    End With
    
    ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True
    Application.DisplayAlerts = False
    ActiveWorkbook.Close
    Application.DisplayAlerts = True
If n = EndNum Then
Range("A2").Select
Exit Sub
End If
Next n

BadSheet:
ActiveWorkbook.Close
ThisWorkbook.Activate
NewSheetName = InputBox("The sheet listed in row " & n & " does not exist." & Chr(10) & Chr(10) & "Please enter a valid sheet name", "SHEET NAME ERROR", TabName)
Range("C" & n).Value = NewSheetName
Resume ReSheet

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,975
Messages
6,122,538
Members
449,088
Latest member
RandomExceller01

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