Excel VBA 開啟對話方塊,獲取資料夾路徑
阿新 • • 發佈:2019-01-01
Excel 開啟對話方塊,獲取資料夾路徑
'Window「參照」ダイアログで選択したItem 名を取得 Public Function GetFolderName(ByVal DialogType As MsoFileDialogType) As String With Application.FileDialog(DialogType) If .Show = True Then GetFolderName = .SelectedItems(1) End If End With End Function Public Sub setInputAndOutput() Dim inFolder As String: inFolder = GetFolderName(msoFileDialogFolderPicker) Dim outFolder As String ActiveSheet.Range("D4") = inFolder Dim indexOfIN As Integer indexOfIN = InStr(inFolder, "\In") If indexOfIN > 0 Then outFolder = Mid(inFolder, 1, indexOfIN) & "Out" ActiveSheet.Range("D12") = outFolder Range("I2:N15").Select Selection.ClearContents Range("A1").Select Else TOOL_FILE_NAME = ThisWorkbook.Name TOOL_FILE_SHEET_NAME = "ツール" Set ERROR_INFO_LIST = New Collection ERROR_INFO_LIST.Add ("input:inputの選択不正確、" & "inputに対して、In フォルダーを選択ください。") Call setErrorInfoToExcel Set ERROR_INFO_LIST = New Collection Range("A1").Select End If End Sub
以上
具體用法
Application.FileDialog(fileDialogType)
fileDialogType MsoFileDialogType 型別,必需。檔案對話方塊的型別。
MsoFileDialogType 可為以下 MsoFileDialogType 常量之一。
・允許使用者選擇檔案。 msoFileDialogFilePicker
・允許使用者選擇一個資料夾。 msoFileDialogFolderPicker
・允許使用者開啟檔案。 msoFileDialogOpen
・允許使用者儲存一個檔案。 msoFileDialogSaveAs