我有很多名称不同的excel模板。其中之一称为 griep-weerstand v4.xlsb 。但是我的问题是关于所有模板的。
我想将文件名集成到保存脚本中,该脚本设置默认保存目录和默认保存名称。两者的名称中都有空格。添加正确数量的引号后,正确设置了默认的保存目录,但是,我一直在努力将工作簿名称添加到脚本中。我尝试了几种方法,但到目前为止都没有奏效。
默认保存目录为: M:\ Commercie \ Marktdata \ IRi \ Segment ontwikkeling
默认文件名(在此示例中)为: griep-weerstand v4.xlsb
以下是我最后尝试的内容:
Sub save_workbook_name()
Dim workbook_Name As Variant
Dim location As String
Dim workbookname As String
Dim workbookdirectory As String
Dim correctfilename As Variant
workbookname = ActiveWorkbook.Name
workbookdirectory = "M:\Commercie\Marktdata\IRi\Segment ontwikkeling\"
correctfilename = """M:\Commercie\Marktdata\IRi\Segment ontwikkeling\" & workbookname & """"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", initialfilename:=correctfilename)
If workbook_Name <> False Then
ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
End If
End Sub
我认为将工作簿名称作为字符串读取,并在合并的initialfilename
中添加正确的引号即可解决问题。
当我添加一个显示合并结果的消息框时,得到正确的路径和名称:
但是我继续在正确的目录中保存另存为对话框屏幕,但是在getsaveasfilename行中使用此文件名时却没有文件名。
如何获取目录和文件名的组合以与getsaveasfilename
一起使用?还是应该在activeworkbook.saveas
脚本中使用它?
答案 0 :(得分:1)
我不知道我是否正确理解您,但是您可以尝试执行以下操作:
Dim actWb As Workbook
Dim newName As String
Set actWb = ActiveWorkbook
newName = "M:\Commercie\Marktdata\IRi\Segment ontwikkeling\" & actWb.Name
actWb.SaveAs newName
答案 1 :(得分:1)
似乎有点复杂-您是否尝试过以下更简单的方法:
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb),
*.xlsb", initialfilename:=workbookdirectory & ActiveWorkbook.Name)
基于此处示例的完整代码:..它完全可以为您工作(使用包含空格的文件名进行测试)
Sub save_workbook_name()
Dim workbook_Name As Variant
Dim location As String
Dim workbookname As String
Dim workbookdirectory As String
Dim correctfilename As Variant
'workbookname = ActiveWorkbook.Name
workbookdirectory = "C:\Users\myusername\folder with spaces too\"
'correctfilename = """M:\Commercie\Marktdata\IRi\Segment ontwikkeling\" & workbookname & """"
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:=workbookdirectory & ActiveWorkbook.Name)
If workbook_Name <> False Then
ActiveWorkbook.SaveAs Filename:=workbook_Name, FileFormat:=50
End If
End Sub