Hi again,
I'm a great believer in breaking code down into bite-sized pieces - it's easier to maintain and it's usually easier to understand.
See if the following version of your code does what you need:
Option Explicit
'=========================================================================================
'=========================================================================================
Public Sub PerformEndOfMonthRoutine()
Dim sExportFileName As String
Dim wksActive As Worksheet
' Use a function to retrieve the FullName of the file to be exported
sExportFileName = msExportFileName()
' Continue only if the FullName has been retrieved
If sExportFileName <> vbNullString Then
' Store the currently-active worksheet so that it can be activated on completion
Set wksActive = ActiveSheet
Call ExportSheetsToPdf(sExportFileName:=sExportFileName)
Call ResetSheets
' Return to the original active worksheet
wksActive.Activate
End If
End Sub
'=========================================================================================
'=========================================================================================
Private Function msExportFileName() As String
Dim sExportFilePath As String
Dim sExportFileName As String
Dim dlgFolder As FileDialog
' Set the path for the Export file
Set dlgFolder = Application.FileDialog(msoFileDialogFolderPicker)
With dlgFolder
.Title = "Select Target Folder Containing Mandate Files"
.AllowMultiSelect = False
If .Show = -1 Then
sExportFilePath = .SelectedItems(1) & "\"
' Create the full Filename using indicated cells
With ThisWorkbook.Worksheets("TimeEnter")
msExportFileName = sExportFilePath & _
.Range("A3").Value & " " & .Range("B3").Value & _
".pdf"
End With
Else: msExportFileName = vbNullString
End If
End With
End Function
'=========================================================================================
'=========================================================================================
Private Sub ExportSheetsToPdf(sExportFileName As String)
Dim vaExportSheetNames As Variant
Dim vSheetName As Variant
Dim sSheetName As String
vaExportSheetNames = Array("Timesheet", "ExpenseSheet", "MileageSheet")
' Make the Export sheets visible
For Each vSheetName In vaExportSheetNames
sSheetName = CStr(vSheetName)
ThisWorkbook.Worksheets(sSheetName).Visible = xlSheetVisible
Next vSheetName
' Select the Export sheets and then save the array of worksheets as a PDF file
ThisWorkbook.Worksheets(vaExportSheetNames).Select
ActiveSheet.ExportAsFixedFormat Quality:=xlQualityStandard, _
Filename:=sExportFileName, _
IncludeDocProperties:=True, _
IgnorePrintAreas:=False, _
OpenAfterPublish:=True, _
Type:=xlTypePDF
' Hide the Export sheets now that the export operation has been completed
For Each vSheetName In vaExportSheetNames
sSheetName = CStr(vSheetName)
ThisWorkbook.Worksheets(sSheetName).Visible = xlSheetHidden
Next vSheetName
End Sub
'=========================================================================================
'=========================================================================================
Private Sub ResetSheets()
Dim vSheetName As Variant
Dim sSheetName As String
Dim chk As CheckBox
With ThisWorkbook
' Clear data values
.Worksheets("ExpenseEnter").Range("ClearExpense").ClearContents
.Worksheets("MilesEnter").Range("ClearMiles").ClearContents
.Worksheets("TimeEnter").Range("ClearTime").ClearContents
' Reset CheckBoxes
For Each vSheetName In Array("ExpenseEnter", "MilesEnter", "TimeEnter")
sSheetName = CStr(vSheetName)
For Each chk In .Worksheets(sSheetName).CheckBoxes
chk.Value = False
Next chk
Next vSheetName
End With
End Sub
Highlighted values can be altered to suit any future changes/requirements.
The trailing slash in the following statement isn't needed on my installation - you could experiment with omitting it:
sExportFilePath = .SelectedItems(1) & "\"
Hope this helps - as before, please let me know how you get on.
Regards,
Greg M
Bookmarks