Adding in error handling

jr0124

New Member
Joined
Dec 28, 2016
Messages
16
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

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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
 
Upvote 0
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]
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,214,382
Messages
6,119,194
Members
448,874
Latest member
Lancelots

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
Back
Top