我在单元格中有一个带有CommandButtons的电子表格。我只想在没有单击按钮的情况下获取特定单元格中命令按钮的名称/ ID。 我该怎么办呢? 我一直在使用这个代码但效率很低,因为我必须逐个输入每个按钮ID。我有大约60个按钮。
Sub Worksheet_Calculate()
If Cells(6, 3).Value = 0 Then
Me.Buttons("Button 55").Enabled = False
Me.Buttons("Button 55").Font.ColorIndex = 16
Else
Me.Buttons("Button 55").Enabled = True
Me.Buttons("Button 55").Font.ColorIndex = 1
End If
If Cells(7, 3).Value = 0 Then
Me.Buttons("Button 61").Enabled = False
Me.Buttons("Button 61").Font.ColorIndex = 16
Else
Me.Buttons("Button 61").Enabled = True
Me.Buttons("Button 61").Font.ColorIndex = 1
End If
答案 0 :(得分:1)
一种方法是遍历工作表上的所有按钮,并检查TopLeftCell是否与您的单元格匹配。可能会陷入很多按钮,但我认为60会运行正常。如果你有重叠按钮可能会遇到问题,但你可以避免这种情况。这会查看所有Shapes
,可以缩小范围,但我不确定您使用哪种类型的按钮。
'In a worksheet module change Me. to a worksheet as needed.
Function findShape(r As Range) As String
Dim s As Shape
findShape = "Not Found" 'if loop goes all the way around and doesn't find it.
For Each s In Me.Shapes
If s.TopLeftCell.Address = r.Address Then
findShape = s.Name
Exit For 'no need to keep going
End If
Next
End Function