【发布时间】:2016-05-06 23:54:30
【问题描述】:
我已经很好地扫描了 SO,但找不到解决方案。
- 我的 Excel 工作簿中有许多工作表
- 每个工作表都有相同的 ActiveX 切换按钮集合
- 这些切换按钮控制数据透视图中数据的过滤。
过滤“1”或“全部” - 我希望切换按钮在单击时改变颜色。
当所有代码都与工作表连接时,我可以正常工作。
这里我将“tgl_butt”作为按钮名称传递
d
dim Ctrl as OLEObject
With ActiveSheet
'
' change button colour
'
For Each Ctrl In OLEObjects
If TypeName(Ctrl.Object) = "ToggleButton" Then
If Ctrl.Name = Tgl_butt Then
If BooValue = True Then
OLEObjects(Ctrl.Name).Object.BackColor = RGB(255, 255, 0) ' in = yellow
Else
OLEObjects(Ctrl.Name).Object.BackColor = RGB(184, 204, 228) 'out = blue
End If
End If
End If
Next Ctrl
End With
我现在正尝试将此代码放入一个模块中,以便所有包含切换按钮的工作表都可以共享它。
Call do_filter(Tgl_but.Value, "Tgl_name", Ctrl)
然后在模块中..
Public Sub do_filter(BooValue As Boolean, Tgl_butt As String,Ctrl As OLEObject)
With ActiveSheet
Dim myCtrl 'As OLEObject
For Each myCtrl In Ctrl
If TypeName(myCtrl.Object) = "ToggleButton" Then
If myCtrl.Name = Tgl_butt Then
If BooValue = True Then
Ctrl(myCtrl.Name).Object.BackColor = RGB(255, 255, 0) ' in = yellow
Else
Ctrl(myCtrl.Name).Object.BackColor = RGB(184, 204, 228) 'out = blue
End If
End If
End If
Next myCtrl
End With
上面的代码出现“需要对象”错误。 Ctrl 对象是空的......所以我想我在某个地方出现了严重错误!我已经尝试了该代码的多种变体,但似乎无法使其正常工作!欢迎所有建议!
【问题讨论】: