如何以VBA形式动态更新标签标题? [英] How to dynamically update labels captions in VBA form?

查看:345
本文介绍了如何以VBA形式动态更新标签标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想根据存储在工作表中的值动态设置标签数组的标题(在VBA表单内).到目前为止,我可以像这样一一设置它们:

I want to dynamically set the caption for an array of labels (within a VBA form) based on values stored in a worksheet. Thus far I can set them one-by-one like this:

Label1.Caption = MySheet.Range("A1").Value
Label2.Caption = MySheet.Range("B1").Value
Label3.Caption = MySheet.Range("C1").Value ...

有很多遵循重复模式的标签,我想使用更智能的标签,例如:

Having lots of labels that follow a recurrent pattern, I want to use something smarter, like:

'Method1
For i = 1 To X
    Dim MyLabel as Object: Set MyLabel = "Label" & i
    MyLabel.Caption = MySheet.Cells(i + 1, i).Value
Next i
'Method2
For i = 1 To X
    Label(i).Caption = MySheet.Cells(i + 1, i).Value
Next I
'Both Methods failed. I really appreciate some feedback on this.

推荐答案

使用Controls对象

For i = 1 To X
    Controls("Label" & i).Caption =  MySheet.Cells(i + 1, i).Value
Next

这篇关于如何以VBA形式动态更新标签标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆