且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以使用字符串引用对象吗?

更新时间:2022-06-19 23:42:45

在工作表中,标签是Shape对象,因此可以使用Shapes集合:

In a Worksheet, a Label is a Shape Object, so you can use the Shapes collection:

Dim i As Integer
Dim shpLabel As Shape

For i = 1 To 81
    Set shpLabel = Sheet1.Shapes("labelnum" & i)
    If shpLabel.Caption = Label1.Caption Then
        shpLabel.BackColor = Label1.BackColor
    End If
    Set shpLabel = Nothing
Next i

 
在用户窗体中,标签是Control对象,因此可以使用Controls集合:

 
In a UserForm, a Label is a Control Object, so you can use the Controls collection:

Dim i As Integer
Dim ctrlLabel As Control

For i = 1 To 81
    Set ctrlLabel = Me.Controls("labelnum" & i)
    If ctrlLabel.Caption = Label1.Caption Then
        ctrlLabel.BackColor = Label1.BackColor
    End If
    Set ctrlLabel = Nothing
Next i