cocopops2001
Board Regular
- Joined
- Apr 18, 2011
- Messages
- 112
I have this peice of code that lets the user choose which folder they want to create a new workbook in. it is called by a button on a userform and seems to work well but for some reason it seems to call open itself when i click a button. basically for it to choose a folder, exit the form with X or click cancel i have to push the button twice. not sure why?
Code:
Function GetFolder(Optional startFolder As Variant = -1) As Variant
Dim fldr As FileDialog
Dim vItem As Variant
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
If startFolder = -1 Then
.InitialFileName = ThisWorkbook.Path
Else
If Right(startFolder, 1) <> "\" Then
.InitialFileName = startFolder & "\"
Else
.InitialFileName = startFolder
End If
End If
If .show <> -1 Then GoTo NextCode
vItem = .SelectedItems(1)
End With
NextCode:
GetFolder = vItem
Set fldr = Nothing
End Function