gpt4 book ai didi

vba - VBA 中 .GetOpenFileName 的多选错误

转载 作者:行者123 更新时间:2023-12-02 01:28:40 25 4
gpt4 key购买 nike

我在 excel 2010 VBA (7.0) 上创建了一个用户表单,它将传输用户通过.GetOpenFileName 属性选择的文件。然后将所选文件的文件路径插入到 ListBox

我的问题是目前我正在尝试使用 MultiSelect,但是当我为 .GetOpenFileName 提供 Multiselect 属性来发送我的 ListBox 的文件路径(启用了多行) 我遇到了 GetOpenFileName 代码行显示的类型不匹配错误。代码示例如下:

Private Sub CommandButton1_Click ()
Dim strFilePath As String

StrFilePath = Application.GetOpenFilename (,,,, MultiSelect:= True)
If strFilePath = "False" Then Exit Sub

FilesFrom.Value = strFilePath

End Sub

FilesFrom 是我想要进入的文件路径的列表框。我的代码可以允许用户选择单个文件并传输该文件,但它不允许我用多个文件路径填充此列表框。

关于如何允许用户选择多个文件并将文件路径插入名为 FilesFrom 的列表框的任何想法?

最佳答案

问题是 MultiSelect 返回一个 Array

下面的代码应该正是您想要的。它适合多选或单选。

 Private Sub CommandButton1_Click()
'GetOpenFile MultiSelect will return an Array if more than one is selected
Dim FilePathArray As Variant
FilePathArray = Application.GetOpenFilename(, , , , MultiSelect:=True)

If IsArray(FilePathArray) Then

Dim ArraySize As Long
ArraySize = UBound(FilePathArray, 1) - LBound(FilePathArray, 1) + 1

Dim ArrayPosition As Long
For ArrayPosition = 1 To ArraySize

If Not FilePathArray(ArrayPosition) = Empty Then
'Replace "UserForm1" with the name of your Userform
UserForm1.FilesFrom.AddItem (FilePathArray(ArrayPosition))
End If

Next ArrayPosition

ElseIf FilePathArray <> False Then

'Replace "UserForm1" with the name of your Userform
UserForm1.FilesFrom.AddItem (FilePathArray)

End If
End Sub

关于vba - VBA 中 .GetOpenFileName 的多选错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35626944/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com