vba 取消按钮应该退出子

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27077614/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:10:15  来源:igfitidea点击:

Cancel button should exit the sub

excelvba

提问by user2075017

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.

我使用了一个状态变量,并注意到在取消状态更改为 -1。因此,我尝试使用 if 条件来实现注释部分中的代码以退出子程序。

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

回答by Dustin

Keep in mind that vbFalse = 0 and vbTrue = -1. In other words clicking 'OK' would return -1 and clicking 'Cancel' would return 0.

请记住,vbFalse = 0 和 vbTrue = -1。换句话说,单击“确定”将返回 -1,单击“取消”将返回 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

回答by MrMyagi

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

我知道这已关闭,但想第一次尝试发帖。=D

回答by FCastro

If there are no items selected, *SelectedItems(1)*doesn't exist, and Excel will return an error. That's what's happening when the user presses the Cancelbutton.

如果没有选择的项目,*SelectedItems(1)*不存在,Excel 将返回错误。这就是用户按下取消按钮时发生的情况。

A solution for this is to check how many items are selected using the structure below:

对此的解决方案是使用以下结构检查选择了多少项:

With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    'Optional: limits the user to choosing a single option. Necessary if you want to avoid an error because the user selected multiple files.
    .Title = "Dialog Title" 'Changing the title is also Optional
    .Show
    If .SelectedItems.Count = 0 Then
        MsgBox "Canceled by user" 'or just do nothing!
    Else
        MyVar = .SelectedItems(1)
    End If
    'Alternatively, "if .selecteditems.count = 1 then myvar = .selecteditems(1)" can be used
End With