Excel VBA:将工作簿限制为本地宏

时间:2017-05-11 13:25:37

标签: excel vba excel-vba

我编写了一个代码,将两个电子表格复制到一个新的工作簿中,并将宏嵌入到工作表中,但是,调用宏的按钮仍然调用原始工作簿,而不是新的重复宏。工作簿。这个新工作簿将被发送给其他无法访问原始工作簿的人,所以我的问题是,有没有办法自动更新按钮以调用新工作簿中的宏,我将如何进行此操作?

导出代码如下所示:

Sub ExportPropWB()
    Dim a As String, b As String, c As String, Mainbook As Workbook, info As Range, FileExtStr As String, FileFormatNum As Long, foldername As String, datestring As String, Newbook As Workbook, info1 As Range, info2 As Range, paste1 As Range, paste2 As Range
    Set Mainbook = Application.ThisWorkbook
    a = Mainbook.Worksheets("Application").Range("Client")
    b = Mainbook.Worksheets("Application").Range("ProjectName")
    datestring = Format(Now, "yyyy-mm-dd")
    c = datestring & " - " & a & " Proposal " & b
    foldername = Mainbook.Path & "\proposal\"

    Application.ScreenUpdating = False

    Mainbook.Worksheets("Equipment").Copy
    Set Newbook = Application.ActiveWorkbook

    Set info1 = Mainbook.Worksheets("equipment").Range("A1:AU343")
    Set paste1 = Newbook.Worksheets("equipment").Range("A1:AU343")

    paste1.Value = info1.Value

    Mainbook.Worksheets("Proposal").Copy after:=Newbook.Worksheets("Equipment")

    Set info2 = Mainbook.Worksheets("Proposal").Range("A1:AG1480")
    Set paste2 = Newbook.Worksheets("Proposal").Range("A1:AG1480")

    paste2.Value = info2.Value

    FileExtStr = ".xlsm"
    xFile = foldername & c & " " & FileExtStr
    Newbook.SaveAs Filename:=xFile, FileFormat:=52

    Application.ScreenUpdating = True  
End Sub

按钮调用的代码如下:

Sub HidePage02()
    With ThisWorkbook.Worksheets("Proposal").Shapes("Hide2").TextFrame2.TextRange.Characters
        If .Text = "Hide Page 2" Then
            .Text = "Show Page 2"
            Rows("75:148").Hidden = True
            ThisWorkbook.Worksheets("Proposal").Shapes("BHIP2").Visible = False
            Thisworkbook.worksheets("Proposal").Range("Page2") = "Yes"
        Else
            .Text = "Hide Page 2"
            With ThisWorkbook.Worksheets("Proposal").Shapes("Hide2")
                Rows("75:148").Hidden = False
                Thisworkbook.worksheets("Proposal").Range("Page2") = "no"
                ThisWorkbook.Worksheets("Proposal").Shapes("BHIP2").Visible = True
            End With
        End If
    End With
End Sub

感谢您的时间和帮助。

1 个答案:

答案 0 :(得分:0)

SRJ提供的答案:

With ThisWorkbook.Worksheets("Proposal")
    .Shapes("Hide2").OnAction = "Sheet22.HidePage02"
End With

此代码将形状更新为新宏,其中Proposal是工作表,Hide2是对象的名称," Sheet22.HidePage02"是本地宏名称