2014-09-24 44 views
0

我对所有这些编码的东西都陌生,正在尝试创建一个曾经使用的宏将保存活动工作表作为任何日期在单元格“D2”中输入的名称。我使用excel 2010.我发现了一些代码,可以将工作表保存到指定的文件夹中,但没有任何工具可以将它保存为单元格“D2”中的任何内容。用于保存单元格值的Excel宏

这是我现在使用的代码:

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 

任何帮助将真正理解。谢谢!

回答

0

以下编辑应该适合您。我删除了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 
0

试着改变你的代码如下:

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为空,我添加了一行代码将取消该过程。

相关问题