Adding in error handling

jr0124

New Member
Joined
Dec 28, 2016
Messages
12
Thank you in advance for your help!

I have the code for saving my file working the way I'd like it to as long as the path already exists. When I share with my team though, they may have named their folder something different. What I'd like to do is add in error handling where if the path needed does not exist it goes to the users 'my documents' folder. I am still learning VBA but whenever I try to add it in I just end up with more errors. Thoughts?

VBA Code:
Sub saveaswithlocationwithusernamewitherrorhandling()

    Dim file_name As Variant

    Dim fName As String

    ChDrive "C"

    ChDir "C:\Users\" & Environ("username") & "\Documents\Job Folders - 2020\" & Range("C13")

    fName = "Offer Checklist - " & Range("C26").Value & " " & Range("C27").Value & " - " & Range("C13").Value

 

    ' Get the file name.

    file_name = Application.GetSaveAsFilename(fName, _

                                              Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,All Files,*.*", _

                                              Title:="Save As File Name")

    ' See if the user canceled.

    If file_name = False Then Exit Sub

    ' Save the file with the new name.

    If LCase$(Right$(file_name, 5)) <> ".xlsm" Then

        file_name = file_name & ".xlsm"

    End If

    ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52

   

    

End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,623
Office Version
  1. 2007
Platform
  1. Windows
How about:

VBA Code:
Sub saveaswithlocationwithusernamewitherrorhandling()
  Dim file_name As Variant, fName As String, sPath1 As String
  ChDrive "C"
  sPath1 = "C:\Users\" & Environ("username") & "\Documents\Job Folders - 2020\" & Range("C13")
  If Dir(sPath1, vbDirectory) = "" Then
    sPath1 = "C:\Users\" & Environ("username") & "\Documents"
  End If
  ChDir sPath1
  '
  fName = "Offer Checklist - " & Range("C26").Value & " " & Range("C27").Value & " - " & Range("C13").Value
  ' Get the file name.
  file_name = Application.GetSaveAsFilename(fName, _
              Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,All Files,*.*", _
              Title:="Save As File Name")
  ' See if the user canceled.
  If file_name = False Then Exit Sub
  ' Save the file with the new name.
  If LCase$(Right$(file_name, 5)) <> ".xlsm" Then
      file_name = file_name & ".xlsm"
  End If
  ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52
End Sub
 

jr0124

New Member
Joined
Dec 28, 2016
Messages
12
How about:

VBA Code:
Sub saveaswithlocationwithusernamewitherrorhandling()
  Dim file_name As Variant, fName As String, sPath1 As String
  ChDrive "C"
  sPath1 = "C:\Users\" & Environ("username") & "\Documents\Job Folders - 2020\" & Range("C13")
  If Dir(sPath1, vbDirectory) = "" Then
    sPath1 = "C:\Users\" & Environ("username") & "\Documents"
  End If
  ChDir sPath1
  '
  fName = "Offer Checklist - " & Range("C26").Value & " " & Range("C27").Value & " - " & Range("C13").Value
  ' Get the file name.
  file_name = Application.GetSaveAsFilename(fName, _
              Filefilter:="Excel Macro-Enabled Workbook,*.xlsm,All Files,*.*", _
              Title:="Save As File Name")
  ' See if the user canceled.
  If file_name = False Then Exit Sub
  ' Save the file with the new name.
  If LCase$(Right$(file_name, 5)) <> ".xlsm" Then
      file_name = file_name & ".xlsm"
  End If
  ActiveWorkbook.SaveAs Filename:=file_name, FileFormat:=52
End Sub

That worked! Would you be able to help with this one so that if they hit 'yes' but the director is not there that it just goes to the end?

VBA Code:
Sub Path_Exists()

 

Dim Path As String

Dim Folder As String

Dim Answer As VbMsgBoxResult

 

    Path = "C:\Users\" & Environ("username") & "\Documents\Job Folders - 2020\" & Range("C13")

 

    Folder = Dir(Path, vbDirectory)

    If Folder = vbNullString Then

 

        Answer = MsgBox("Path does not exist. Would you like to create it?", vbYesNo, "Create Path?")

 

        Select Case Answer

            Case vbYes

                VBA.FileSystem.MkDir (Path)

            Case Else

                Exit Sub

        End Select

 

    Else

 

    End If

   

 

End Sub

VBA Code:
[CODE=vba]
[/CODE]
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,623
Office Version
  1. 2007
Platform
  1. Windows
Would you be able to help with this one so that if they hit 'yes' but the director is not there that it just goes to the end?
I do not understand what do you need.
 

Watch MrExcel Video

Forum statistics

Threads
1,130,169
Messages
5,640,544
Members
417,151
Latest member
ChickenTenderer

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top