VBA Excel使用灵活名称保存.xlsx文件 [英] VBA Excel save .xlsx file with the flexible name
问题描述
我想用一个灵活的名称保存我的文件,该名称会随单元格值的变化而变化.
一个答案在这里:
但是,我还想要名称的固定部分,该名称不会像上面查询中描述的部分一样发生变化.
基于此解决方案,我尝试编写如下内容:
Sub Save()昏暗的名字作为字符串,Custom_Name作为字符串名称=范围("A2").值Custom_Name ="NBU"&姓名&-机会列表.xlsx"ActiveWorkbook.SaveAs文件名:= Custom_Name
实际上,我得到一个错误:
此扩展名不能与所选文件类型一起使用.在文件名"文本框中更改文件扩展名,或通过更改另存为"类型来选择其他类型的文件.
我希望将此文件以.xlsx扩展名保存.
以上答案与我的情况不符.尽可能删除新保存文件中的所有表单控制按钮至关重要.
感谢&
结束子
列出的例程中没有用于保存文件的操作.它只是简单地获取单元格的内容,并创建一个带有包装值的字符串.
我不确定您的目标是什么,但是您需要从提供的第二个链接中添加操作. Workbook.SaveAs方法.
有关我创建的要测试的工作示例,请参见下面的代码.
Public Sub Save()昏暗的名字作为字符串,Custom_Name作为字符串名称=范围("A2").值Custom_Name = ThisWorkbook.Path&"\"&"NBU"和姓名&-机会list.xlsx"'保存时禁用警报Application.DisplayAlerts = False'保存工作簿.ActiveWorkbook.SaveAs文件名:= Custom_Name,文件格式:= 51结束子
您应该注意,执行此代码后,您现在将位于新创建的文件中.这不是出口.
对此进行测试,如果您有任何疑问,请告诉我.您的代码中似乎有一些不必要的事情,但是如果您找到了第一个问题,我们可以解决.
我还要专门列出该范围的工作表.
名称= Worksheets("Sheet1").Range("A2")
I would like to save my file with the flexible name, which will change as the cell value changes.
The one answer is here:
Save a file with a name that corresponds to a cell value
however, I want also some fixed part of the name, which won't change unlike the part described in the query above.
Basing on this solution I tried to write something as follows:
Sub Save ()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = "NBU" & name & "- Opportunity list.xlsx"
ActiveWorkbook.SaveAs Filename:=Custom_Name
In the effect, I am getting an error:
This extension cannot be used with the selected file type. Change the file extension in the File name text box or select a different type file by changing the Save as type.
I would like to have this file in the .xlsx extension.
Excel VBA - save as with .xlsx extension
The answer above doesn't really match to my situation. It will be vital to remove all form control buttons in the newly saved file, when possible.
Thanks & Regards,
End Sub
There is no action in the routine listed to save the file. It just simply takes the contents of a cell and creates a string with wrapped values.
I am not totally sure of what your goal is, but you need to add the action from the second link you provided. Workbook.SaveAs Method.
See the code below for a working example that I created to test.
Public Sub Save()
Dim name As String, Custom_Name As String
name = Range("A2").Value
Custom_Name = ThisWorkbook.Path & "\" & "NBU" & name & " - Opportunity list.xlsx"
'Disable alert when saving
Application.DisplayAlerts = False
'Save the workbook.
ActiveWorkbook.SaveAs Filename:=Custom_Name, FileFormat:=51
End Sub
You should note that after this code has executed, you will now be in the newly created file. This is not an export.
Test this and let me know if you have any questions. There are a few things that seem to be unnecessary in your code, but we can address those if you find this answers your first issue.
Edit:
I would also call out specifically then worksheet with the range as well.
name = Worksheets("Sheet1").Range("A2")
这篇关于VBA Excel使用灵活名称保存.xlsx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!