简体   繁体   English

文件夹选择器 Excel VBA 并将路径粘贴到单元格

[英]Folder Picker Excel VBA & paste Path to Cell

I am having difficulty figuring out how to put the Folder Path in Cell C49.我很难弄清楚如何将文件夹路径放在单元格 C49 中。 I'd like to have the Path there for the User to understand where they are searching and if they have to change said Path.我希望有路径供用户了解他们正在搜索的位置以及他们是否必须更改所述路径。

I got this VBA code from, http://learnexcelmacro.com/wp/2016/12/how-to-open-file-explorer-in-vba/我从http://learnexcelmacro.com/wp/2016/12/how-to-open-file-explorer-in-vba/得到这个 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

I've tried rearranging the location of,我试过重新排列的位置,

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

and tried changing并尝试改变

.SelectedItems.Item(1)

to, [folderPath] with no prevail.以,[folderPath] 以无为准。

what am I missing?我错过了什么? all I need is the path to be displayed above the txtbox and if it needs to be changed then the User used the button to redirect the search.我只需要显示在 txtbox 上方的路径,如果需要更改,则用户使用该按钮重定向搜索。 (this button will not initiate the search Macro) (此按钮不会启动搜索宏) 在此处输入图片说明

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM