VBA在excel中创建一个动态按钮

时间:2017-07-13 11:46:13

标签: excel vba button dynamic module

我需要一些关于以下代码的帮助。

  1. 它的第一个目的是检查按钮是否存在(工作正常)。

  2. 在电子表格上创建一个动态按钮(" Top20LossContracts"),(这也有效)

  3. 最后,当按下按钮时,它会运行另一个名为" FilterPivotTable"

  4. 的Sub方法

    上面的第3点在" Sub Modify_CommButton"中有编译错误。并且不会创建所需的代码模块。我不知道如何继续。

    大量错误,例如"未找到方法或数据成员"即使我厌倦了宣布所有数据类型。

    在Excel 2013上运行代码 非常感谢提前。

        Option Explicit
    
        ' Sub works fine
        Sub AddComm_button()
           Dim obj As OLEObject
           Dim FindButton As Boolean
           Dim mybutton
           For Each obj In ActiveSheet.OLEObjects
               If TypeOf obj.Object Is MSForms.CommandButton Then
            If obj.Name = "Filter_profit" Then
                FindButton = True
                Exit For
            End If
          End If
         Next
    
         If Not FindButton Then
           Set mybutton = ActiveSheet.OLEObjects.Add         (ClassType:="Forms.CommandButton.1")
           Application.DisplayAlerts = False
           With mybutton
           .Name = "Filter_profit"     
           .Object.Caption = "Filter Profit"
           .Top = 20
           .Left = 126
           .Width = 126.75
           .Height = 25.5
           .Placement = xlMoveAndSize
           .PrintObject = True           
            End With
    
            Call Modify_CommButton
         End If
      End Sub
    
      Sub Modify_CommButton()
       Dim LineNum As Long 'Line number in module
       Dim SubName As String 'Event to change as text
       Dim Proc As String 'Procedure string
       Dim EndS As String 'End sub string
       Dim Ap As String 'Apostrophe
       Dim Tabs As String 'Tab
       Dim LF As String 'Line feed or carriage return
       Dim ws As Worksheet
    
       Ap = Chr(34)
       Tabs = Chr(9)
       LF = Chr(13)
       EndS = "End Sub"
       SubName = "Private Sub Filter_profit_Click()" & LF
       Proc = Tabs & "Call " & Ap & "FilterPivotTable(0)" & Ap & LF
       Proc = Proc & "End Sub" & LF
       ws = Sheets("Top20LossContracts")
    
       Application.DisplayAlerts = False
       Set NewModule = ws.VBProject.VBComponents("Top20LossContracts").CodeModule
       With NewModule
         LineNum = .CountOfLines + 1
        .InsertLines LineNum, SubName & Proc & EndS
       End With
      End Sub
    

1 个答案:

答案 0 :(得分:0)

VBProject是Workbook对象的属性,而不是Worksheet对象。此外,在引用VBComponents集合中的组件时,您需要使用工作表的代码名称,而不是工作表名称。

Set NewModule = ActiveWorkbook.VBProject.VBComponents(ws.CodeName).CodeModule

此外,您为自己的程序构建的字符串并不完全正确。并且,不是将字符(例如制表符或回车符)分配给变量,而是可以使用VBA常量。我认为您的程序可以改写如下......

Sub Modify_CommButton()
    Dim ws As Worksheet
    Dim CM As Object 'Code module
    Dim LineNum As Long 'Line number
    Dim Proc As String 'Procedure

    Set ws = Worksheets("Top20LossContracts")

    Proc = "Private Sub Filter_profit_Click()" & vbCrLf
    Proc = Proc & vbTab & "Call FilterPivotTable(0)" & vbCrLf
    Proc = Proc & "End Sub" & vbCrLf

    Application.DisplayAlerts = False
    Set CM = ActiveWorkbook.VBProject.VBComponents(ws.CodeName).CodeModule
    With CM
      LineNum = .CountOfLines + 1
     .InsertLines LineNum, Proc
    End With
End Sub