Solved

How do I group Excel user form buttons with the Click event ?

Posted on 2014-01-06
9
353 Views
Last Modified: 2014-01-06
Hi,

I have an Excel VBA user form with a multi-page control (of 6 pages) each containing a single command button.

I want to be able to detect which of the (6) buttons has been clicked without resorting to coding separate procedures for each of the buttons by name
eg. Sub cbCalculate1_Click, Sub cbCalculate2_Click, Sub cbCalculate3_Click etc.......

I had a similar requirement with text boxes on the user form and an EE expert provided me with the code to resolve this issue. He set up a Class module and checked the text box name to establish which of the text boxes had been changed. The functionality works fine.

So, I've tried to adapt his code and apply it to my button problem. But when I click on a button nothing happens. I've tried it in debug and the Class module procedure isn't even invoked. I can only assume I don't have the correct syntax to 'catch' this click event.

Here's the code I have so far.
In a Class module I have called clsCalculate...

Option Explicit

Public WithEvents aButton  As MSForms.CommandButton

Private Sub aButton_Click()

   Dim Idx As Integer

   If aButton.Name Like "cbCalculate*" Then
      Idx = CInt(Mid(aButton.Name, 12, 1)) ' Which group?
      Call ufJobsForm.Calculate_Prices(Idx)
   End If

End Sub

Open in new window


The above Class procedure should pass an argument (Idx) to the form module procedure Calculate_Prices but when I click on a button it doesn't do anything.

Your help will be much appreciated.

Toco
0
Comment
Question by:Tocogroup
  • 5
  • 4
9 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39759010
Hi,

How do you initialze your command buttons?

Regards
0
 

Author Comment

by:Tocogroup
ID: 39759031
Hi,

Ah, I don't ! Didn't know I'd need to.

Prior to attempting to adopt your text box code for the buttons, I called each button by name with 6 separate procedures.
0
 

Author Comment

by:Tocogroup
ID: 39759051
How should I initialise the buttons in my example ?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39759134
Hi,

pls try

in the form

Dim myCmdBtns() As clsCalculate

Private Sub UserForm_Initialize()
    Dim ControlItem As Object, pointer As Long
    ReDim myCmdBtns(1 To Me.Controls.Count)
    For Each ControlItem In Me.Controls
        If TypeName(ControlItem) = "CommandButton" Then
            pointer = pointer + 1
            Set myCmdBtns(pointer) = New clsCalculate
            Set myCmdBtns(pointer).aButton = ControlItem
        End If
    Next ControlItem
    ReDim Preserve myCmdBtns(1 To pointer)
End Sub

Open in new window

Regards
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:Tocogroup
ID: 39759824
Ah, yes. I see what you mean. It's the same as you suggested for the text box code. I've got it to work thanks.

Can I use the same approach for a double-click event on a textbox field ? Like so.....

Public WithEvents aConfirmation  As MSForms.TextBox

Private Sub aConfirmation_DblClick()

   Dim Idx As Integer

   If aConfirmation.Name Like "teConfirmation*" Then
      Idx = CInt(Mid(aConfirmation.Name, 15, 1)) ' Which group?
      Call ufJobsForm.Create_Confirmation(Idx)
   End If

End Sub

Do I need to change the textbox initialize code for it to accept the double-click event ?
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39759854
Yes that's right

you have to initialize each control as a corresponding Class to be able to manipulate them as Class

Regards
0
 

Author Comment

by:Tocogroup
ID: 39759919
The problem is it's giving me a compilation error as I'm using the same initialization code for both types of textbox control:
a) textbox controls which get changed
b) other textbox controls on which I double-click.

How do I differentiate between the two types (or events) of textbox control ? This is the initialisation code you gave me for a change of value...

   For Each ControlItem In Me.Controls
      If TypeName(ControlItem) = "TextBox" Then
         Pointer = Pointer + 1
         Set myTextBx(Pointer) = New clsTextBx
         Set myTextBx(Pointer).aTextBox = ControlItem
      End If
   Next ControlItem

.aTextBox is used by the Class procedure when a textbox changes, whereas, I also need to set a separate Class control (.aConfirmation) for a different textbox when I double-click on that.
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39759952
If your remember

in your txtbox Class

you have the Change Event

Private Sub aTextBox_Change()
If aTextBox.Name Like "teDelegateName*" Then
   Call UserForm1.TextBxCountDelegates
End If

Open in new window


You could add a double click Event for the other Textboxes
Private Sub aTextBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
If Not(aTextBox.Name Like "teDelegateName*") Then
'   Call Another code
End If

Open in new window

Regards
0
 

Author Closing Comment

by:Tocogroup
ID: 39760024
Ah ! I hadn't included the (ByVal Cancel As MSForms.ReturnBoolean) as the argument. Not sure what that does though.

Anyway, it works perfectly.

Thanks again for your expert assistance.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
location range 4 22
How to Calculate Stainless Steel U-Bolt Dimension 15 67
Update As Well As Add 6 38
Google Sheets - Artificial Intelligence 2 17
Drop Down List with Unique/Distinct Values (enhancing the Combo-Box with a few steps and a little code) David miller (dlmille) Intro Have you ever created a data validation list from a database field or spreadsheet column (e.g., Zip Codes or Co…
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now