Hi Lewis
Your explanation makes sense but if I already have the code
sDestinationFolder = "C:\Users\Paul\Briefing Pack Selector\Output\"
How can I also have the code:
sDestinationFolder = sPathAndSubFolderName & "\"
This is the code I have in the project at present:
Option Explicit
Sub cmd_CopyPk1_By_Lewis()
Dim a As Integer
Dim iNumberOfFiles As Integer
Dim b As String
Dim sDestinationFolder As String
Dim sFileName As String
Dim sSourceFolder As String
Dim sSubFolderName As String
Dim sPathAndSubFolderName As String
sSourceFolder = "C:\Users\Paul\Briefing Pack Selector\Pack Contents\"
sDestinationFolder = "C:\Users\Paul\Briefing Pack Selector\Output\"
Sheets("Briefing Pack 1").Select
'Copy Source files to the 'Output Folder'
iNumberOfFiles = Cells(Rows.Count, 1).End(xlUp).Row
For a = 1 To iNumberOfFiles
sFileName = Cells(a, 1)
FileCopy sSourceFolder & sFileName, sDestinationFolder & sFileName
Next a
'**********************************************************
'Option 1 - Create a New Subfolder if it is a complete name (path and file) in Cell 'C3'
'**********************************************************
'Get the Subfolder name (including path)
sPathAndSubFolderName = Sheets("Briefing Pack Selector").Range("C3").Text
'Strip out leading and trailing spaces from the name
sPathAndSubFolderName = Trim(sPathAndSubFolderName)
'Make sure the name is NOT BLANK
If Len(sPathAndSubFolderName) = 0 Then
MsgBox "Terminating. The contents of cell 'C3' is BLANK"
'Exit (i.e. 'Exit Sub' or do some other kind of error processing)
End If
'Create the new SubFolder (if it doesn't already exist)
If LJMFolderExists(sPathAndSubFolderName) = False Then
MkDir sPathAndSubFolderName & "\"
If LJMFolderExists(sPathAndSubFolderName) = False Then
MsgBox "Error in creating Subfolder: " & vbCrLf & _
"Name: " & sPathAndSubFolderName
End If
End If
'**********************************************************
'Option 2 - Create a New Subfolder if it is not a completed path (.e.g. 'Archive')
'that is a subfolder of 'C:\Users\Paul\Briefing Pack Selector\Output\'
'**********************************************************
'Create the Subfolder name
'
'NOTE: I arbitrarily chose 'C4' for this file name - for example purposes only
sSubFolderName = Sheets("Briefing Pack Selector").Range("C3").Text
'Strip out leading and trailing spaces from the name
sPathAndSubFolderName = Trim(sPathAndSubFolderName)
'Make sure the name is NOT BLANK
If Len(sSubFolderName) = 0 Then
MsgBox "Terminating. The contents of cell 'C3' is BLANK"
'Exit (i.e. 'Exit Sub' or do some other kind of error processing)
End If
'Create the complete Sub Folder path and file name
'NOTE: 'sDestinationFolder' MUST have a trailing '\' (as it does in your case)
sPathAndSubFolderName = sDestinationFolder & sSubFolderName
'Create the new SubFolder (if it doesn't already exist)
If LJMFolderExists(sPathAndSubFolderName) = False Then
MkDir sPathAndSubFolderName & "\"
If LJMFolderExists(sPathAndSubFolderName) = False Then
MsgBox "Error in creating Subfolder: " & vbCrLf & _
"Name: " & sPathAndSubFolderName
End If
End If
MsgBox "Files have been copied to output folder"
Sheets("Briefing Pack Selector").Select
End Sub
Public Function LJMFolderExists(sPathAndFullFileName As String) As Boolean
'This returns TRUE if a folder exists and FALSE if a folder does NOT exist
'This will return FALSE if the 'sPathAndFullFileName' is a file
Dim iFileAttributes As Integer
On Error Resume Next
iFileAttributes = GetAttr(sPathAndFullFileName)
iFileAttributes = iFileAttributes And vbDirectory
On Error GoTo 0
LJMFolderExists = False
If iFileAttributes = vbDirectory Then
LJMFolderExists = True
End If
End Function
Thanks again
Paul
Bookmarks