I have a dialog box to pick a folder name and display the name of the folder that the user selects.
If the user selects cancel instead of folder path and OK, it throws an error.
I used a status variable and noticed that upon cancel the status changes to -1. So I tried to implement the code that is in comment section using a if condition to exit the sub.
That doesn't work in the case of selecting a folder when the commented part is present in the code.
Without that it works in selecting a folder.
sub abc()
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Title = "Select a folder then hit OK"
diaFolder.Show
'Status = diaFolder.Show
'If Status < 0 Then
'Exit Sub
'End If
a = diaFolder.SelectedItems(1)
MsgBox ("Folder selected is :" & a)
ens sub
Keep in mind that vbFalse = 0 and vbTrue = -1. In other words clicking 'OK' would return -1 and clicking 'Cancel' would return 0.
Try the following code:
Sub abc()
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Title = "Select a folder then hit OK"
If .Show = -1 Then
MsgBox ("Folder selected is :" & .SelectedItems(1))
Else
Exit Sub
End If
End With
End Sub
Sub abc()
Set diaFolder = Application.FileDialog(msoFileDialogFolderPicker)
diaFolder.AllowMultiSelect = False
diaFolder.Title = "Select a folder then hit OK"
Dim status As Integer
status = diaFolder.Show
If status <> -1 Then
MsgBox "Cancel Chosen"
Exit Sub
End If
a = diaFolder.SelectedItems(1)
MsgBox ("Folder selected is :" & a)
End Sub
I know this is closed out but wanted to try posting for the first time. =D
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With