通过VBA浏览将文件保存在所需文件夹中
编写代码,将具有定义文件名的文件保存到用户输入的特定文件夹中。但是,文件保存在指定位置之前的位置。例如,我提供的文件保存路径为“C:\Users\arorapr\Documents\PAT”,但文件保存在路径“C:\Users\arorapr\Documents”中。我已经写了下面的代码通过VBA浏览将文件保存在所需文件夹中,vba,excel,Vba,Excel,编写代码,将具有定义文件名的文件保存到用户输入的特定文件夹中。但是,文件保存在指定位置之前的位置。例如,我提供的文件保存路径为“C:\Users\arorapr\Documents\PAT”,但文件保存在路径“C:\Users\arorapr\Documents”中。我已经写了下面的代码 File_Name = Format(Now(), "DDMMYYYY") & "_" & LName & EmpIN & "_" & Range("C6").Valu
File_Name = Format(Now(), "DDMMYYYY") & "_" & LName & EmpIN & "_" & Range("C6").Value & "_" & Range("J3").Value & "_" & "PAT"
Application.DisplayAlerts = False
MsgBox "Please select the folder to save PAT"
With Application.FileDialog(msoFileDialogFolderPicker)
.AllowMultiSelect = False
.Show
End With
ActiveWorkbook.saveas Filename:=File_Name & ".xlsm", FileFormat:=52
Application.DisplayAlerts = True
ActiveWorkbook.Close
在代码中,您没有将所选文件夹的路径保存到变量中。在下面的代码中,路径保存到变量
selectedFolder
,该变量的值来自fldr.SelectedItems(1)
。然后保存路径+“\”+您的文件名和.xlsm
:
Option Explicit
Sub TestMe()
Dim fldr As FileDialog
Dim selectedFolder As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.Show
selectedFolder = .SelectedItems(1)
End With
ActiveWorkbook.SaveAs Filename:=selectedFolder & "\" & "YourFileName" & ".xlsm"
End Sub
或者,您也可以使用函数,从此处返回文件夹的路径:
我用来
GetFolder
的一个强大函数是:
Option Explicit
Sub myPathForFolder()
Debug.Print GetFolder(Environ("USERPROFILE"))
End Sub
Function GetFolder(Optional InitialLocation As String) As String
On Error GoTo GetFolder_Error
Dim FolderDialog As FileDialog
Dim SelectedFolder As String
If Len(InitialLocation) = 0 Then InitialLocation = ThisWorkbook.Path
Set FolderDialog = Excel.Application.FileDialog(msoFileDialogFolderPicker)
With FolderDialog
.Title = "My Title For Dialog"
.AllowMultiSelect = False
.InitialFileName = InitialLocation
If .Show <> -1 Then GoTo GetFolder_Error
SelectedFolder = .SelectedItems(1)
End With
GetFolder = SelectedFolder
On Error GoTo 0
Exit Function
GetFolder_Error:
Debug.Print "Error " & Err.Number & " (" & Err.Description & ")
End Function
选项显式
子myPathForFolder()
打印GetFolder(环境(“用户配置文件”))
端接头
函数GetFolder(可选初始位置为String)为String
On错误转到GetFolder\u错误
Dim FolderDialog作为文件对话框
将选定文件夹设置为字符串
如果Len(InitialLocation)=0,则InitialLocation=ThisWorkbook.Path
Set FolderDialog=Excel.Application.FileDialog(msoFileDialogFolderPicker)
使用FolderDialog
.Title=“对话框的我的标题”
.AllowMultiSelect=False
.InitialFileName=InitialLocation
如果.Show-1,则转到GetFolder\u错误
SelectedFolder=.SelectedItems(1)
以
GetFolder=SelectedFolder
错误转到0
退出功能
GetFolder\u错误:
调试。打印“错误”和错误号(“&Err.Description&”)
端函数
您面临的挑战是,您正在打开一个文件对话框,但没有使用用户在另存为中的选择。请尝试以下方法:
Sub SaveFile()
Dim FolderName As String
File_Name = Format(Now(), "DDMMYYYY") & "_" & LName & EmpIN & "_" & Range("C6").Value & "_" & Range("J3").Value & "_" & "PAT"
Application.DisplayAlerts = False
MsgBox "Please select the folder to save PAT"
' Pop up the folder-selection box to get the folder form the user:
FolderName = GetFolder()
' If the user didn't select anything, you can't save, so tell them so:
If FolderName = "" Then
MsgBox "No folder was selected. Program will terminate."
Exit Sub
End If
' Create a path by combining the file and folder names:
File_Name = FolderName & "\" & File_Name & ".xlsm"
ActiveWorkbook.SaveAs Filename:=File_Name, FileFormat:=52
Application.DisplayAlerts = True
ActiveWorkbook.Close
End Sub
' A separate function to get the folder name and return it as a string
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
子保存文件()
Dim FolderName作为字符串
文件名=格式(Now(),“DDMMYYYY”)&“&”LName&EmpIN&“&”Range(“C6”)。值&“&”Range(“J3”)。值&“&”和“PAT”
Application.DisplayAlerts=False
MsgBox“请选择要保存PAT的文件夹”
'弹出文件夹选择框以从用户处获取文件夹:
FolderName=GetFolder()
'如果用户没有选择任何内容,则无法保存,请告诉他们:
如果FolderName=“”,则
MsgBox“未选择任何文件夹。程序将终止。”
出口接头
如果结束
'通过组合文件名和文件夹名创建路径:
File\u Name=FolderName&“\”&File\u Name&“.xlsm”
ActiveWorkbook.SaveAs文件名:=文件名,文件格式:=52
Application.DisplayAlerts=True
活动工作簿。关闭
端接头
'一个单独的函数,用于获取文件夹名称并将其作为字符串返回
函数GetFolder()作为字符串
Dim fldr As FILE对话框
以字符串形式显示
设置fldr=Application.FileDialog(msoFileDialogFolderPicker)
与fldr
.Title=“选择一个文件夹”
.AllowMultiSelect=False
.InitialFileName=Application.DefaultFilePath
如果.Show-1,则转到下一个代码
sItem=.SelectedItems(1)
以
下一个代码:
GetFolder=sItem
设置fldr=无
端函数
希望这会有所帮助。您可以使用类似于If.SelectedItems.Count>0…
@CLR-Yup.的内容来捕获单击此处取消的用户,然后我可以放置错误处理程序和初始位置。一般来说,我正在考虑是否从此处开始提供整个函数,但我已决定参考SO问题。@perna arora-您很好来吧。一般来说,看看这些函数,它们可以帮你解决一些问题,而且非常健壮。