Option Explicit
Sub FileCopier()
Dim rngNames As Range
Dim sOriginalFilepathNameExt As String
Dim sOutputFilePath As String
Dim sOutput As String
Dim lNameCount As LongPtr
Dim rngCell As Range
Dim sExt As String
Dim lCopyCount As Long
Dim lBadnameCount As Long
Dim sTestFileNameExt As String
Dim iFreeFile As Integer
Set rngNames = Worksheets("Sheet1").Range("A1").CurrentRegion
lNameCount = rngNames.CurrentRegion.Cells.Count
sOriginalFilepathNameExt = "C:\Users\philip.bornemeier\Documents\-- Excel Processing\MrE\Build Calendar.xlsb"
sExt = Mid(sOriginalFilepathNameExt, InStrRev(sOriginalFilepathNameExt, "."))
sTestFileNameExt = "xyzzyTemp" & sExt
sOutputFilePath = "C:\Users\philip.bornemeier\Documents\-- Excel Processing\MrE\1\"
sOutputFilePath = FixPath(sOutputFilePath)
'Check for missing source file
If Dir(sOriginalFilepathNameExt, vbNormal) = vbNullString Then
sOutput = sOutput & vbLf & "Source File: " & sOriginalFilepathNameExt & " does not exist"
End If
'Check for missing output path
If Dir(sOriginalFilepathNameExt, vbDirectory) = vbNullString Then
sOutput = sOutput & vbLf & "Output Path: " & sOutputFilePath & " does not exist"
End If
'Create a dummy file for name testing
iFreeFile = FreeFile
Open ThisWorkbook.Path & "\" & sTestFileNameExt For Output As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFreeFile]#iFreeFile[/URL]
Write [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , "Hello World", 234 ' Write comma-delimited data.
Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=iFreeFile]#iFreeFile[/URL]
'validate proposed filenames by creating & deleting files with target names and source extension
rngNames.Cells.Interior.Color = xlNone 'Clear Color from cells
On Error Resume Next
For Each rngCell In rngNames.Cells
DoEvents
FileCopy ThisWorkbook.Path & "" & sTestFileNameExt, ThisWorkbook.Path & "" & rngCell.Value & sExt
DoEvents
Kill ThisWorkbook.Path & "" & rngCell.Value & sExt
DoEvents
If Err.Number <> 0 Then
rngCell.Interior.Color = rgbYellow
lBadnameCount = lBadnameCount + 1
Err.Clear
End If
Debug.Print
Next
Kill ThisWorkbook.Path & "" & sTestFileNameExt
'Any bad names found?
If lBadnameCount > 0 Then
sOutput = sOutput & vbLf & "There " & IIf(lBadnameCount = 1, "is ", "are ") & lBadnameCount & " invalid filename" & IIf(lBadnameCount = 1, "", "s") & " listed. " & _
IIf(lBadnameCount = 1, "This cell has", "These cells have") & " been colored yellow and must be corrected before continuing."
End If
'Were any errors found
If Len(sOutput) > 0 Then
sOutput = Mid(sOutput, 2)
MsgBox "The following problem(s) exist: " & vbLf & vbLf & sOutput & vbLf & vbLf & _
"Correct problem(s) and try again.", , "Problem(s) Found"
GoTo End_Sub
End If
'To be thorough there should be a section here to check for duplicate or blank cells, to make
' sure the entered name was a valid filename, to make sure the file did not already exist,
' but I will leave that for your enjoyment
Select Case MsgBox("Ready to make " & lNameCount & " copies of the file?", vbYesNo, "Make Copies?")
Case vbYes
For Each rngCell In rngNames.Cells
FileCopy sOriginalFilepathNameExt, sOutputFilePath & rngCell.Value & sExt
lCopyCount = lCopyCount + 1
Next
MsgBox lCopyCount & " copies made.", , "Coping Completed"
Case Else
MsgBox "Copy Cancelled", , "User Cancelled Copy"
End Select
End_Sub:
End Sub
Function FixPath(sPath) As String
'Ensure that the sPath path ends in a single path separator
' and does not contain multiple sequential path separators
Dim sPathSep As String
Dim lX As Long
sPathSep = Application.PathSeparator
sPath = Trim(sPath)
Do While Right(sPath, 1) = sPathSep
sPath = Left(sPath, Len(sPath) - 1)
Loop
For lX = Len(sPath) - 1 To 2 Step -1
If Mid(sPath, lX, 1) = sPathSep And Mid(sPath, lX + 1, 1) = sPathSep Then
sPath = Left(sPath, lX - 1) & Mid(sPath, lX + 1)
End If
Next
sPath = sPath & sPathSep
FixPath = sPath
End Function