Why does loop stop after exporting only one file? - excel

I am trying to export Excel files to another folder as PDFs. The macro is stored in a separate .xlsm that I have open, and I directed the code to the folder with all the files that need to be PDFs.
The code only exports the first PDF in the folder. The error I got was that it could not operate in Page Break Mode, so I set it to normal mode for running the code but I still get the error.
Beyond that, it is reading the workbook that I have the macro stored in as a second active window. I ran the code to export to PDF on a single PDF and it worked as expected.
Option Explicit
Sub PPG_PDF_File()
'Below is used to make code run faster
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim wkbDest As Workbook
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim strExtension As String
Dim wsA As Worksheet
Dim strName As String
Dim strName1 As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
Const strPath1 As String = "C:\Users\steve.argen\Documents\PPG\GW Sampling March 2020\PPG Balogna NUMBER 2\Final excel sheets\Test Macro Folder DNAPL Wells\"
ChDir strPath1
strExtension = Dir(strPath1 & "*.xls*")
Do While strExtension <> ""
Set wkbSource = Workbooks.Open(strPath1 & strExtension)
With wkbSource.Sheets("LowFlow GW front")
ActiveWindow.View = xlNormalView
On Error GoTo errHandler
Set wkbSource = ActiveWorkbook
Set wsA = ActiveSheet
'get active workbook folder, if saved
strPath = wkbSource.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
strName = wsA.Range("A1").Value _
& " - " & wsA.Range("A2").Value _
& " - " & wsA.Range("A3").Value
'create default name for savng file
strFile = wkbSource.Name & ".pdf"
strFile = Replace(strFile, ".xlsx", "")
strPathFile = "C:\Users\steve.argen\Documents\PPG\GW Sampling March 2020\PPG Balogna NUMBER 2\Final excel sheets\Final PDF\" & strFile
'export to PDF in current folder
wkbSource.Sheets(Array("LowFlow GW Front", "LowFlow GW Back")).Select
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
strPathFile, Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
Application.CutCopyMode = False 'If you ever need to copy a large amount of info, this will hide any warnings
ActiveWindow.View = xlPageBreakPreview
End With
wkbSource.Close savechanges:=False
strExtension = Dir
Loop
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub

This code
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
it should be at the end of the routine and not in the middle of the loop.

Related

How to loop through two different paths in VBA? [duplicate]

I have the code below that I would like to run to all of the available excel files in a folder. Ideally, I would like to input the path of the folder into cell C3 in Sheet1 and the macro to apply the code to all of the existing files.
The code will simply save the second sheet of each file into a PDF version, it works perfectly standalone.
Sample Folder Path:
C:\Users\MMMM\Desktop\Project X\ Project II
Suggestions on how to approach this?
Private Sub CommandButton1_Click()
Dim MyFolder As String, MyFile As String
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
MyFolder = .SelectedItems(1)
Err.Clear
End With
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
Application.Calculation = xlCalculationManual
MyFile = Dir(MyFolder & "\", vbReadOnly)
Do While MyFile <> ""
DoEvents
On Error GoTo 0
Workbooks.Open Filename:=MyFolder & "\" & MyFile, UpdateLinks:=False
Dim ReportSheet As Worksheet
Dim allColumns As Range
Set allColumns = Sheets("RT").Columns("N:S")
allColumns.Hidden = True
With Worksheets("RT").PageSetup
.Zoom = False
.FitToPagesWide = 1
End With
Filename = ActiveWorkbook.Name
Cell = Replace(Filename, ".xlsx", ".PDF")
Set ReportSheet = Sheets("RT")
Sheets("RT").Select
Sheets("RT").PageSetup.Orientation = xlLandscape
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & Cell, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=True, OpenAfterPublish:=True
0
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir
Loop
'turns settings back on that you turned off before looping folders
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
Application.Calculation = xlCalculationManual
End Sub
This needs a reference (see this link)
It's untested (so let me know if anything comes up)
Basically:
As suggested by SmileyFtW it asks you for the root folder
Scans the subfolders for excel files (adjust the extension in code)
Process the DoSomething procedure where you export the file
EDIT: Added handle user cancel file select dialog
Code:
Option Explicit
' Add a reference to Microsoft Scripting Runtime
' See https://vbaf1.com/filesystemobject/create-microsoft-scripting-runtime-library-reference/
Private Sub ProcessAllFilesInFolder()
Dim FileSystem As Scripting.FileSystemObject
Dim fileDialogResult As Office.FileDialog
Dim folderPath As String
Set FileSystem = New Scripting.FileSystemObject
Set fileDialogResult = Application.FileDialog(msoFileDialogFolderPicker)
With fileDialogResult
.AllowMultiSelect = False
.Title = "Select a folder"
If .Show = True Then
folderPath = .SelectedItems(1)
End If
If .SelectedItems.Count = 0 Then Exit Sub
End With
ProcessFolder FileSystem.GetFolder(folderPath)
End Sub
Private Sub ProcessFolder(ByVal targetFolder As Scripting.Folder)
Dim FileSystem As Scripting.FileSystemObject
Dim File As Scripting.File
Dim SubFolder As Scripting.Folder
Set FileSystem = New Scripting.FileSystemObject
For Each SubFolder In targetFolder.SubFolders
ProcessFolder SubFolder
Next
For Each File In targetFolder.Files
If FileSystem.GetExtensionName(File.Name) Like "xls?" And File.Name <> ThisWorkbook.Name Then
DoSomething File.Path
End If
Next
End Sub
Private Sub DoSomething(ByVal filePath As String)
Dim FileSystem As Scripting.FileSystemObject
Dim ReportSheet As Worksheet
Dim targetFileName As String
targetFileName = Replace(ThisWorkbook.Name, ".xlsm", ".PDF")
Set ReportSheet = ThisWorkbook.Worksheets("Sheet2")
ReportSheet.PageSetup.Orientation = xlLandscape
ReportSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
ThisWorkbook.Path & "\" & targetFileName, _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=True, OpenAfterPublish:=True
End Sub
Let me know if it works!

Workbook.Activate method

i got a variable:
V_WBNameOutPut as string
and use it inside the following code:
Application.Workbooks(V_WBNameOutPut).Activate
This two part of code are inside a huger code which work fine for 99.99% of different users, but only for one user the code go in error and when I debug its stop to Application.Workbooks(V_WBNameOutPut).Activate line.
And the error is the following:
Runtime Error 9: Subscript Out of Range
Any ideas why this happend and possible solution?
Thanks
I try it to debug but the code works fine but for one particular user it doesn't
The subroutine to generate the output file, which the Application.Workbooks(V_WBNameOutPut).Activate refers to:
Sub CreateWB()
Dim File_Name As Variant
Dim File_Name_Saved As String
Dim i_attempt As Integer
Dim NewWorkBook As Workbook
Set NewWorkBook = Workbooks.Add
Do While i_attempt < 2
i_attempt = i_attempt + 1
File_Name = Application.GetSaveAsFilename(InitialFileName:=V_WBNameOutPut, filefilter:="Excel Files(*.xlsx),*.xlsx,Excel-Macro Files (*.xlsm),*.xlsm", Title:="Please choose a Folder")
File_Name_Saved = Left(Right(File_Name, Len(V_WBNameOutPut) + 5), Len(V_WBNameOutPut))
If File_Name = False Then
ActiveWorkbook.Close
End
Else
If UCase(File_Name_Saved) <> UCase(V_WBNameOutPut) Then
If i_attempt < 2 Then
MsgBox "Please do not change the File name" & vbCrLf & i_attempt & "/2 Attempt"
Else
ActiveWorkbook.Close
End
End If
Else
Application.DisplayAlerts = False
NewWorkBook.SaveAs File_Name, ConflictResolution:=True
Exit Do
End If
End If
Loop
End Sub
You can loop through the open workbooks looking for a match without the file extension. A better solution would be to make CreateWB a function that returns the saved filename.
Option Explicit
Dim V_WBNameOutPut
Sub test()
Dim wb As Workbook
V_WBNameOutPut = "test2"
CreateWB
For Each wb In Workbooks
If wb.Name Like V_WBNameOutPut & "*" Then
wb.Activate
Exit For
End If
Next
Sheets(1).Cells(1, 1).Select ' active workbook
End Sub
Sub CreateWB()
Dim NewWorkBook As Workbook
Dim fso As Object, bSaveOK As Boolean, i_attempt As Integer
Dim File_Name As Variant, File_Name_Saved As String
Set fso = CreateObject("Scripting.FileSystemObject")
For i_attempt = 1 To 2
File_Name = Application.GetSaveAsFilename( _
InitialFileName:=V_WBNameOutPut, _
filefilter:="Excel Files(*.xlsx),*.xlsx,Excel-Macro Files (*.xlsm),*.xlsm", _
Title:="Please choose a Folder")
If File_Name = False Then Exit Sub
bSaveOK = (fso.getbasename(File_Name) = V_WBNameOutPut)
If Not bSaveOK And i_attempt = 1 Then
MsgBox "Please do not change the File name from " & V_WBNameOutPut _
& vbCrLf & i_attempt & "/2 Attempt"
Else
Exit For
End If
Next
' create workbook and save
If bSaveOK Then
Set NewWorkBook = Workbooks.Add
Application.DisplayAlerts = False
NewWorkBook.SaveAs File_Name, ConflictResolution:=True
Application.DisplayAlerts = True
End If
End Sub

Workbook Scraper Issue

I have been working on a workbook that scrapes data from hundreds of other workbooks and it works fine. However, when an error occurs, the workbook that has the error opens in the background and since this happens numerous times, my computer freezes before it can get through all the workbooks. Is there a way to suppress all link issue prompts and close workbooks that have errors instead of having them remain open? Here is the code that I have that works great for small sets of workbooks (I have done 10 without issue):
Sub CopyRange()
Application.ScreenUpdating = False
Dim wkbDest As Workbook, sh As Worksheet
Dim wkbSource As Workbook
Set wkbDest = ThisWorkbook
Dim LastRow As Long
'you need to create this worksheet named "Log"
Dim LogSheet As Worksheet
Set LogSheet = ThisWorkbook.Worksheets("Log")
Const strPath As String = "E:\Desktop\Example\"
ChDir strPath
strExtension = Dir(strPath & "*.xls*")
Application.StatusBar = "Importing Data..."
Do While strExtension <> ""
path = strPath & strExtension
If VerifyTasks(strPath & strExtension, wkbDest) Then
LogSheet.Range("A" & LogSheet.Rows.Count).End(xlUp).Offset(1, 0).Value = strPath & strExtension & " " & "Succeeded"
Else
LogSheet.Range("A" & LogSheet.Rows.Count).End(xlUp).Offset(1, 0).Value = strPath & strExtension & " " & "Failed"
End If
On Error GoTo 0
strExtension = Dir
Loop
Application.ScreenUpdating = True
Application.StatusBar = False
MsgBox "Data imported, review Log sheet."
End Sub
Function VerifyTasks(path As String, ByRef wkbDest As Workbook) As Boolean
On Error GoTo errorhandler:
Set wkbSource = Workbooks.Open(path)
With wkbSource
'locate last row to start copying new value from the next spreadsheet
LastRow = wkbDest.Sheets("Master").Cells(Rows.Count, "A").End(xlUp).Offset(1, 0).Row
'From the Basis & Credits cell AB46, copy to last row+1 in the Master sheet starting in row A2
.Sheets("Basis & Credits").Range("AB46").Copy
wkbDest.Sheets("Master").Range("A" & LastRow).PasteSpecial Paste:=xlPasteValues
.Close savechanges:=False
End With
VerifyTasks = True
Exit Function
errorhandler:
VerifyTasks = False
End Function
Thank you.

Save clipboard when closing workbook VBA

It seems like a easy question, yet I can't seem to find the correct answer on Google.
What I want to do is open a workbook, copy a section and then close the workbook while saving the section I just copied.
I'm aware of the function to disable the clipboard prompt:
Application.CutCopyMode = False
ActiveWindow.Close
But this does not save the clipboard. Thus far I have written the following code to do so:
Sub Input()
Application.ScreenUpdating = False
Dim wb As Workbook
Dim wbPad As String
On Error GoTo ErrHandler
wbPad = ThisWorkbook.Sheets("Voorblad").Range("C10").Value
Set wb = Workbooks.Open(wbPad)
Cells.Select
Selection.Copy
Windows("Masterfile.xlsm").Activate
Worksheets("INPUT").Activate
Cells.Select
ActiveSheet.Paste
Range("A1").Select
Worksheets("Voorblad").Activate
Exit Sub
ErrHandler:
MsgBox ("Bestand niet gevonden. Controleer de maand en de naam van het bestand dat je wilt openen")
End Sub
If this is not possible, I would like to .Activate the workbook I opened using the cell reference and close this.
Maybe you could just skip the whole .select and .activate commands and use the optional Destination parameter of the .copy function.
(https://learn.microsoft.com/de-de/office/vba/api/excel.range.copy)
Since you did not provide how you want to save the range, I've added multiple basic examples below.
OPT1 - Save as .xlsx or .csv
Dim cpyRng As Range, newWb As Workbook, sPath As String
Application.DisplayAlerts = False 'remove system alert prompts
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
sPath = ThisWorkbook.Path & "\"
Set newWb = Workbooks.Add
With newWb
cpyRng.Copy
.Sheets("Sheet1").Cells(1, 1).PasteSpecial Paste:=xlPasteValues
.SaveAs Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & ".xlsx", FileFormat:=51 'change file name to suit
'If you want to save as .csv use
'.SaveAs Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & ".csv", FileFormat:=6
.Close
End With
'save your workbook and quit Excel
ThisWorkbook.Save = False 'use "True" if you want to save changes
Application.Quit
Application.DisplayAlerts = True 'Turn system alert prompts back on(best practice)
OPT2 - Save as .pdf
Dim cpyRng As Range, sPath As String
Application.DisplayAlerts = False 'remove system alert prompts
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
sPath = ThisWorkbook.Path & "\"
'Change file name to suit
cpyRng.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sPath & "Test" & "_" & Format(Date, "yyyymmdd") & _
".pdf", Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=True
Application.DisplayAlerts = True 'Turn system alert prompts back on(best practice)
OPT3 - Save as Word Doc
Dim cpyRng As Range
Set cpyRng = ThisWorkbook.Sheets("Sheet1").Range("A1:C10") 'Change sheet and range as needed
Dim objWord As Object
Set objWord = CreateObject("Word.Application")
cpyRng.Copy
With objWord
.Visible = True
.Documents.Add
.Selection.Paste
End With
Application.CutCopyMode = False
Set objWord = Nothing

VBA Syntax to export Certain tabs to PDF in custom order

I have a macro that I can use in many workbooks to export certain tabs by name to a PDF, which works. The problem is the named tabs which I need to export are not always in the same order/my desired order. My code below shows the names of the tabs which I am exporting to PDF, but excel defaults the export order of named tabs to the order in which they appear(from left to right). I was wondering if any of you know how I could define the order which these sheets appear in the PDF no matter what order they appear in my workbook? I am trying to avoid a macro that would export my sheets to a separate workbook temporarily to do this.
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.Path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.Name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename _
(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
wbA.Activate
wbA.Sheets(Array(wbA.Sheets(2).Name, wbA.Sheets(3).Name)).Select
**------------------------------ THis is where I imagine the code would go**
ActiveSheet.ExportAsFixedFormat _
Type:=xlTypePDF, Filename:=myFile, Quality:=xlQualityStandard, _
IncludeDocProperties:=True, IgnorePrintAreas:=False,
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub
Similar to what #fabio.avigo mentioned, modify the routine you posted like this:
Sub PDFActiveSheet(ByRef wsA As Worksheet)
...
'--- comment out this line
'Dim wsA As Worksheet
'--- and this one
'Set wsA = ActiveSheet
...
End Sub
Then create another sub to call it with your worksheets in any order you want, like this:
Public Sub PDFMySheets()
PDFActiveSheet ThisWorkbook.Sheets("Sheet3")
PDFActiveSheet ThisWorkbook.Sheets("Sheet2")
PDFActiveSheet ThisWorkbook.Sheets("Sheet1")
End Sub
The problem with exporting selected worksheets to a PDF is that Excel will save them in a single file, but only in the order they appear in the workbook. This means we'll have to re-order the worksheets to the desired order. The code below uses the PDFActiveSheet routine as posted in the OP, but adds logic to re-order the worksheets PLUS logic to restore the original order when we're done with the export.
Option Explicit
Public Sub SaveThem()
SaveSheetsToPDF "Sheet3", "Sheet1", "Sheet2"
End Sub
Private Sub SaveSheetsToPDF(ParamArray args())
'--- inputs to this sub are the Worksheet names to save to a single
' PDF file, in the order given. Excel will save multiple
' worksheets to a single PDF, but only in the order they exist
' in the workbook. So we'll have to re-order them.
Dim i As Long
Dim ws As Worksheet
Dim thisWB As Workbook
Set thisWB = ThisWorkbook
'--- initial error checking
If UBound(args, 1) = -1 Then
MsgBox "SaveSheetsToPDF called with no arguments!", _
vbCritical + vbOKOnly
Exit Sub
Else
'--- make sure the sheets exist before proceeding
For i = LBound(args, 1) To UBound(args, 1)
On Error Resume Next
Set ws = thisWB.Sheets(args(i))
If ws Is Nothing Then
MsgBox "SaveSheetsToPDF called with an invalid sheet name!", _
vbCritical + vbOKOnly
Exit Sub
End If
On Error GoTo 0
Next i
End If
'--- save the existing worksheet order
Dim numberOfWorksheetsInBook As Long
numberOfWorksheetsInBook = thisWB.Sheets.Count
Dim sheetsInOrder() As String
ReDim sheetsInOrder(1 To numberOfWorksheetsInBook)
For i = 1 To numberOfWorksheetsInBook
sheetsInOrder(i) = thisWB.Sheets(i).name
Debug.Print i & " = " & sheetsInOrder(i)
Next i
'--- move the given worksheets in the requested order after all the
' other worksheets
With thisWB
For i = LBound(args, 1) To UBound(args, 1)
.Sheets(args(i)).Move After:=.Sheets(numberOfWorksheetsInBook)
Next i
End With
'--- now save those worksheets to a PDF file
thisWB.Sheets(args).Select
PDFActiveSheet
'--- restore the original order to the sheets
Dim sheetName As Variant
With thisWB
For Each sheetName In sheetsInOrder
.Sheets(sheetName).Move Before:=.Sheets(1)
Next sheetName
End With
End Sub
Sub PDFActiveSheet()
'www.contextures.com
'for Excel 2010 and later
Dim wsA As Worksheet
Dim wbA As Workbook
Dim strTime As String
Dim strName As String
Dim strPath As String
Dim strFile As String
Dim strPathFile As String
Dim myFile As Variant
On Error GoTo errHandler
Set wbA = ActiveWorkbook
Set wsA = ActiveSheet
strTime = Format(Now(), "yyyymmdd\_hhmm")
'get active workbook folder, if saved
strPath = wbA.path
If strPath = "" Then
strPath = Application.DefaultFilePath
End If
strPath = strPath & "\"
'replace spaces and periods in sheet name
strName = Replace(wsA.name, " ", "")
strName = Replace(strName, ".", "_")
'create default name for savng file
strFile = strName & "_" & strTime & ".pdf"
strPathFile = strPath & strFile
'use can enter name and
' select folder for file
myFile = Application.GetSaveAsFilename(InitialFileName:=strPathFile, _
FileFilter:="PDF Files (*.pdf), *.pdf", _
Title:="Select Folder and FileName to save")
'export to PDF if a folder was selected
If myFile <> "False" Then
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, _
Filename:=myFile, _
Quality:=xlQualityStandard, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=False
'confirmation message with file info
MsgBox "PDF file has been created: " _
& vbCrLf _
& myFile
End If
exitHandler:
Exit Sub
errHandler:
MsgBox "Could not create PDF file"
Resume exitHandler
End Sub

Resources