【问题标题】:VBA - Application.DisplayAlerts = False not workingVBA - Application.DisplayAlerts = False 不工作
【发布时间】:2021-03-17 06:04:13
【问题描述】:

这是一个每日运行以更新报告的宏。该文件需要每天覆盖现有文件。但是,Application.DisplayAlerts = False 不起作用,我仍然会弹出说这个文件已经存在并且如果我想替换的话。我的代码是否有问题,或者是否有解决方法可以使用自动为我单击“是”的方法?

Sub DailyRefresh ()
    'Open and refresh Access
    Dim appAccess As Object
    Set appAccess = GetObject("S:\Shared\DailyRefresh.accdb")
    Application.DisplayAlerts = False
    appAccess.Visible = True
    appAccess.DoCmd.RunMacro "Run_All_Queries"
    appAccess.CloseCurrentDatabase

   'Open Excel

    Set xl = CreateObject("Excel.Application")
    xl.Workbooks.Open ("s:\Shared\Template.xlsx")
    xl.Visible = True
    Application.DisplayAlerts = False
    
'Set date to the 1st of the Month on Summary tab
    xl.Sheets("Summary").Visible = True
    xl.Sheets("Summary").Select
    xl.Range("C10").Value = DateSerial(Year(Now), Month(Now), 1)
    xl.Range("C10").NumberFormat = "mm/dd/yyyy"

' REFRESH Table
    xl.Sheets("Data").Visible = True
    xl.Sheets("Data").Select
    xl.Range("A1").Select
    xl.Range("DailyRefresh.accdb[[#Headers],[ACTIVITY_DT]]").Select
    xl.Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
    xl.Worksheets("Fname").Visible = True
    xl.Sheets("Fname").Select
    
     
'Copy and Save AS
    
    Application.DisplayAlerts = False
    Path = "S:\Shared\NewTemplate"
    Filename = xl.Sheets("Fname").Range("A7").Value
    xl.SaveAs Path & Filename & ".xlsx", FileFormat:=51, CreateBackup:=False
    xl.Worksheets("Fname").Visible = False
    xl.Close
    Application.DisplayAlerts = True
    
End Sub

【问题讨论】:

  • 如果您知道文件在哪里,您可以发送Kill FilePath 并提前将其删除以避开覆盖提示。

标签: vba


【解决方案1】:
Application.DisplayAlerts = False

指的是运行您的代码的应用程序,而不是您创建的 Excel 实例。

xl.DisplayAlerts = False

会是你想要的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    • 2019-10-25
    • 1970-01-01
    • 2016-10-04
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    相关资源
    最近更新 更多