Clicking on a Userform label puts its label name automatically in a textbox

chazrab

Well-known Member
Joined
Oct 21, 2006
Messages
917
Office Version
  1. 365
Platform
  1. Windows
As title explains. The image below shows a userform with 66 labels each with the name of a Bible book. I.e., I just changed the name of Label1 to Genesis, etc.
When a user clicks on that label, it automatically puts its label name in Textbox1. That's it. That's all I want to accomplish. The form is opened with a button
on another userform. has to be geneic. That is, to be able to be used with any new userform. I've gotten this far with the code below but can't figure out how
to make it generic.
Code:
Private Sub Genesis_Click()
TextBox1.Value = "Genesis"
End Sub

Private Sub Exodus_Click()
TextBox1.Value = "Exodus"
End Sub

Easy enough. But that would mean I'd have to code all 66 labels each with their given Bible book name and assign their value to Textbox1.
I want to use this for several different userforms, Userform1, Userform2 Userform3, etc, that have a Textbox as an input value of the label name to assign to a Find method. It seems there should be a much simpler, more elegant way to do this, either with a For Loop or using a Function call but I've not
figured out how to do this. The images below show what I've done. The operation is ver simple: a Book label name is clicked which displays a chapter userform which when any label is clicked displays a final verse userform. The last click completes putting the verse to Find in Textbox1 on the calling userform.
If I can cant this to work with just one userform, I can apply it to the other two.
Did not intended to over describe, but I wanted to be compete so as to give a full understanding of the endpoint to be accomplished: Textbox1.value = "Genesis 12:3"

Any help would be greatly appreciated.

Many thanks,
cr
 

Attachments

  • BIBLE BOOKS.png
    BIBLE BOOKS.png
    39.1 KB · Views: 14
  • BOOK CHAPTER VERSE FORMS ARE SHOWN WITH 1 CLICK.png
    BOOK CHAPTER VERSE FORMS ARE SHOWN WITH 1 CLICK.png
    125.4 KB · Views: 14

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Add a class module called LabelClicker:

VBA Code:
Public tb As msforms.TextBox
Public WithEvents lb As msforms.Label

Private Sub lb_Click()
   tb.Text = lb.Name
End Sub

then in your userform you need:

VBA Code:
Dim colHandler As Collection
Private Sub UserForm_Initialize()
   Dim ctl As msforms.Control
   Dim oHandler As LabelClicker
   Set colHandler = New Collection
   For Each ctl In Me.Controls
      If TypeName(ctl) = "Label" Then
         Set oHandler = New LabelClicker
         Set oHandler.lb = ctl
         Set oHandler.tb = Me.TextBox1
         colHandler.Add oHandler
      End If
   Next
      
End Sub
 
Upvote 0
Solution
Add a class module called LabelClicker:

VBA Code:
Public tb As msforms.TextBox
Public WithEvents lb As msforms.Label

Private Sub lb_Click()
   tb.Text = lb.Name
End Sub

then in your userform you need:

VBA Code:
Dim colHandler As Collection
Private Sub UserForm_Initialize()
   Dim ctl As msforms.Control
   Dim oHandler As LabelClicker
   Set colHandler = New Collection
   For Each ctl In Me.Controls
      If TypeName(ctl) = "Label" Then
         Set oHandler = New LabelClicker
         Set oHandler.lb = ctl
         Set oHandler.tb = Me.TextBox1
         colHandler.Add oHandler
      End If
   Next
     
End Sub
Works great, RoryA, thanks. So simple (if you know what to do :)
cr
 
Upvote 0
Hi again RoryA:
I have to ask for a little more help. First, with all due respect - you've helped me for the past years greatly. What I do when anyone
offers a solution is just copy the code as I did here and, as said, it works great. However - I've not learned anything - the what's this and why, etc. Per your code:

Code:
'Add a class module called LabelClicker:
Public tb As msforms.TextBox
Public WithEvents lb As msforms.Label

Private Sub lb_Click()
   tb.Text = lb.Name
End Sub

then in your userform you need:
VBA Code:
Dim colHandler As Collection
Private Sub UserForm_Initialize()
Dim ctl As msforms.Control
Dim oHandler As LabelClicker
Set colHandler = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "Label" Then
Set oHandler = New LabelClicker
Set oHandler.lb = ctl
Set oHandler.tb = Me.TextBox1 '---From what I can decipher, this line should put the first label value in a calling form textbox immediately and it does.  What then should happen is immediately upon the first clicked label on the first form(BOOKTABLE)  open another userform(BOOKCHAPTER) with numbers 1-100 as labels.  When a user clicks any number label, it immediately adds 1 space and that number value with a colon  to textbox11 of the calling form.   

Finally, with that click, it immediately opens another userform(CHAPTERVERSE) with label numbers 1 to 100.  When that last label on the CHAPTERVERSE userform, is clicked,   Textbox11 on the calling form is complete and looks like this:    Matthew 24:15. (or any other book-chapter verse combination depending on what's clicked in the previous forms.                                                                                                                                      

Another button named FIND performs the search .  I'd really like to learn how this code works and what exactly it does,  so I won't have to be so 
Message Board dependent for help.   

Below is the image of the userform sequence I am speaking of. I've accomplished but in a painfully way of hard coding 66 + 100+100 labels on three userforms - an 8 hour+ task for me.  Not to overburden you, but if you can help me with a generic solution that can be used for any textbox on any
userform in any application.  A better understanding  i.e, with brief comments per line, so I won't take any more of your time than necessary.  
Thanks as always for all your help.
cr


'then in your userform you need:
( I have no idea what his code does)
VBA Code:
Dim colHandler As Collection
Private Sub UserForm_Initialize()
Dim ctl As msforms.Control
Dim oHandler As LabelClicker
Set colHandler = New Collection
For Each ctl In Me.Controls
If TypeName(ctl) = "Label" Then
Set oHandler = New LabelClicker
Set oHandler.lb = ctl
Set oHandler.tb = Me.TextBox1
colHandler.Add oHandler
End If
Next
      
End Sub

colHandler.Add oHandler
End If
Next
      
End Sub
 

Attachments

  • SEQUENCE OF FORMS OPENING AS SOON AS PREVIOUS LABEL IS CLICKED.png
    SEQUENCE OF FORMS OPENING AS SOON AS PREVIOUS LABEL IS CLICKED.png
    140.8 KB · Views: 9
Upvote 0

Forum statistics

Threads
1,223,871
Messages
6,175,092
Members
452,612
Latest member
MESTeacher

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