我有一个模块可以打开4个其他工作簿,重新格式化一些数据并将其粘贴到当前工作簿中,然后关闭其他工作簿。
当我关闭其他工作簿时,我得到一个DisplayAlert,询问我是否要保存工作簿。我想停止出现DisplayAlert。
microsoft的建议是使用:
Application.DisplayAlerts = False
ActiveWorkbook.Close
Application.DisplayAlerts = True
然而,当我这样做时,宏每次都会崩溃。
如果我注释掉DisplayAlerts行,那么宏运行正常,除非我必须处理"你要保存吗?" DisplayAlerts。
有什么想法吗?
代码:
Function rngFoundLog(searchDate As Date)
Set rngSearchLog = Workbooks("Ecom KPI.xlsm").Worksheets("Daily Update Log").Range("A:A")
Set rngFoundLog = rngSearchLog.Find(What:=Sheet1.searchDate, LookIn:=xlValues, LookAt:=xlPart)
End Function
Function formatHourlies(fileName As String) As Object
Dim fullFileName As String
fullFileName = ActiveWorkbook.Path & "\Hourlies\" + fileName
Workbooks.Open fileName:=fullFileName
Workbooks(fileName).Worksheets("Top Line Metrics").Range("B9:H32").Copy
Workbooks(fileName).Worksheets("Top Line Metrics").Range("A34").PasteSpecial Transpose:=True
Workbooks(fileName).Worksheets("Top Line Metrics_0").Range("B9:H32").Copy
Workbooks(fileName).Worksheets("Top Line Metrics").Range("Y34").PasteSpecial Transpose:=True
Workbooks(fileName).Worksheets("Top Line Metrics_1").Range("N9:H32").Copy
Workbooks(fileName).Worksheets("Top Line Metrics").Range("AW34").PasteSpecial Transpose:=True
Workbooks(fileName).Worksheets("Top Line Metrics").Range("A34:BT40").Select
Selection.Replace What:="-", Replacement:="0", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False
Selection.Copy
End Function
Sub HourlyData()
Application.Calculation = xlManual
Application.ScreenUpdating = False
'Application.DisplayAlerts = False
Dim proxyServer As String
Dim clientID As String
Dim report_period As String
Dim report_date As String
Dim searchDate As Date
Sheet1.proxyServer = Worksheets("Update Data").Range("H2").Value
Sheet1.proxyStatus = Worksheets("Update Data").Range("H1").Value
Sheet1.report_date = Worksheets("Update Data").Range("B2").Value
Sheet1.searchDate = Worksheets("Update Data").Range("B3").Value
Dim answer As Integer
answer = MsgBox("Do you want to import the data?", vbYesNo + vbQuestion, "Import Data?")
If answer = vbYes Then
Dim StartTime As Double
Dim MinutesElapsed As String
Dim dateRange As Range
StartTime = Timer
reportDate = Worksheets("Update Data").Range("B3").Value
searchDatev2 = reportDate - 7
Set rngSearch = Worksheets("Business Objects").Range("A:A")
Set rngFound = rngSearch.Find(What:=searchDatev2, LookIn:=xlValues, LookAt:=xlPart)
Dim fileName As String
fileName = "couk Hourlies.xlsx"
formatHourlies (fileName)
Workbooks("Ecom KPI.xlsm").Worksheets("Hourlies").Range("B" & rngFound.Row).PasteSpecial xlPasteValues
Workbooks(fileName).Close
fileName = "mcouk Hourlies.xlsx"
formatHourlies (fileName)
Workbooks("Ecom KPI.xlsm").Worksheets("Hourlies").Range("EQ" & rngFound.Row).PasteSpecial xlPasteValues
Workbooks(fileName).Close
fileName = "ie Hourlies.xlsx"
formatHourlies (fileName)
Workbooks("Ecom KPI.xlsm").Worksheets("Hourlies").Range("KF" & rngFound.Row).PasteSpecial xlPasteValues
Workbooks(fileName).Close
fileName = "mie Hourlies.xlsx"
formatHourlies (fileName)
Workbooks("Ecom KPI.xlsm").Worksheets("Hourlies").Range("PU" & rngFound.Row).PasteSpecial xlPasteValues
Workbooks(fileName).Close
Workbooks("Ecom KPI.xlsm").Worksheets("Daily Update Log").Range("T" & rngFoundLog(Sheet1.searchDate).Row).Value = Application.UserName
MinutesElapsed = format((Timer - StartTime) / 86400, "hh:mm:ss")
MsgBox "Data Import Completed in " & MinutesElapsed
Else
'do nothing
End If
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
'Application.DisplayAlerts = True
End Sub
答案 0 :(得分:4)
如果您不想保存数据,请尝试使用ActiveWorkbook.Close False
。这将关闭工作簿而不保存(没有提示)并且不使用DisplayAlerts行。您还可以将活动工作簿设置为您需要的任何工作簿名称。
答案 1 :(得分:1)
我不会尝试关闭活动工作簿,但会设置工作簿并关闭它:
fields
答案 2 :(得分:1)
您可以保留DisplayAlerts,但是对于您要关闭的工作簿,请设置已保存的标记...
例如,就在关闭Someworkbook之前:
Someworkbook.Saved = true
这应该会停止要求您保存的消息框...
希望有所帮助..
答案 3 :(得分:0)
这发生在我身上,我意识到它发生了,因为我打开了VBA编辑器。当我关闭VBA编辑器,然后运行宏时,Excel没有崩溃。