Multiple Open Files

saltkev

Active Member
Joined
Oct 21, 2010
Messages
324
Office Version
  1. 2013
Platform
  1. Windows
Good Afternoon

I wonder can someone help. I have multiple Excel Files in one Directory.

1. I need to Open Each one in turn.
2. Activate a given Sheet. The Sheet Name will vary from Workbook to workbook (E.g Station 1, Station 2 , Station 3)
3. Print the Selected Sheet to my default printer.
4. Close the Work book.

Many Thanks

Kev
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Kev,

You might consider the following...

Code:
Sub LoopFilesSheets()
Application.ScreenUpdating = False
Dim wb As Workbook, wb1 As Workbook
Dim FolderName As String, fileName As String
Dim i As Long

''''Select folder that contains files
With Application.FileDialog(msoFileDialogFolderPicker)
  .AllowMultiSelect = False
  If .Show = 0 Then Exit Sub
  FolderName = .SelectedItems(1) & "\"
End With

Set wb = ThisWorkbook
fileName = Dir(FolderName & "*.xls?")

''''Loop through files
Do While fileName <> ""
    If fileName <> wb.Name Then
        Set wb1 = Workbooks.Open(FolderName & fileName)
        ''''Loop through sheets
        For i = 1 To wb1.Worksheets.Count
            If InStr(wb1.Sheets(i).Name, "Station") > 0 Then wb1.Sheets(i).PrintOut
        Next i
        wb1.Close savechanges:=False
    End If
    fileName = Dir
Loop
Application.ScreenUpdating = True
MsgBox "The dishes are done, dude!"
End Sub

Cheers,

tonyyy
 
Upvote 0
Hi Tony

I am trying now for the next evolution of your code! What I would like to do is.

Rather than open all the Excel files & print all the Station Sheets in a given Folder. I would like to be able select which Excel files I open and print the station sheet.

thanking you in anticipation

Kev
 
Upvote 0
Top Job Tony, Worked a Treat, Many Thanks

You're very welcome.

Rather than open all the Excel files & print all the Station Sheets in a given Folder. I would like to be able select which Excel files I open and print the station sheet.

Code:
Sub LoopFilesSheets2()
Application.ScreenUpdating = False
Dim fd As FileDialog
Dim wb As Workbook
Dim i As Long, j As Long

''''Open FileDialog and select file(s)
Set fd = Application.FileDialog(msoFileDialogFilePicker)
With fd
    .InitialView = msoFileDialogViewList
    .AllowMultiSelect = True
    If .Show = 0 Then Exit Sub
End With

''''Loop through selected file(s)
For i = 1 To fd.SelectedItems.Count
    Set wb = Workbooks.Open(fd.SelectedItems(i))
    ''''Loop through sheets
    For j = 1 To wb.Worksheets.Count
        If InStr(wb.Sheets(j).Name, "Station") > 0 Then wb.Sheets(j).PrintOut
    Next j
    wb.Close savechanges:=False
Next i
Application.ScreenUpdating = True
MsgBox "The dishes are done, dude!"
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

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