Excel 如何将目录选择器添加到宏

Excel 如何将目录选择器添加到宏,excel,directory,vba,Excel,Directory,Vba,我不想硬编码目录,而是想打开一个目录选择器功能,以便用户可以为以下内容选择源文件夹和目标文件夹: Sub XlsToTxt() Dim aFile As String Const SourceFolder = "C:\Users\Documents\PCS\" ' note the backslash at the end of the string Const targetFolder = "C:\Users\Desktop\PCS Text\" ' note the

我不想硬编码目录,而是想打开一个目录选择器功能,以便用户可以为以下内容选择源文件夹和目标文件夹:

Sub XlsToTxt()
    Dim aFile As String
    Const SourceFolder = "C:\Users\Documents\PCS\" ' note the backslash at the end of the string
    Const targetFolder = "C:\Users\Desktop\PCS Text\" ' note the backslash at the end of the string
    Application.DisplayAlerts = False
    aFile = Dir(SourceFolder & "*.xls")
    Do While aFile <> ""
        Workbooks.Open SourceFolder & aFile
        ActiveWorkbook.SaveAs targetFolder & Left(aFile, Len(aFile) - 4) _
        & ".csv", FileFormat:=xlCSV _
        , CreateBackup:=False
        ActiveWorkbook.Close
        aFile = Dir
    Loop
    Application.DisplayAlerts = True
End Sub
Sub-XlsToTxt()
像线一样模糊
Const SourceFolder=“C:\Users\Documents\PCS\”注意字符串末尾的反斜杠
Const targetFolder=“C:\Users\Desktop\PCS Text\”,注意字符串末尾的反斜杠
Application.DisplayAlerts=False
aFile=Dir(SourceFolder&“*.xls”)
在文件“”中执行
工作簿。打开SourceFolder和文件(&A)
ActiveWorkbook.SaveAs targetFolder&左(aFile,Len(aFile)-4)_
&“.csv”,文件格式:=xlCSV_
,CreateBackup:=False
活动工作簿。关闭
aFile=Dir
环
Application.DisplayAlerts=True
端接头

此:
fnameandpath=Application.GetOpenFilename(标题:=“选择文件”)

将打开一个文件选择器对话框,供用户选择源文件和目标文件。他们可以正常浏览,选择文件时会返回完整路径和文件名进行处理

编辑 要添加筛选器-
fnameandpath=Application.GetOpenFilename(FileFilter:=“Excel文件(*.xls),*.xls”,Title:=“选择文件”)

请尝试以下方法:

Sub XlsToTxt()
    Dim aFile As String
    Dim SourceFolder As String
    Dim targetFolder As String

    With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Title = "Select Source folder"
      .Show
      On Error Resume Next
      SourceFolder = .SelectedItems(1) & "\"
      On Error GoTo 0
    End With
    With Application.FileDialog(msoFileDialogFolderPicker)
      .AllowMultiSelect = False
      .Title = "Select Target folder"
      .Show
      On Error Resume Next
      targetFolder = .SelectedItems(1) & "\"
      On Error GoTo 0
    End With

    If SourceFolder = "" Or targetFolder = "" Then Exit Sub

    Application.DisplayAlerts = False
    aFile = Dir(SourceFolder & "*.xls")
    Do While aFile <> ""
        Workbooks.Open SourceFolder & aFile
        ActiveWorkbook.SaveAs targetFolder & Left(aFile, Len(aFile) - 4) _
        & ".csv", FileFormat:=xlCSV _
        , CreateBackup:=False
        ActiveWorkbook.Close
        aFile = Dir
    Loop
    Application.DisplayAlerts = True
End Sub
Sub-XlsToTxt()
像线一样模糊
将SourceFolder设置为字符串
将targetFolder设置为字符串
使用Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect=False
.Title=“选择源文件夹”
显示
出错时继续下一步
SourceFolder=.SelectedItems(1)和“\”
错误转到0
以
使用Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect=False
.Title=“选择目标文件夹”
显示
出错时继续下一步
targetFolder=.SelectedItems(1)和“\”
错误转到0
以
如果SourceFolder=“”或targetFolder=“”,则退出Sub
Application.DisplayAlerts=False
aFile=Dir(SourceFolder&“*.xls”)
在文件“”中执行
工作簿。打开SourceFolder和文件(&A)
ActiveWorkbook.SaveAs targetFolder&左(aFile,Len(aFile)-4)_
&“.csv”,文件格式:=xlCSV_
,CreateBackup:=False
活动工作簿。关闭
aFile=Dir
环
Application.DisplayAlerts=True
端接头
见帖子。