Hi there,
See if the following code does what you need. It contains two options:
(a) The "SaveActiveWorksheetInCommonFolder" routine will save the active worksheet using a common filename prefix plus the suffix contained in Cell J6 of the "ECN" worksheet. All files will be saved in the same folder "S:\Quality Control\Macro testing\ECR Workflow\ECN"
(b) The "SaveActiveWorksheetInIndividualFolder" routine will save the active worksheet as described above, but the file will be saved in folder "S:\Quality Control\Macro testing\ECR Workflow\ECN\ECN-" plus the suffix from Cell J6
The code used is as follows:
Option Explicit
'=========================================================================================
'=========================================================================================
Sub SaveActiveWorksheetInCommonFolder()
Const sFILENAME_PREFIX As String = "ECN-"
Const sFOLDER_PATH As String = "S:\Quality Control\Macro testing\ECR Workflow\ECN"
Const sSUFFIX_CELL As String = "J6"
Const sSHEET_NAME As String = "ECN"
Dim sFileNameSuffix As String
Dim sFullName As String
' Check whether or not the required folder already exists
If mbFolderExists(sFolderPath:=sFOLDER_PATH) = True Then
' Determine the suffix to be appended to the file name
sFileNameSuffix = ThisWorkbook.Worksheets(sSHEET_NAME).Range(sSUFFIX_CELL).Value
' Determine the full name of the new file
sFullName = sFOLDER_PATH & "\" & sFILENAME_PREFIX & sFileNameSuffix
' Save the active worksheet in .pdf format under the above file name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFullName
End If
End Sub
'=========================================================================================
'=========================================================================================
Sub SaveActiveWorksheetInIndividualFolder()
Const sFOLDER_PREFIX As String = "S:\Quality Control\Macro testing\ECR Workflow\ECN\ECN-"
Const sFILENAME_PREFIX As String = "ECN-"
Const sSUFFIX_CELL As String = "J6"
Const sSHEET_NAME As String = "ECN"
Dim sFolderPath As String
Dim sFullName As String
Dim sSuffix As String
' Determine the suffix to be appended to the folder name and file name
sSuffix = ThisWorkbook.Worksheets(sSHEET_NAME).Range(sSUFFIX_CELL).Value
' Determine the name of the new folder
sFolderPath = sFOLDER_PREFIX & sSuffix
' Create the new folder if required
If mbFolderExists(sFolderPath:=sFolderPath) = True Then
' Determine the full name of the new file
sFullName = sFolderPath & "\" & sFILENAME_PREFIX & sSuffix
' Save the active worksheet in .pdf format under the above file name
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=sFullName
End If
End Sub
'=========================================================================================
'=========================================================================================
Private Function mbFolderExists(sFolderPath As String) As Boolean
Const sSLASH As String = "\"
Dim sFolderName As String
Dim vaFolders As Variant
Dim iFolderNo As Integer
On Error GoTo ErrorEncountered
' Check whether or not the folder already exists
If Dir$(sFolderPath, vbDirectory) = vbNullString Then
' Create an array containing the names of the various folders
vaFolders = Split(sFolderPath, sSLASH)
' Scan through the names of each of the folders in the above list
For iFolderNo = LBound(vaFolders) To UBound(vaFolders)
sFolderName = sFolderName & vaFolders(iFolderNo) & sSLASH
' Create the required folder if it does not already exist
If Dir$(sFolderName, vbDirectory) = vbNullString Then
MkDir sFolderName
End If
Next iFolderNo
End If
mbFolderExists = True
ExitPoint:
Exit Function
ErrorEncountered:
MsgBox "The folder """ & sFolderPath & """ could not be created", vbCritical
mbFolderExists = False
Resume ExitPoint
End Function
The highlighted values may be altered to suit your own requirements.
The code contains error handling to cater for the situation where (for whatever reason) one of the required folders cannot be created.
Hope this helps - please let me know how you get on.
Regards,
Greg M
Bookmarks