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

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

我有很多遵循循环模式的标签,我想使用更聪明的东西,例如:
'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 - 如何以VBA形式动态更新标签标题?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20677449/

10-13 02:14