用于重命名和保存的 VBA 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16879726/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:44:30  来源:igfitidea点击:

VBA Code to rename & save

vbaexcel-vbaexcel

提问by Paquita

I work in a sales office and currently we use hard copies of all sales quotes and file them alphabetically. I have made an excel version of our quote paperwork to hopefully make everything more efficient. Unfortunately we are not confident of everyone in the office's computer skills, so it needs to be made as user friendly as possible.

我在销售办公室工作,目前我们使用所有销售报价的硬拷贝并按字母顺序归档。我已经制作了我们报价文件的 excel 版本,希望能让一切变得更有效率。不幸的是,我们对办公室的每个人的计算机技能都没有信心,因此需要尽可能地使用户友好。

I need to use a command button through vba that will save the excel sheet to a specified path on our network & rename the copy as the cell content of a specified range of cells (if this is possible).

我需要通过 vba 使用一个命令按钮,它将 Excel 工作表保存到我们网络上的指定路径,并将副本重命名为指定单元格范围的单元格内容(如果可能的话)。

Basically for every quote I would like to have a saved copy of the sheet saved as "Doe.John.06.01.2013" if possible. I did not know vba existed until yesterday, and while i think it is amazing and it made my inner nerd jump for joy, i am a code NOOB!...i have researched online and been unable to find anything like what I need. If anyone on here can help me, i will be eternally grateful :)

基本上对于每个报价,如果可能的话,我希望将工作表的已保存副本另存为“Doe.John.06.01.2013”​​。直到昨天我才知道 vba 存在,虽然我认为它很棒,它让我内心的书呆子高兴得跳了起来,但我是一个代码菜鸟!...我在网上进行了研究,但找不到我需要的任何东西。如果这里有人可以帮助我,我将永远感激:)

回答by nemmy

I'd suggest you familiarize yourself with the Macro Recorder in Excel. Basically it allows you to record your actions into VBA. It's a great way of learning what code to use to accomplish certain actions. For example, I just recorded this:

我建议您熟悉 Excel 中的宏记录器。基本上它允许您将您的操作记录到 VBA 中。这是学习使用什么代码来完成某些操作的好方法。例如,我刚刚记录了这个:

ActiveWorkbook.SaveAs Filename:="C:\Users\Nemmy\Documents\Book2.xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

It's a fairly trivial matter to take that code and have it run on a button click. You could modify it to get the file name from a couple of cell values using something like this:

获取该代码并让它在单击按钮时运行是一件相当微不足道的事情。您可以修改它以使用以下内容从几个单元格值中获取文件名:

    Dim sPath as string
    sPath=ActiveSheet.Range("A1").Value & ActiveSheet.Range("A2").Value & ".xlsx"
    ActiveWorkbook.SaveAs Filename:=sPath, _
        FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Cell A1 would contain your base path to your quote directory and Cell A2 would contain the name of the file.

单元格 A1 将包含引用目录的基本路径,单元格 A2 将包含文件名。