我不是 VBA 程序员或编码员,但经常使用记录宏功能,它确实有帮助。对于下面的宏,我想做的是能够在宏的顶部键入一个“文件名”,并在宏中使用这个“名称”。 (我做了一些访问数据库工作,所以我在这里使用访问的“VBA”)
// current macro begin
Sub TR_Weight_031023()
'
' TR_Weight_tracker_SAVE_111422 Macro
Application.DisplayAlerts = False
ChDir "D:\Dropbox\G_emiguy_052921\__Twix and Rolo"
ActiveWorkbook.SaveAs Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\TR - weight after 031023.xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\TR - weight after 031023.pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
ActiveWorkbook.SaveAs Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\TR - weight after 031023.xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Range("O5:P6").Select
Application.DisplayAlerts = True
End Sub
//current macro end
这是我的开放思维过程
这样做将允许我选择任何名称(例如,“TR - weight sent to Jim”)并且文件将是该新名称。如果我使整个 VBA / 宏变长,我只需要更改“文件名”值(同样,“值”是正确的术语吗??)
不确定这是否正确,但这是我设想的“新”宏(不要笑得太厉害!!)
** begin NEW macro with attempt to change
Sub TR_new_weight()
Dim file_name as string
file_name = "TR_new_file_name"
Application.DisplayAlerts = False
ChDir "D:\Dropbox\G_emiguy_052921\__Twix and Rolo"
ActiveWorkbook.SaveAs Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\"&'TR_new_file_name'&".xlsx", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\"&'TR_new_file_name'&".pdf", _
Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False
ActiveWorkbook.SaveAs Filename:= _
"D:\Dropbox\G_emiguy_052921\__Twix and Rolo\"&'TR_new_file_name'&".xlsm", _
FileFormat:=xlOpenXMLWorkbookMacroEnabled, CreateBackup:=False
Range("O5:P6").Select
Application.DisplayAlerts = True
End Sub
** end NEW macro with attempt to change
这行得通吗? 我会玩一下,看看会发生什么。
谢谢。