如何防止Excel提示用户启用宏?

时间:2013-12-19 14:00:21

标签: excel-vba vba excel

我正在使用具有两个工作表的Excel采购订单生成器:PO生成器(sheet1)和PO上的下拉验证列表的详细信息(sheet2)。 ThisWorkbook代码模块中有代码。

PO生成器通过增加采购订单编号,复制/粘贴,使用新文件名保存和重新保存来工作。

我正在努力采用一种方法来提示用户确保启用宏并阻止生成器(Sheet1)被看到。

我已经包含了一个例程来删除生成的采购订单上的所有vba代码(并且没有代码或模块存在于他们身上),但是他们会重新打开宏安全警告。

<小时/> 我的代码:

Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    If ThisWorkbook.ReadOnly Then
        MsgBox "Please use dropdown arrow next to filename " & _
            "within SharePoint and select " & _
            "'Edit in Microsoft Office Excel' instead."
        ThisWorkbook.Close
    End If
    ActiveWorkbook.Sheets("Sheet1").Visible = xlSheetVisible
    Sheets("Sheet1").Select
    Range("U13") = Range("U13") + 1
    Workbooks.Open Filename := _
        "\\ehfnp01\users\gminter\My Documents\PO Log Elite\PO Log Elite.xls"
    Workbooks("PO Log Elite.xls").Activate
    With ActiveWorkbook.Sheets("Sheet1")
        .Unprotect Password:="2"
    End With
    Workbooks("Elite Purchase Order Generator Ver 7 20-12-13.xls").Activate
    Sheets("Sheet1").Select
    Range("U13").Copy
    Workbooks("PO Log Elite.xls").Activate
    With ActiveWorkbook.Sheets("Sheet1")
        Dim lst As Long
        lst = .Range("B" & Rows.Count).End(xlUp).Row + 1
        .Range("B" & lst).PasteSpecial xlPasteValuesAndNumberFormats
    End With
    Workbooks("Elite Purchase Order Generator Ver 7 20-12-13.xls").Activate
    ActiveWorkbook.Sheets("Sheet1").Visible = xlSheetVeryHidden
    ActiveWorkbook.Save
    Workbooks("PO Log Elite.xls").Activate
    With ActiveWorkbook.Sheets("Sheet1")
        lst = .Range("A" & Rows.Count).End(xlUp).Row + 1
        .Range("A" & lst) = Now
    End With
    With ActiveWorkbook.Sheets("Sheet1")
        lst = .Range("C" & Rows.Count).End(xlUp).Row + 1
        .Range("C" & lst).Value = Environ("Username")
        .Protect Password:="2"
    End With
    ActiveWorkbook.Save
    ActiveWorkbook.Close
    ActiveWorkbook.Sheets("Sheet1").Visible = xlSheetVisible
    Sheets("Sheet1").Select
    ThisFile = Application.DefaultFilePath & "\" & _
        Range("T13").Value & Range("U13").Text & ".xls"
    ActiveWorkbook.SaveAs Filename:=ThisFile, Password:=""
    Range("U14") = Now
    Range("L21").Value = Environ("Username")
    Set Rng = Intersect(ActiveSheet.UsedRange, Range("L21"))
    For Each C In Rng
        C.Value = StrConv(C.Value, vbUpperCase)
    Next
    Cells.Locked = False
    Range("U13:Y14,L21:Z21,A79:AE144").Locked = True
    ActiveSheet.Protect Password:="1"
    ActiveWorkbook.Sheets("MacroEnable").Visible = xlSheetVeryHidden

    Dim x               As Integer

    On Error Resume Next
    With ActiveWorkbook.VBProject
        For x = .VBComponents.Count To 1 Step -1
            .VBComponents.Remove .VBComponents(x)
        Next x
        For x = .VBComponents.Count To 1 Step -1
            .VBComponents(x).CodeModule.DeleteLines _
            1, .VBComponents(x).CodeModule.CountOfLines
        Next x
    End With
    On Error GoTo 0
    ActiveWorkbook.Save
    Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

因此,如果我理解,您希望PO生成器生成的Excel文件保存下来,以便打开它不会触发宏警告弹出栏。

要使其发挥作用:

  1. 您需要从工作表 中删除所有代码并从Visual Basic编辑器中删除每个Module对象 (在{{1}中} ribbon)。请注意,除非您实际Developer每个Module对象(在VB编辑器中选择并右键单击),Excel将继续显示警告栏。在这些删除后保存它将阻止宏警告......

  2. 要检查的另一项是您是否有外部数据连接。如果这样做,可能仍会出现警告,提示用户是否激活这些警告。要摆脱这些,请到Remove...功能区Data并逐个删除所有连接。

  3. 请记住将文件保存为标准的 > Connections 格式(Excel工作簿),而不是默认的.xlsx用于带有宏的工作表(Excel启用宏的工作簿) )。

  4. 只要没有代码,任何按钮或其他UI功能都可以保留。