I would like to store my files in newly created folders at the same time. Unfortunately, the current code:
Dim City as Range
Dim Saverng as Range
Dim PathName As String
Dim FolderName As String
Set Target = ActiveCell
SelectedRow = Target.Row
Set City = cstws.Range("L" & SelectedRow)
Set Saverng = cstws.Range("K" & SelectedRow)
PathName = ThisWorkbook.path & "\test\"
FolderName = UCase(City)
If Dir(PathName & FolderName, vbDirectory) = "" Then
MkDir PathName & FolderName
Else
MsgBox "The folder " & FolderName & " already exists"
End If
Set wkb = Workbooks.Add
With wkb
.SaveAs filename:=PathName & FolderName & "\" & Saverng & " - Pre-Survey Template V1.1.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
doesn't allow on it. It keep saying that my directory doesn't exist.
I guess some silly error is here, how can I save files under the newly created directory then?
UPDATE:
My current code looks as follows:
PathName = ThisWorkbook.path & "\test\"
FolderName = UCase(City)
If FolderName = vbNullString Then
If City = "" Then
MsgBox ("What is the Site Address City?")
Exit Sub
Else
MkDir (PathName & FolderName)
End If
Else
MsgBox ("The Folder " & UCase(City) & " already exists")
End If
Despite the lack of a folder, I get the info, that my folder already exists afterward I am unable to save the file in the directory as shown earlier.
UPDATE II:
Now my code looks like this:
PathName = Application.ThisWorkbook.path & "\test\"
FolderName = UCase(City)
If City = "" Then
MsgBox ("What is the Site Address City?")
Exit Sub
End If
If Dir(PathName & FolderName, vbDirectory) = "" Then
MkDir (PathName & FolderName)
Else
MsgBox ("The Folder " & UCase(City) & " already exists")
End If
Set wkb = Workbooks.Add
With wkb
.SaveAs filename:=PathName & FolderName & "\" & WAddress & " - Pre-Survey Template V1.1.xlsm", FileFormat:=xlOpenXMLWorkbookMacroEnabled
And the folder is being created, but I get the 1004 error:
Run-time error 1004 The file could not be accessed....
Basically, I can store just one file there, but every other throws an error like the above.