vba 在 Excel 用户窗体中,如何更新标签的标题?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6858747/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:41:41  来源:igfitidea点击:

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

excelvbalabelcaptionuserform

提问by phan

I created my first modeless UserFormin 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?

UserForm在 Excel 中创建了我的第一个无模式并在其上放置了一个 ActiveX 标签。如何设置标签的标题,以便它显示 中的任何内容Sheet1.Range("A1"),并在单元格 A1 中的值更改时自动更新?

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

基本上,我希望Userform's标签总是在 Excel 单元格中的第二个任何更改时更新。谢谢!

采纳答案by Jacob

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 Changegets called everytime a cell changes. The code does this: if A1was changed, change the caption of Label1on UserForm1. The form must have been opened not modal (vbModeless).

Change每次单元格更改时都会调用sub 。代码是这样做的:如果A1被更改,则更改Label1on的标题UserForm1。必须已打开该表单而不是模态 ( vbModeless)。

UserForm1.Show vbModeless

回答by moberme

This Worked for me.

这对我有用。

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