【问题标题】:Folder Picker Excel VBA & paste Path to Cell文件夹选择器 Excel VBA 和粘贴路径到单元格
【发布时间】:2018-11-01 17:12:34
【问题描述】:

我很难弄清楚如何将文件夹路径放在单元格 C49 中。我希望有路径供用户了解他们在哪里搜索以及他们是否必须更改所述路径。

我得到了这个 VBA 代码, http://learnexcelmacro.com/wp/2016/12/how-to-open-file-explorer-in-vba/

Private Sub cmd_button_BROWSEforFolder_Click()

    On Error GoTo err
    Dim fileExplorer As FileDialog
    Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)

    'To allow or disable to multi select
    fileExplorer.AllowMultiSelect = False

    With fileExplorer
        If .Show = -1 Then 'Any folder is selected
            [folderPath] = .SelectedItems.Item(1)
            ThisWorkbook.Sheets("Home").Range("C49") = .SelectedItems.Item(1)
        Else ' else dialog is cancelled
            MsgBox "You have cancelled the dialogue"
            [folderPath] = "" ' when cancelled set blank as file path.
        End If
        End With
err:
    Exit Sub

End Sub

我已经尝试重新安排,

ThisWorkbook.Sheets("Home").Range("C49") = .SelectedItems.Item(1)

并尝试改变

.SelectedItems.Item(1)

到, [文件夹路径] 没有占上风。

我错过了什么? 我只需要显示在 txtbox 上方的路径,如果需要更改,则用户使用按钮重定向搜索。 (此按钮不会启动搜索宏)

【问题讨论】:

  • @Marcucciboy2 感谢您的回复,但我不确定您评论的链接中我应该使用什么。我在想我必须将 Selected Folder 变成 String Dim'd 对象,然后链接 .range ("C49 ") = Selected Folder
  • 是的,我的建议更多的是替代您上面的代码,而不是修复[文件夹路径]的示例

标签: excel vba path filedialog


【解决方案1】:
Private Sub cmd_button_BROWSEforFolder_Click()

    On Error GoTo err
    Dim fileExplorer As FileDialog
    Set fileExplorer = Application.FileDialog(msoFileDialogFolderPicker)
    Dim folderPath As String

    'To allow or disable to multi select
    fileExplorer.AllowMultiSelect = False

    With fileExplorer
        If .Show = -1 Then 'Any folder is selected
            folderPath = .SelectedItems.Item(1)

        Else ' else dialog is cancelled
            MsgBox "You have cancelled the dialogue"
            folderPath = "NONE" ' when cancelled set blank as file path.
        End If
    End With
err:

ThisWorkbook.Sheets("Home").Range("C49") = folderPath

End Sub

【讨论】:

    猜你喜欢
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    • 1970-01-01
    • 2023-01-19
    相关资源
    最近更新 更多