Excel Userform VBA: Check Label Tag Property & Replace its Caption

MrRajKumar

Active Member
Joined
Jan 29, 2008
Messages
281
Office Version
  1. 365
Platform
  1. Windows
Hi,

I am designing an userform. So basically i would like to get the following.

Design:
A simple form to data entry. I added a label on underneath of each control. If there is any error its caption shows errors in red color, so they can fix it.
So when the form initialize, I used a code to replace caption of every labels (error lablels) to a 'space'.

Question:
Looking for a function to check all labels 'tag' property. if the tag value is 'error' then change their caption to 'specific' value.

Eg:

ChangeLabelCaption(TagValue,NewCaption)
ChangeLableCapton("error"," ")

So I can call this function, rather to add a code for each label in the userform.

I hope my question is clear. If not, sorry, please feel free to ask.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try...

VBA Code:
Private Sub ChangeLabelCaptions(ByVal TagValue As String, ByVal NewCaption As String)

    Dim ctrl As Control
    
    For Each ctrl In Me.Controls
        If TypeName(ctrl) = "Label" Then
            If LCase$(ctrl.Tag) = LCase$(TagValue) Then
                ctrl.Caption = NewCaption
            End If
        End If
    Next ctrl
    
End Sub

Then call the procedure as follows...

VBA Code:
    ChangeLabelCaptions "Error", ""

Hope this helps!
 
Upvote 0
Thank you Domenic for the help and sorry for the delay.

But I got error when run this procedure. It highlight the Me part.

How can I make this as global, then run on any userform in the project?
 
Upvote 0
Is this what you mean? If not, can you please specify the logic that's involved?

VBA Code:
Sub test()

    ChangeLabelCaptions UserForm1, "Error", ""
  
    UserForm1.Show
  
End Sub

Sub ChangeLabelCaptions(ByVal uf As Object, ByVal TagValue As String, ByVal NewCaption As String)

    Dim ctrl As msforms.Control
  
    For Each ctrl In uf.Controls
        If TypeName(ctrl) = "Label" Then
            If LCase$(ctrl.Tag) = LCase$(TagValue) Then
                ctrl.Caption = NewCaption
            End If
        End If
    Next ctrl
  
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,429
Messages
6,124,842
Members
449,193
Latest member
MikeVol

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