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

Folder Picker Excel VBA & paste Path to Cell

我很难弄清楚如何将文件夹路径放入单元格 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)

为了, [文件夹路径] 没有优势。

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

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