Option Explicit
Sub AllFiles()
Dim folderPath As String
Dim filename As String
Dim wb As Workbook
Dim SumRg As Range
folderPath = "\\obcsvr\Share\Account\customer\" 'change to suit
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*.xlsx")
Application.ScreenUpdating = False
Do While filename <> ""
Application.AskToUpdateLinks = False
Application.DisplayAlerts = False
Set wb = Workbooks.Open(folderPath & filename)
'Call a subroutine here to operate on the just-opened workbook
Range("D1").Value = filename
Columns("A:AJ").Select
Selection.Copy
ThisWorkbook.Activate
Sheets("Sheet1").Select
Columns("A:AJ").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Set SumRg = Range("Z4", Range("Z" & Rows.Count).End(xlUp))
Range("Z" & Rows.Count).End(xlUp).Offset(1, 0) = "=Sum(" & SumRg.Address & ")"
Sheets("Print").Select
Cells.Select
Selection.Clear
ActiveSheet.PageSetup.PrintArea = ""
Sheets("template").Activate
Columns("A:S").Select
Selection.Copy
Sheets("Print").Select
Columns("A:S").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
Dim i As Integer
Dim j As Integer
For i = 1 To 1000
j = InStr(1, Cells(i, 15), "Other", vbTextCompare)
If j = 1 Then
'Cells(i, 1).EntireRow.Copy
Cells(i + 1, 1).EntireRow.Insert
Application.CutCopyMode = False
Cells(i + 1, 3).Value = "Total"
Cells(i + 1, 7).Value = Cells(i, 16)
Cells(i + 1, 5).Value = Cells(i, 17)
i = i + 1
Else
End If
Next i
ThisWorkbook.Sheets("Print").Activate
ActiveSheet.PageSetup.PrintArea = ""
Dim LR As Long
Dim ws As Worksheet: Set ws = ActiveSheet
With ws
LR = .Range("G" & .Rows.Count).End(xlUp).Row
.PageSetup.PrintArea = .Range(.Cells(1, 1), .Cells(LR, "N")).Address
.PageSetup.Orientation = xlLandscape
.PageSetup.PrintTitleRows = "$2:$2" --> I just added this, but even without it, it's the same
End With
'ThisWorkbook.Sheets("Print").PrintOut
wb.SaveAs filename:="\\obcsvr\Share\Docs\Cust\Order\west\" & Range("S1").Value & ".xlsx"
wb.Close SaveChanges:=False
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
filename = Dir
Loop
Application.ScreenUpdating = True
MsgBox "Printed"
End Sub