Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在VBA表单中动态更新标签标题?_Vba_Excel_Label - Fatal编程技术网

如何在VBA表单中动态更新标签标题?

如何在VBA表单中动态更新标签标题?,vba,excel,label,Vba,Excel,Label,我想根据工作表中存储的值动态设置标签数组(在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 M

我想根据工作表中存储的值动态设置标签数组(在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.

使用
控件
对象

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

如果要在VBA中使用此选项:

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

如果标签不在UserForm内,而是在工作表上,是否也可以这样做?工作表上有哪些控件?表单控件还是activex控件?你可能想发布一个新问题,这样它不仅可以帮助你,也可以帮助未来的访问者?