Results 1 to 11 of 11

Same code for 26 Buttons

Threaded View

  1. #2
    Forum Expert nigelog's Avatar
    Join Date
    12-14-2007
    Location
    Cork, Ireland
    MS-Off Ver
    Office 365 Windows 10
    Posts
    2,293

    Re: Same code for 26 Buttons

    this was posted here a while back and offers a probable solution
    see attached workbook for multiple controls run by class

    In Userform
    Option Explicit
    Dim handlers As Collection
    Private Sub UserForm_Initialize()
        
        
        Dim x As Long
        Dim y As Long
        Dim btn As MSForms.CommandButton
        Dim handler As ButtonHandler
        
        Const width = 40
        Const height = 30
        Const Space = 5
        Const lblHeight = 15
        
        Me.width = (6 * width) + (4.5 * Space)
        Me.height = (6 * height) + (2 * Space)
        
        Set handlers = New Collection
        
      For x = 1 To 5
        
                For y = 1 To 6
                Set btn = Me.Controls.Add("Forms.CommandButton.1")
                Set handler = New ButtonHandler
                Set handler.CommandButton = btn
                handlers.Add handler
                With btn
                    .height = height
                    .width = width
                    .Top = ((x * height) - height) + Space
                    .Left = ((y * width) - width) + Space
                    .Caption = "R" & x & "C" & y
                End With
            Next y
        Next x
    
    
    
    End Sub
    in class module called button handler
    Option Explicit
    
    Public WithEvents CommandButton As MSForms.CommandButton
    
    Private Sub CommandButton_Click()
        MsgBox CommandButton.Caption & " was clicked"
    End Sub
    Attached Files Attached Files
    Last edited by nigelog; 07-04-2019 at 11:24 AM.

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Macros with buttons, spin buttons, scroll buttons, etc.
    By qqbbppdd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-12-2013, 09:34 AM
  2. Useform Code - 12 Command Buttons dependant on other command buttons
    By darrenkaye in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-20-2013, 10:58 AM
  3. assigning code to buttons
    By crazyhick in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-28-2010, 08:02 AM
  4. assigning code to buttons
    By crazyhick in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2010, 06:36 AM
  5. [SOLVED] Generic protect/unprotect code through buttons and code?
    By StargateFanFromWork in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 01-02-2006, 03:35 PM
  6. Buttons click in Code
    By Bettergains in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-09-2005, 07:05 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