Solved

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

Posted on 2014-01-06
9
345 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 48

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 48

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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 48

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 48

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

11 Experts available now in Live!

Get 1:1 Help Now