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

时间:2016-02-25 03:32:22

标签: excel vba

我的问题是关闭工作簿2时我需要使用代码在弹出的消息框中自动选择“否”。这就是我的代码的布局:

  1. Workbook1根据用户输入创建多个文件。
  2. Workbook1中的循环打开Workbook2并从Workbook1输入数据。
  3. 当完成循环输入数据时,它会关闭工作簿2并弹出一个消息框,上面带有“是”或“否”按钮。
  4. 此时用户应始终选择否。
  5. 另一个窗口询问用户是否要保存,并且应始终为是。
  6. 循环继续,直到创建了用户请求的所有文件
  7. 我尝试使用谷歌搜索我的问题的变化,但没有太多的运气。非常感谢任何帮助。

     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
    

    当此代码运行时,将从工作簿QTR中显示msgbox。我不希望用户此时必须单击是或否。我想自动选择否并继续下一个文件。对每个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
    

2 个答案:

答案 0 :(得分:1)

如果您的问题是避免将某个Workbook_BeforeClose()事件处理程序置于" ThisWorkbook"要执行的代码,那么你必须"附上"关闭工作簿的代码行如下

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

答案 1 :(得分:0)

退出子 ,然后 结束,如果让代码退出更早。

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