title: VBA - Avoid error when cancel getfolder
body: <p>I am getting a basic error when I cancel the selection of a folder. I just want to Exit Sub when press the cancel button.</p>
  
  <p><a href="https://i.stack.imgur.com/m9oWg.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/m9oWg.png" alt="enter image description here"></a></p>
  
  <p>I'm using the following code</p>
  
  <pre><code>Set recsFolder = fso.GetFolder(Functions.GetFolder("C:\"))
  
  Function GetFolder(strPath As String) As String
  Dim Fldr As FileDialog
  Dim sItem As String
  Set Fldr = Application.FileDialog(msoFileDialogFolderPicker)
  With Fldr
      .Title = "Select a Folder"
      .AllowMultiSelect = False
      .InitialFileName = strPath
      If .Show &lt;&gt; -1 Then GoTo NextCode
      sItem = .SelectedItems(1)
  End With
  NextCode:
  GetFolder = sItem
  Set Fldr = Nothing
  End Function
  </code></pre>