Dave,
It works, thanks. Now can this be expanded? I want it to create a directory,
let's say US 49, if it doesn't exist and then create a sub directory, let's
say Form 1257, if it doesn't exist then save the file. I can't figure out how
to do the IFs or what exactly I need to do. The following is some of the code
I have:
Function DirectoryExist(sstr As String)
Dim lngAttr As Long
DirectoryExist = False
If Dir(sstr, vbDirectory) <> "" Then
lngAttr = GetAttr(sstr)
If lngAttr And vbDirectory Then _
DirectoryExist = True
End If
End Function
Function DoesPathExist(myPath As String) As Boolean
Dim TestStr As String
If Right(myPath, 1) <> "\" Then
myPath = myPath & "\"
End If
TestStr = ""
On Error Resume Next
TestStr = Dir(myPath & "nul")
On Error GoTo 0
DoesPathExist = CBool(TestStr <> "")
End Function
Dim dirstr As String
Dim dirstr1 As String
dirstr = "U:\" & Range("hwy") & "\"
dirstr1 = "C:\" & Range("hwy") & "\"
Application.DisplayAlerts = False
If DoesPathExist("U:\") Then
If Not DirectoryExist(dirstr) Then
MkDir dirstr
myFileName = Range("file")
myFileName = "U:\" & myFileName & ".xls"
ActiveWorkbook.SaveAs Filename:= _
myFileName, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
Else
myFileName = Range("file")
myFileName = "U:\" & myFileName & ".xls"
ActiveWorkbook.SaveAs Filename:= _
myFileName, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End If
'put stuff on that drive
MsgBox "File Saved to " & myFileName
Else
If Not DirectoryExist(dirstr1) Then
MkDir dirstr1
myFileName = Range("file")
myFileName = "C:\" & myFileName & ".xls"
ActiveWorkbook.SaveAs Filename:= _
myFileName, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
Else
myFileName = Range("file")
myFileName = "C:\" & myFileName & ".xls"
ActiveWorkbook.SaveAs Filename:= _
myFileName, FileFormat:=xlNormal, _
Password:="", WriteResPassword:="", ReadOnlyRecommended:=False, _
CreateBackup:=False
End If
'put stuff on C: drive
MsgBox "File Saved to " & myFileName
End If
Application.DisplayAlerts = True
End Sub