closures文件时如何跳过工作簿2中的代码?

我的问题是当closuresworkbook2我需要使用代码自动select否popup一个消息框。 这是我的代码是如何布局的:

  1. Workbook1基于用户input创build多个文件。
  2. Workbook1中的循环打开Workbook2并从Workbook1input数据。
  3. 当循环完成input数据时,它会closures工作簿2,并popup一个消息框,其上有一个“是”或“否”button。
  4. 此时用户应始终select否
  5. 另一个窗口询问用户是否想要保存,并且应该始终是“是”。
  6. 循环继续,直到它创build了用户请求的所有文件

我尝试了谷歌search我的问题的变化,但没有太多的运气。 任何帮助深表感谢。

Dim JobName As String Dim lngLoop As Long Dim i As Integer Dim Customer As String Dim LastRow As Long Dim iCus As Integer Dim CompanyName As String Dim d As Long Dim strDir As Variant Dim DIV As String Dim XL As Excel.Application Dim WBK As Excel.Workbook Dim ActSheet As Worksheet Dim ActBook As Workbook Dim CurrentFile As Workbook Dim NewFileType As String Dim NewFile As String Dim QTR_NUM As String Dim MFG As String Dim Job As String Dim visitdate As Variant Dim visitdate_text As String Dim Quote_Request As Worksheet Dim QTR As Workbook Dim QTRLOG As Workbook Dim FORM As Workbook Dim DCSProgram As Workbook Dim ILast As Long Dim j As Integer Dim k As Integer Dim CustomerIDNum As String Dim QTRNUM As String Dim FolderName As String 'Creates Quote For Each MFG For j = 0 To QTRList.ListCount - 1 Set QTRLOG = Workbooks.Open("C:\QTR LOG.xlsm") Set QTR = Workbooks.Open("C:\QTR.xlsx") 'CODE TO INPUT DATA FROM USERFORM NEW QTR With DCSProgram.Sheets("MFG_DATA") ILast = .Cells(.Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If .Cells(i, 1).Value = MFG Then QTR.Sheets(1).Range("B7").Value = .Cells(i, 2).Value QTR.Sheets(1).Range("B8").Value = .Cells(i, 3).Value QTR.Sheets(1).Range("B9").Value = .Cells(i, 4).Value QTR.Sheets(1).Range("B12").Value = .Cells(i, 5).Value QTR.Sheets(1).Range("B13").Value = .Cells(i, 6).Value QTR.Sheets(1).Range("B14").Value = .Cells(i, 7).Value QTR.Sheets(1).Range("B15").Value = .Cells(i, 8).Value End If: Next: End With With QTRLOG.Sheets("QTR_LOG") ILast = .Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If .Cells(i, 1).Value = QTR_NUM Then .Cells(i, 2) = QTRList.List(j) '.Cells(i, 3) = FORM.Sheets(1).Range("H11").Value .Cells(i, 5) = JobName .Cells(i, 8) = "OPEN" .Cells(i, 9) = QTR.Sheets(1).Range("H9").Value End If: Next: End With QTRLOG.Save QTRLOG.Close QTR.SaveAs Filename:="C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\2. QUOTE REQUESTS\" & JobName & "\" _ & " DCS QTR " & QTRList.List(j) & " " & JobName & " (" & CustomerIDNum & ") " & visitdate_text & " .xlsx", _ FileFormat:=51, CreateBackup:=False, local:=True 'Code To Close File After Creating It QTR.Close Next j End If Application.ScreenUpdating = True Call Shell("explorer.exe" & " " & "C:\Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\2. QUOTE REQUESTS", vbNormalFocus) Unload NewQTR End Sub 

当这个代码运行一个msgbox从工作簿QTR出现。 我不希望用户在这个时候点击是或否。 我想自动select否,继续下一个文件。 每个MFG都重复这个过程。

QTR代码:

 Application.ScreenUpdating = True MSG1 = MsgBox("Are you ready to email to MFG?", vbYesNo, "EMAIL MFG") If MSG1 = vbYes Then 'Code to create email and attached workbook as PDF Else Const kPath As String = "C:\" Const kFile As String = "Users\Geoffrey\Dropbox\DCS PROGRAM\FILES\9. PROGRAM FILES\1. QUOTE REQUEST\QUOTE REQUEST LOG.xlsm" Dim TOTALFOB As Double Dim TOTALWC As Double Dim Wbk As Workbook Dim INWBK As Workbook Dim QTR_NUM As String Dim ILast As Long Dim i As Long Dim TOTMFG As Variant Dim TOTWC As Variant Dim LR As Long Dim TOTALTIME As Variant Set INWBK = ThisWorkbook With Sheets("QTR") LR = .Range("I" & Rows.Count).End(xlUp).Row TOTALFOB = WorksheetFunction.Sum(.Range("I23:I" & LR)) End With TOTALWC = TOTALFOB + INWBK.Sheets("QTR").Range("D18").Value QTR_NUM = INWBK.Sheets("QTR").Range("H7").Value TOTALTIME = INWBK.Sheets("WS_LOG").Range("J3").Value Rem Set Wbk in case it's open On Error Resume Next Set Wbk = Workbooks(kFile) On Error GoTo 0 Rem Validate Wbk If Wbk Is Nothing Then Set Wbk = Workbooks.Open(kPath & kFile) With Workbooks("QUOTE REQUEST LOG.xlsm").Sheets("QTR_LOG") ILast = .Cells(Rows.Count, 1).End(xlUp).Row For i = 1 To ILast If .Cells(i, 1).Value = QTR_NUM Then .Cells(i, 6) = TOTALFOB .Cells(i, 7) = TOTALWC .Cells(i, 10) = TOTALTIME End If: Next: End With Wbk.Save Wbk.Close End If End Sub 

如果您的问题是避免将某些Workbook_BeforeClose()事件处理程序放置在要执行的“ThisWorkbook”代码中,则必须“closures”closures该工作簿的代码行,如下所示

 Application.EnableEvents = False ' your code that closes the workbook Application.EnableEvents = True 

退出Sub 之前, 如果正在使代码更早退出。

所以删除上面提到的一个,并检查。