简体   繁体   English

在保存之前通过单击按钮删除Workbook_open()代码

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

I am new to VBA, I have a workbook that I am using to generate purchase orders. 我是VBA的新手,我有一个用于生成采购订单的工作簿。 In ThisWorkbook I have the below code. 在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

I also have the attached code on Sheet1. 我在Sheet1上也有附加的代码。 Is there a way to delete the Workbook_open coding in the commandbutton2_click()? 有没有办法删除commandbutton2_click()中的Workbook_open编码? The workbook is emailed and saved appropriately, however everytime it is re-opened it repeats the workbook_open() and clears out the data. 该工作簿将通过电子邮件发送并适当地保存,但是每次重新打开它时,都会重复workbook_open()并清除数据。 The saved workbook still needs to have Macro's enabled, because of an approval process. 由于批准过程,保存的工作簿仍需要启用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

Try adding this: 尝试添加以下内容:

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