簡體   English   中英

在保存之前通過單擊按鈕刪除Workbook_open()代碼

[英]Deleting Workbook_open() code with button click before save

我是VBA的新手,我有一個用於生成采購訂單的工作簿。 在ThisWorkbook中,我有以下代碼。

Private Sub Workbook_open()
Sheet1.[g6] = Sheet1.[g6] + 1
Sheet1.[b4] = ""
Sheet1.[c10] = ""
Sheet1.[a17] = ""
Sheet1.[a20:h35] = ""
Sheet1.CommandButton2.Enabled = False
Sheet1.CommandButton1.Enabled = False
End Sub

我在Sheet1上也有附加的代碼。 有沒有辦法刪除commandbutton2_click()中的Workbook_open編碼? 該工作簿將通過電子郵件發送並適當地保存,但是每次重新打開它時,都會重復workbook_open()並清除數據。 由於批准過程,保存的工作簿仍需要啟用Macro。

Private Sub CommandButton2_Click()
  If IsEmpty(Sheet1.Range("B4")) Then
        MsgBox "Please fill Highlighted cells before closing."
        Cancel = True
    End If
    If IsEmpty(Sheet1.Range("C10")) Then
        MsgBox "Please fill Highlighted cells before closing."
        Cancel = True
    End If
    If IsEmpty(Sheet1.Range("A17")) Then
        MsgBox "Please fill Highlighted cells before closing."
        Cancel = True
    End If


Dim FileName As String
 Dim Path As String

Application.DisplayAlerts = False

Path = "\\COSRVR1\Data\Purchase orders\"
 FileName = Sheet1.Range("G6").Value & ".xlsm"

ThisWorkbook.SaveAs Path & FileName, xlOpenXMLWorkbookMacroEnabled

CommandButton2.Visible = False

Dim olapp As Outlook.Application
Set olapp = CreateObject("outlook.application")

Dim olmail As Outlook.MailItem
Set olmail = olapp.CreateItem(olMailItem)
olmail.To = Sheet1.Range("a8")
olmail.Subject = "Approval needed_" & Sheet1.Range("g6")
olmail.Attachments.Add ActiveWorkbook.FullName
olmail.send
ActiveWorkbook.Close

End Sub


Private Sub Worksheet_Change(ByVal Target As Range)

    If Sheet1.Range("i38").Value > 1999 Then
        CommandButton2.Enabled = True
    Else
        CommandButton2.Enabled = False
End If
    If Sheet1.Range("i38").Value = 0 Then
        CommandButton1.Enabled = False
    Else
    End If
    If Sheet1.Range("i38").Value < 2000 Then
        CommandButton1.Enabled = True
    Else
        CommandButton1.Enabled = False
End If
End Sub

嘗試添加以下內容:

With ThisWorkbook.VBProject.VBComponents("ThisWorkbook").CodeModule
    .DeleteLines 1, .CountOfLines
End With

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM