|
楼主 |
发表于 2009-8-23 19:32
|
显示全部楼层
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
原帖由 amulee 于 2009-7-28 15:06 发表
amulee 给出了4种方案,其中两种方案使用了类模块。
首先,插入一个名为MyCls的类模块,代码如下:
Private MyShape As Shape
Public Sub Attach(MyObj)
Set MyShape = MyObj
SetShape
End Sub
Private Sub SetShape()
Dim i%, ColorInd&
i = Mid(MyShape.Name, 9, Len(MyShape.Name))
With MyShape
.Left = Sheet1.Cells(6, (i \ 9) * 3 + 3).Left
.Top = Sheet1.Cells(6, (i \ 9) * 3 + 3).Top + (IIf(i > 8, i - 8, i) - 1) * .Height
ColorInd = WorksheetFunction.Choose((i Mod 3) + 1, RGB(0, 255, 255), RGB(255, 0, 0), RGB(0, 0, 255))
.OLEFormat.Object.Object.Caption = .Name
.OLEFormat.Object.LinkedCell = "IV" & i
.OLEFormat.Object.Object.Value = Not .OLEFormat.Object.Object.Value
.OLEFormat.Object.Object.ForeColor = ColorInd
End With
End Sub
之后,在标准模块中,使用如下四种方案
Dim chkbox2(1 To 16) As New MyCls
Sub 方法1() '直接设置,直接找出各个复选框
Dim i%, ColorInd&, chkbox(1 To 16)
For i = 1 To 8
Set chkbox(i) = Sheet1.Shapes("CheckBox" & i)
Next i
For i = 9 To 16
Set chkbox(i) = Sheet1.Shapes("组合 42").GroupItems.Item(i - 8)
Next i
For i = 1 To 16
With chkbox(i)
.Left = Sheet1.Cells(6, (i \ 9) * 3 + 3).Left
.Top = Sheet1.Cells(6, (i \ 9) * 3 + 3).Top + (IIf(i > 8, i - 8, i) - 1) * .Height
ColorInd = WorksheetFunction.Choose((i Mod 3) + 1, RGB(0, 255, 255), RGB(255, 0, 0), RGB(0, 0, 255))
.OLEFormat.Object.Object.Caption = .Name
.OLEFormat.Object.LinkedCell = "IV" & i
.OLEFormat.Object.Object.Value = Not .OLEFormat.Object.Object.Value
.OLEFormat.Object.Object.ForeColor = ColorInd
End With
Next i
End Sub
Sub 方法2() '类模块
Dim i%
For i = 1 To 8
chkbox2(i).Attach Sheet1.Shapes("CheckBox" & i)
Next i
For i = 9 To 16
chkbox2(i).Attach Sheet1.Shapes("组合 42").GroupItems.Item(i - 8)
Next i
End Sub
Sub 方法3() '通过图形的类别找出复选框
Dim i%, obj, obj2
i = 1
For Each obj In Sheet1.Shapes
If obj.Type = 12 Then
chkbox2(i).Attach obj
i = i + 1
End If
If obj.Type = 6 Then
For Each obj2 In obj.GroupItems
If obj2.Type = 12 Then
chkbox2(i).Attach obj2
i = i + 1
End If
Next
End If
Next
End Sub
Sub 方法4() '取消组合,设置完成后再组合
Dim i%, obj, Arr(9 To 16)
For Each obj In Sheet1.Shapes '取消组合
If obj.Type = 6 Then obj.Ungroup
Next
For i = 1 To 16 '通过类模块设置对象
chkbox2(i).Attach Sheet1.Shapes("CheckBox" & i)
Next i
For i = 9 To 16 '重新组合
Arr(i) = "CheckBox" & i
Next i
Sheet1.Shapes.Range(Arr).Select
Selection.ShapeRange.Group.Name = "组合 42"
End Sub
老朽意见:经测试,4种方法均符合要求,建议给2分
[ 本帖最后由 zldccmx 于 2009-8-23 21:21 编辑 ] |
|