如何使用VBA“另存为”

时间:2015-08-05 10:52:30

标签: excel vba excel-vba

我想请求帮助下面的代码。当我在将新文件保存到桌面时录制宏时,我得到了这个。

我遇到了更改目录行的问题。每次不同的用户使用此宏时,都会发生此更改,这也可能取决于e的值。

此外,部分文件名会有所不同,具体取决于d的值。

请帮我改进我的代码。谢谢!

Dim b, c, d, e As String

b = MsgBox("This file has to be saved. Click OK.", vbOKCancel, "Save to SharePoint")

ActiveWorkbook.Save

c = MsgBox("Save this file to local", vbOKOnly, "Save to Local")

d = InputBox("This shift record falls on what week?", "Save Shift Record", "YYYYMMDD-YYYYMMDD")

e = InputBox("Enter your EID", "Save Shift Record", "EID")

'this line give error ~~~~~ 
ChDir "C:\Users\ma.cristina.i.gumato\Desktop"

ActiveWorkbook.SaveAs Filename:= "C:\Users\d\Desktop\MANILA_ShiftRecord_" + d + ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

1 个答案:

答案 0 :(得分:1)

这对你有用吗?

Dim b, c, d, e As String

b = MsgBox("This file has to be saved. Click OK.", vbOKCancel, "Save to SharePoint")

ActiveWorkbook.Save

c = MsgBox("Save this file to local", vbOKOnly, "Save to Local")

d = InputBox("This shift record falls on what week?", "Save Shift Record", "YYYYMMDD-YYYYMMDD")

e = InputBox("Enter your EID", "Save Shift Record", "EID")

ActiveWorkbook.SaveAs Filename:= "C:\Users\" & environ("username") & "\Desktop\MANILA_ShiftRecord_" + d + ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False