Specify Click Action for Dynamic Text on UserForm

Jonathan Harrison

New Member
Joined
Jul 15, 2011
Messages
43
I have a VBA userform with a grid of numbers, each of which needs its own "click" action. For example:

[0 1 1 4 0 1 0]
[2 0 0 0 0 2 0]

These numbers are updated through other VBA functions. How can I create click actions for each of these numbers? Right now this data is created as lables on the form, but that can be changed if needed. Ideally, clicking each number would call the same function, just passing the lable name to the function.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
You could use a class to handle the click events of the labels, this is a very simple example of that.

Add a new class module, Class1.

Add this code to the class module.
Code:
Option Explicit

Public WithEvents lbl As MSForms.Label

Private Sub lbl_Click()
    MsgBox lbl.Caption
End Sub
Add a new, blank userform and put this code in it's module.
Code:
Option Explicit

Dim lbls(1 To 14) As Class1

Private Sub UserForm_Initialize()
Dim ctl As MSForms.Label
Dim I As Long
Dim lngTop As Long
Dim lngLeft As Long

    lngLeft = 5
    lngTop = 5
    
    For I = 1 To 14
    
        Set ctl = Me.Controls.Add("Forms.Label.1", "Label" & I, True)
        
        ctl.Height = 10
        ctl.Left = lngLeft
        ctl.Top = lngTop
        ctl.Width = 6
        
        Set lbls(I) = New Class1
        
        Set lbls(I).lbl = ctl
        
        ctl.Caption = Int(Rnd() * 7)
        
        lngLeft = lngLeft + ctl.Width + 2
        
        If I = 7 Then
            lngTop = lngTop + ctl.Height + 2
            lngLeft = 5
        End If
        
    Next I
    
End Sub

Now run the userform and see what happens when you click on one of the labels.

Like I said very simple.
 
Upvote 0
These labels need to be able to change and the grid isn't always the same size. How can I remove all the old labels when I update the data so that old data isn't retained?

Example:
1st round of data [0 1 2 3 4]
2nd round of data [5 6 7] <--This is how it should look
2nd round of data [5 6 7 3 4] <--This is how it actually looks
 
Upvote 0
In the example the labels were dynamically created at runtime in the userform Initialize event.

So I don't see why you couldn't remove them and create another set of labels.

I've not tested that though, bit early, but if it is possible then you should be able to repeat for as many 'rounds' are needed.

Alternatively, the form could be unloaded and then reloaded for each round.

Obviously the code would need to be altered so the no of labels isn't hard-coded.

How are you generating the data?
 
Upvote 0
The data comes from a terminal session. It comes into a string that is then split into an array (one element per line of terminal output).

I've tried to use a button with this code but get the error below:
Code:
Private Sub btnScratch_Click()
Dim ctrlX As MSForms.Control
For Each ctrlX In Me.Controls
    If TypeOf ctrlX Is MSForms.Label Then
        Me.Controls.Remove ctrlX.name
    End If
Next ctrlX

End Sub

ERROR:
Run-time error '444':

Could not delete the controls. This method can't be used in this context.
 
Upvote 0
It looks like the error was from the script tying to remove labels that were created in design mode before it got to the ones create at runtime. The runtime labels have a different font so I was able to fix the issue by checking the font type before attempting to remove to weed out the ones created in design. This probably isn't the "correct" way to fix it, but it works for me.

Code:
Private Sub btnScratch_Click()
Dim ctrlX As MSForms.Control
For Each ctrlX In Me.Controls
    If TypeOf ctrlX Is MSForms.Label And ctrlX.font.name <> "Tahoma" Then
        Me.Controls.Remove ctrlX.name
    End If
Next ctrlX

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,150
Messages
6,170,377
Members
452,323
Latest member
robertbs021

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top