簡體   English   中英

文件夾選擇器 Excel VBA 並將路徑粘貼到單元格

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

我很難弄清楚如何將文件夾路徑放在單元格 C49 中。 我希望有路徑供用戶了解他們正在搜索的位置以及他們是否必須更改所述路徑。

我從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

我試過重新排列的位置,

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

並嘗試改變

.SelectedItems.Item(1)

以,[folderPath] 以無為准。

我錯過了什么? 我只需要顯示在 txtbox 上方的路徑,如果需要更改,則用戶使用該按鈕重定向搜索。 (此按鈕不會啟動搜索宏) 在此處輸入圖片說明

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