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

三世轮回 提交于 2019-12-23 10:58:08

问题


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!


回答1:


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



回答2:


This Worked for me.

Sheets("Sheet").Shapes("TheNameOfTheLabel").TextFrame.Characters.Text = "Hello"


来源:https://stackoverflow.com/questions/6858747/in-an-excel-userform-how-do-i-update-a-labels-caption

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!