+ Reply to Thread
Results 1 to 2 of 2

Error in VBA code to conditionally add ActiveX command buttons and assign click events

Hybrid View

  1. #1
    Registered User
    Join Date
    06-29-2023
    Location
    Mumbai, India
    MS-Off Ver
    Microsoft Office Home and Student 2021
    Posts
    1

    Question Error in VBA code to conditionally add ActiveX command buttons and assign click events

    I am creating a spreadsheet for my budget, where some cells keep track of my expenses in a month in a particular field. For example, a cell might keep track of all my expenses on food in June 2023. Such cells start with a "=" sign followed by a mathematical expression with numbers, plus or minus signs, and parentheses. For example: "=345+86-72+782".

    I am trying to write VBA code for a macro that detects all such cells in the active sheet and assigns ActiveX command buttons to each of these cells which on clicking open an input box where I can type in a new expense (like "+31") and it will get added to the cell.

    I am running into several errors, can someone help?

    Sub AddButtonToCells()
        Dim button As Object
        Dim cell As Range
        
        For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeFormulas)
            If Not HasPrecedents(cell) Then
                Set button = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", Link:=False, _
                        DisplayAsIcon:=False, Left:=cell.Left + cell.Width - 6, Top:=cell.Top + 1, _
                        Width:=15, Height:=22)
                    
                With button
                    .Object.Caption = "+"
                    .Object.BackColor = RGB(0, 255, 0)
                    .Object.ForeColor = RGB(255, 255, 255)
                    .Object.Font.Size = 12
                    .Object.Font.Name = "Arial"
                    .Object.Font.Bold = True
                    .Name = "Button_" & cell.Address
                End With
                    
                With ThisWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
                    .InsertLines .CreateEventProc("Click", "Button_" & cell.Address) + 1, "AddNumericValue " & cell.Address
                End With
            End If
        Next cell
    End Sub
    
    Function HasPrecedents(rngCheck As Range) As Boolean
       Dim lngSheetCounter As Long, lngRefCounter As Long, rngDep As Range
       On Error Resume Next
       With rngCheck
          .ShowPrecedents False
          Set rngDep = .NavigateArrow(True, 1, 1)
          If rngDep.Address(External:=True) = rngCheck.Address(External:=True) Then
             HasPrecedents = False
          Else
             HasPrecedents = (Err.Number = 0)
          End If
          .ShowPrecedents True
       End With
    End Function
    
    Private Sub AddNumberToCell(cellAddress As String)
        Dim str As String
        Dim cellFormula As String
        Dim cell As Range
        
        Set cell = Range(cellAddress)
        
        str = Application.InputBox("Enter addition to formula: ")
        cellFormula = cell.formula
        cellFormula = cellFormula & str
        cell.formula = cellFormula
    End Sub
     
    Sub DeleteAllControls()
        Dim obj As OLEObject
    
        For Each obj In ActiveSheet.OLEObjects
            obj.Delete
        Next obj
        Dim shp As Shape
    
        For Each shp In ActiveSheet.Shapes
            ' Check if the shape is a form button
            If shp.Type = msoFormControl Then
                ' Delete the form button
                shp.Delete
            End If
        Next shp
        
        Dim startLine As Long
        Dim endLine As Long
        Dim i As Long
        
        With ThisWorkbook.VBProject.VBComponents(ActiveSheet.Name).CodeModule
            startLine = .ProcStartLine("AddNumericValue", vbext_pk_Proc)
            endLine = .ProcCountLines("AddNumericValue", vbext_pk_Proc) + startLine
            For i = endLine To startLine + 1 Step -1
                .DeleteLines i
            Next i
        End With
    End Sub

  2. #2
    Forum Expert torachan's Avatar
    Join Date
    12-27-2012
    Location
    market harborough, england
    MS-Off Ver
    Excel 2010
    Posts
    4,313

    Re: Error in VBA code to conditionally add ActiveX command buttons and assign click events

    You are in the process of developing a nightmare, ActiveX controls imbedded on a worksheet are notoriously unstable.
    I would urge you to read up on the web "reasons not to use ActiveX on the sheet.
    Torachan,

    Mission statement; Promote the use of Tables, Outlaw the use of 'merged cells' and 'RowSource'.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. ActiveX Command Button combined with ActiveX List Boxes – VBA Code Needed
    By Stlcards13 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-26-2017, 02:56 PM
  2. [SOLVED] Trap events on UserForm multiple command buttons in one sub
    By EssoExplJoe in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 07-28-2016, 10:02 AM
  3. Infor bubble on activeX command buttons
    By EMcCausland in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-16-2016, 05:39 PM
  4. [SOLVED] One Sub for Multiple Command Buttons (ActiveX Control)
    By Raymundus in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-19-2016, 11:30 AM
  5. [SOLVED] Command Buttons Form Control or ActiveX
    By Epscan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2014, 01:41 PM
  6. [SOLVED] Conditionally Hide or deactivate code in an activeX command Button.
    By Kramxel in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-26-2014, 12:19 PM
  7. use a command button to click other command buttons
    By fcharl9 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-20-2013, 08:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1