无效的过程调用或参数 vba。运行时错误 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24720320/
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-12 03:50:01  来源:igfitidea点击:

Invalid procedure call or argument vba. Run time error 5

excel-vbavbaexcel

提问by Bart g

Every time I try to select only one file but I have .AlloMultiSelect = True There is an error message saying: Run-Time error '5':Invalid procedure call or argument and highlights "strPathAndSparks = .SelectedItems(2)". So my question is if I can enable multiselection and pick only one file, if so where is my mistake in this lines of code. This is what I have. I thank you all in advance.

每次我尝试只选择一个文件但我有 .AlloMultiSelect = True 有一条错误消息说:运行时错误'5':无效的过程调用或参数并突出显示“strPathAndSparks = .SelectedItems(2)”。所以我的问题是我是否可以启用多选并只选择一个文件,如果可以,我在这行代码中的错误在哪里。这就是我所拥有的。我先谢谢大家。

 strInitialDirectory = CurDir
 Set fd = Application.FileDialog(msoFileDialogFilePicker)

 With fd
    .Title = "Choose Jira File"
    .InitialFileName = CurDir & "\"
    .AllowMultiSelect = True
    .Filters.Clear
    .Filters.Add "Excel Files", "*.xls;*.xlsx;*.xlsm"

    If .Show = False Then
        ChDir (strInitialDirectory)
        Exit Sub
    End If

        strPathAndJira = .SelectedItems(1)
        strPathAndSparks = .SelectedItems(2)
 End With

回答by chris neilsen

The error occurs if the user selects 1 or fewer items. You cannot compelthe user to select 2 items, but you can test how many items wereselected with

如果用户选择 1 个或更少的项目,则会发生此错误。你不能强迫用户选择2项,但你可以考多少项目与选择

fd.SelectedItems.Count