我可以使用字符串引用对象吗? [英] Can I reference an object using a string?
问题描述
所以我的代码如下:
Dim i As Integer
Dim labelnum As String
For i = 1 To 81
labelnum = "Label" & i
If "labelnum".Caption = Label1.Caption Then
"labelnum".BackColor = Label1.BackColor
End If
Next i
我想循环浏览81个标签,以查看该标题中的标题是否与我选择的标题相同.我还能在标有"labelnum"的地方放些东西吗?
I want to loop through 81 labels to check to see if the caption in that one is the same as the one I have selected. Is there something else I can put where it says "labelnum"?
我正在练习并尝试通过VBA制作数独.我要突出显示已选择的框,并突出显示板上具有相同编号的所有其他正方形.
I'm practicing and trying to make sudoku through VBA. I want to highlight the box I have selected and highlight all other squares on the board that have the same number.
谢谢!
推荐答案
在工作表中,标签是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
这篇关于我可以使用字符串引用对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!