Excel - 如何在宏的顶部定义“文件名”并将其传递给宏

问题描述 投票:0回答:0

我不是 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

这是我的开放思维过程

  1. 在宏的顶部定义一个文本字符串(对吗?)作为“文件名”。目前“文件名”是“TR - 031023 之后的重量”——但是——我在宏中输入了这个。
  2. 在“文件名”字符串(或任何类型?文本??)中键入一个新名称,并使用此新名称代替当前的“TR - 031023 之后的重量”

这样做将允许我选择任何名称(例如,“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

这行得通吗? 我会玩一下,看看会发生什么。

谢谢。

excel vba
© www.soinside.com 2019 - 2024. All rights reserved.