VBA文件对话框。显示重新启动子文件

VBA文件对话框。显示重新启动子文件,vba,excel,show,filedialog,Vba,Excel,Show,Filedialog,我正在尝试使用filedialog.show函数获取文件夹的路径 我面临的问题是: 在folderpicking窗口中选择文件夹后,代码不会继续。它要么重新启动,要么在没有任何事情发生的情况下结束。 有什么问题吗 [...] Dim fpath As Variant Dim fldr As Object Set fldr = Application.FileDialog(msoFileDialogFolderPicker) fldr.Title = "Select a Folder" fldr

我正在尝试使用filedialog.show函数获取文件夹的路径

我面临的问题是:

在folderpicking窗口中选择文件夹后,代码不会继续。它要么重新启动,要么在没有任何事情发生的情况下结束。 有什么问题吗

[...]

Dim fpath As Variant
Dim fldr As Object
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
fldr.Title = "Select a Folder"
fldr.AllowMultiSelect = False
fldr.InitialFileName = Application.DefaultFilePath
If fldr.Show = -1 Then
    fpath = fldr.SelectedItems(1)
Else
    GoTo NextCode
End If
NextCode: 
set fldr = Nothing

[...]

它可以工作,您只是不使用它来显示路径结果或从该子节点返回字符串值

更改您的代码:

If fldr.Show = -1 Then
    fpath = fldr.SelectedItems(1)
Else
    GoTo NextCode
End If

NextCode: 
set fldr = Nothing
致:

以及测试它的子代码:

Sub Test()

Dim FolderPath As String

FolderPath = GetFolderPath
MsgBox FolderPath

End Sub

代码对我有用。错误可能在于您显示为[…]的部分,这使得帮助您有点困难。显然,代码确实工作正常。只是因为我使用了调试步骤,它丢失了。我使用MsgBox作为检查点,并在代码中进一步使用了一个换行符,一切都进行得很顺利。
Function GetFolderPath() As String

Dim fpath As Variant
Dim fldr As Object

Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
    .Title = "Select a Folder"
    .AllowMultiSelect = False
    .InitialFileName = Application.DefaultFilePath

    If .Show = -1 Then
        GetFolderPath = .SelectedItems(1)
    End If
End With    
Set fldr = Nothing

End Function
Sub Test()

Dim FolderPath As String

FolderPath = GetFolderPath
MsgBox FolderPath

End Sub