vba to loop through a column containing file paths and print

cknnugget

New Member
Joined
Jun 29, 2020
Messages
44
Office Version
  1. 365
Platform
  1. Windows
I'm writing vba to print all files listed in a table column. The macro works for one filename however, I'm not sure how to loop through the rest of the column to print all the workbooks?
The folder path is located (Sheet9.name).Range("D2")
The file names are located in column A5 and down
This code works for the first Cell in the column A5

Dim sourcefolder As String
Dim sourcefile As String
sourcefolder = (Worksheets(Sheet9.name).Range("D2"))
sourcefile = Range("A5")
Workbooks.Open (sourcefolder) & (sourcefile)
ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
ActiveWindow.Close

How do I loop through changing the file name to contained in the next cell?
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
This could be a solution:
VBA Code:
Option Explicit
Sub test()
    Dim sourcefolder As String
    Dim sourcefile As String
    Dim x      As Long
    sourcefolder = (Worksheets(Sheet9.Name).Range("D2"))
    For x = 5 To Range("A" & Rows.Count).End(xlUp).Row
        sourcefile = Range("A" & x)
        Workbooks.Open (sourcefolder) & (sourcefile)
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        ActiveWindow.Close
    Next x
End Sub
 
Upvote 0
Solution
Dim rowCount As Integer
rowCount = 0

Do While Range("A5").Offset(rowCount,0).Value <> ""
sourceFile = Range("A5").Offset(rowCount,0).Value
'Your code to print sourcefile
rowCount = rowCount + 1

Loop
 
Upvote 0
Worked perfect thank you.
This could be a solution:
VBA Code:
Option Explicit
Sub test()
    Dim sourcefolder As String
    Dim sourcefile As String
    Dim x      As Long
    sourcefolder = (Worksheets(Sheet9.Name).Range("D2"))
    For x = 5 To Range("A" & Rows.Count).End(xlUp).Row
        sourcefile = Range("A" & x)
        Workbooks.Open (sourcefolder) & (sourcefile)
        ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, IgnorePrintAreas:=False
        ActiveWindow.Close
    Next x
End Sub
Worked perfectly thank you
 
Upvote 0

Forum statistics

Threads
1,215,054
Messages
6,122,897
Members
449,097
Latest member
dbomb1414

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