Following this thread:
VBA Excel - problem with saving file on newly created folder
I've changed the circumstances slightly by adding an additional subfolder.
Unfortunately, it looks like now I am unable to save the excel file in the target directory, as VBA Excel keeps throwing the 1004 error, that the path wasn't found.
My code looks like this:
Dim BPcode As String, GPcode As String, PCity As String, Folder1 As String, Folder2 As String
Dim Target As Range
Dim SelectedRow As Long
Dim PathName As String, fileName As String
Set Target = ActiveCell
SelectedRow = Target.Row
Set WAddress = cstws.Range("K" & SelectedRow)
Set City = cstws.Range("L" & SelectedRow)
PathName = GetLocalPath(ThisWorkbook.path)
fileName = RemoveForbiddenFilenameChars(WAddress)
BPcode = Split(PCode, " ")(0)
GPcode = Remove_Number(BPcode)
Select Case GPcode
Case "CB"
PCity = "Cambridge"
Case "NN"
PCity = "Northampton"
End Select
Folder1 = RemoveForbiddenFilenameChars(UCase(PCity)) & " [" & GPcode & "]"
Folder2 = RemoveForbiddenFilenameChars(UCase(City))
If Folder2 = "" Then MsgBox "What is the Site Address City?", vbCritical: Exit Sub
If fileName = "" Then MsgBox "Address incorrect or not provided", vbCritical: Exit Sub
Dim NewFolderPath As String
NewFolderPath = PathName & Application.PathSeparator & Folder1
If Dir(NewFolderPath, vbDirectory) = "" Then
MkDir NewFolderPath
Else
MsgBox "The Folder " & Folder1 & " already exists"
End If
Dim NewSubFolderPath As String
NewSubFolderPath = NewFolderPath & Application.PathSeparator & Folder2
If Dir(NewSubFolderPath, vbDirectory) = "" Then
MkDir NewSubFolderPath
Else
MsgBox "The Folder " & Folder2 & " already exists"
End If
Set wkb = Workbooks.Add
With wkb
Application.DisplayAlerts = False
.SaveAs fileName:=NewSubFolderPath & "\" & fileName & "- file", FileFormat:=xlOpenXMLWorkbookMacroEnabled
I tried also with NewSubFolderPath & Application.PathSeparator & fileName & "-file", but unfortunately it didn't work either.
Is there any way for pointing out the error here? The saveAs
work just when the solely PathName is used. Otherwise, Error 1004 is thrown.