vba 当我另存为时,如何让我的 Excel 工作表为文件名提取某些单元格值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15973118/
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-11 20:34:58  来源:igfitidea点击:

How to make my Excel worksheet pull certain cell values for File name when I Save As?

excelexcel-vbavba

提问by Jamie Walker

Ok, I will try to explain this the best I can. I have an Excel workbook that will be used as a cost sheet. At the top of this worksheet will be a Proposal Number (in cell D3), Customer Name in cell D4), and System Size (in cell D5). So let's say my sheet is Proposal Number 4423 for Customer Shanghai Noon and System Size 2500. I would like when I click Save As for it to automatically name the file "4423 Shanghai Noon SCR-2500 Tsoc". Is there a way to do this with a Macro or any other way? Any advice is greatly appreciated.

好的,我会尽力解释这一点。我有一个 Excel 工作簿,将用作成本表。此工作表的顶部将是提案编号(在单元格 D3 中)、客户名称在单元格 D4 中)和系统规模(在单元格 D5 中)。假设我的工作表是客户上海中午和系统大小 2500 的提案编号 4423。我希望当我单击“另存为”时自动将文件命名为“4423 上海中午 SCR-2500 Tsoc”。有没有办法用宏或任何其他方式来做到这一点?任何意见是极大的赞赏。

Ok so this is the code I got from recording a macro but it keeps saying Compile error: Expected: end of statement. I don't know how to make it use the text from these cells in the Save As.

好的,这是我从录制宏中得到的代码,但它一直说编译错误:预期:语句结束。我不知道如何使它使用“另存为”中这些单元格中的文本。

ActiveWorkbook.SaveAs Filename:= _
    "C:\Users\walkerja\Documents\CU Proposals\Range("D3").Select Range("D4").Select SCR-Range("D5").Select Tsoc.xlsm", FileFormat:= _
    xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

回答by Stewbob

Change your code to this:

将您的代码更改为:

Dim part1 As String
Dim part2 as String
Dim part3 as String

part1 = Range("D3").Value
part2 = Range("D4").Value
part3 = Range("D5").Value

ActiveWorkbook.SaveAs Filename:= _
"C:\Users\walkerja\Documents\CU Proposals\" & part1 & " " & part2 & " SCR-" & part3 & " Tsoc.xlsm", FileFormat:= _
xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False

You were mixing string values with spreadsheet operation methods. You need to get the values of your cells first, then you can use those values to build a string for the file name.

您将字符串值与电子表格操作方法混合在一起。您需要首先获取单元格的值,然后您可以使用这些值为文件名构建一个字符串。