Johnny Thunder
Well-known Member
- Joined
- Apr 9, 2010
- Messages
- 693
- Office Version
- 2016
- Platform
- MacOS
Hello All,
I am working on a code that I have parsed from several other projects and have hit a small roadblock.
Here is what I got.
Description of my code
1. Does a loop (For Each Statement #1 ) loops thru a list of Filenames on my worksheet Column A and and sets the filename as a variable and then opens the file
2. Once the file is open my second Loop (For Each Statement #2 ) loops thru a list of Sheetnames in a named range "SheetList" and if the adjacent cell of the sheetname contains the text True then it will define a variable "NameofSheet" as a variable and apply .PrintOut to the NameofSheet variable.
Where I am at with the code
1. Currently, when I run the code, the code will run thru the first file and printout the first sheet from the variable "NameOfSheet" as soon as the loop goes to the next Sheetname to print out I get an error "Runtime Error:9 Subscript out of Range"
I have confirmed that the sheetnames I am attempting to print exist. I have also added a msgbox NameofSheet to validate that the code is in fact looping to a secondsheet after the first successful print to see what the second NameofSheet is, but I am not able to figure out what the underlying issue is.
Any help in troubleshooting is appreciated.
I am working on a code that I have parsed from several other projects and have hit a small roadblock.
Here is what I got.
Description of my code
1. Does a loop (For Each Statement #1 ) loops thru a list of Filenames on my worksheet Column A and and sets the filename as a variable and then opens the file
2. Once the file is open my second Loop (For Each Statement #2 ) loops thru a list of Sheetnames in a named range "SheetList" and if the adjacent cell of the sheetname contains the text True then it will define a variable "NameofSheet" as a variable and apply .PrintOut to the NameofSheet variable.
Where I am at with the code
1. Currently, when I run the code, the code will run thru the first file and printout the first sheet from the variable "NameOfSheet" as soon as the loop goes to the next Sheetname to print out I get an error "Runtime Error:9 Subscript out of Range"
I have confirmed that the sheetnames I am attempting to print exist. I have also added a msgbox NameofSheet to validate that the code is in fact looping to a secondsheet after the first successful print to see what the second NameofSheet is, but I am not able to figure out what the underlying issue is.
Any help in troubleshooting is appreciated.
Code:
Sub PrintSheets()
'--------------------------------------------------------
'--- Prints Only specific Sheets from each Territory
'---------------------------------------------------------
Dim Cell As Range, Cell2 As Range
Dim File As Variant
Dim folder As Object
Dim Rng As Range
Dim RngEnd As Range
Dim Wks As Worksheet
Dim NameOfSheet As String
Dim OpenWB As Workbook
Set Wks = Sheet2
Set Rng = Wks.Range("A2")
Set RngEnd = Wks.Cells(Rows.cOunt, Rng.Column).End(xlUp)
Application.DisplayAlerts = False
Application.ScreenUpdating = False
'---------------------------Beginning of code-------------------------------------
If RngEnd.Row < Rng.Row Then Exit Sub Else Set Rng = Wks.Range(Rng, RngEnd)
With CreateObject("Shell.Application")
For Each Cell In Rng
If Cell.Offset(0, 1).Value = Empty Then
Set folder = .Namespace("C:\Users\JLLEE\Dowloaded Files") '<-----------Folder where to find files
'-------------Check that the folder exists--------------
If Not folder Is Nothing Then
'---------- Return the file name with the extension.
File = Dir(folder.self.Path & "\" & Cell.Value & "*")
'---------Check the file exists---------------------
If File <> "" Then
File = folder.self.Path & "\" & File
Cell.Offset(0, 1).Value = Now()
Set OpenWB = Workbooks.Open(File) '----------Opens each individual file
For Each Cell2 In ThisWorkbook.Sheets("PrintTBTFiles").Range("SheetList") 'Named Range on Sheet2
If Cell2.Offset(, 1).Value = True Then
NameOfSheet = Cell2.Value 'Defines the sheet name
'MsgBox NameOfSheet <--------------My line to troubleshoot
Worksheets(NameOfSheet).PrintOut 'Prints each sheet '<-----------------------This is the line that errors out
OpenWB.Close SaveChanges:=False 'Closes the workbook
End If
Next
End If
End If
End If
Next Cell
End With
MsgBox "All Files/Sheets have been printed"
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub