繁体   English   中英

Excel宏,用于保存在单元格值下

[英]Excel Macro for saving under cell value

我不熟悉所有这些编码内容,并且正在尝试创建一个宏,该宏一旦使用将保存活动工作表作为在“ D2”单元格中输入的日期的名称。 我使用的是excel2010。我找到了一些代码,可以将工作表保存到指定的文件夹中,但是还没有什么可以保存为“ D2”单元格中的内容。

这是我现在正在使用的代码:

Sub Save_GPR()
   Application.ScreenUpdating = False
   ActiveSheet.Select
   ActiveSheet.Copy
   ThisFile = Range("Q1").Value
   ActiveWorkbook.SaveAs "C:\Users\owner\Desktop\FlightLog", FileFormat:=52
   Application.ScreenUpdating = True
   ActiveWorkbook.Close
End Sub

任何帮助将不胜感激。 谢谢!

以下编辑适合您。 我删除了Activesheet.Select/Copy,因为它是多余的。

Sub Save_GPR()
   Application.ScreenUpdating = False

   'grab the file name from D2, put it in variable ThisFile
   ThisFile = ActiveSheet.Range("D2").Value

   'Save the activeworkbook. Use the filename in ThisFile variable
   ActiveWorkbook.SaveAs "C:\Users\owner\Desktop\" & ThisFile, FileFormat:=52


   Application.ScreenUpdating = True
   ActiveWorkbook.Close
End Sub

尝试将代码更改为以下内容:

Sub Save_GPR()
   Application.ScreenUpdating = False

   'Exit if D2 is empty
   If Activesheet.Range("D2").Value = vbNullString Then Exit Sub

   ThisFile = Activesheet.Range("D2").Value

   ActiveWorkbook.SaveAs "C:\Users\owner\Desktop\" & ThisFile, FileFormat:=52
   Application.ScreenUpdating = True
   ActiveWorkbook.Close
End Sub

我删除了与您描述的任务无关的代码。 我添加了一行代码,如果单元格D2为空,它将取消该过程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM