Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In an Excel UserForm, how do I update a label's caption?

I created my first modeless UserForm in Excel and put an ActiveX label on it. How do I set the caption of the label so that it displays whatever is in Sheet1.Range("A1"), and updates itself when the value in cell A1 changes?

Basically, I want the Userform's label to always be updated the second anything in the Excel cell changes. Thank you!

like image 453
phan Avatar asked Jul 28 '11 12:07

phan


People also ask

How do I change the caption on a UserForm?

Click on the form. The caption is changed. Then change the activesheet and click on the form again. Caption is changed again.

How do you change the text in a UserForm label in VBA?

Click the UserForm and look to the Toolbox (go to View > Toolbox if you don't see it). Click the A in the toolbox. Once you click the A, go to the form and click and drag until a label appears.

How do I change the title of a form in VBA?

I just change it in the userform properties. Select the userform, and under the properties change the Caption property to show whatever text you want.


1 Answers

Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Cells(1, 1), Target) Is Nothing Then
        Exit Sub
    End If
    UserForm1.Label1.Caption = Sheet1.Range("A1").Value
End Sub

The sub Change gets called everytime a cell changes. The code does this: if A1 was changed, change the caption of Label1 on UserForm1. The form must have been opened not modal (vbModeless).

UserForm1.Show vbModeless
like image 51
Jacob Avatar answered Oct 13 '22 05:10

Jacob