【发布时间】:2020-03-19 22:25:04
【问题描述】:
该宏旨在查找主复选框 (application.caller) 的位置,然后选择该列中最多 14 行以下的所有复选框。我的工作表在其他地方有各种复选框,当前宏检查工作表中的所有复选框。我做错了什么?
Sub SelectAll_Click()
'Select / Clear All macro
Dim xCheckBox As CheckBox, n As Variant, rng As Range, loc As Range, loc1 As Range
'Application.Caller.Name
n = ActiveSheet.CheckBoxes(Application.Caller).Name
With ActiveSheet
Set loc = ActiveSheet.CheckBoxes(Application.Caller).TopLeftCell
'Set loc1 = loc.Address
'MsgBox loc1
loc.Select
Set rng = Range(loc.Address, ActiveCell.Offset(13, 0))
MsgBox (rng.Address)
End With
For Each xCheckBox In Application.ActiveSheet.CheckBoxes
'MsgBox (xCheckBox.TopLeftCell.Address)
'With xCheckBox.Select
If Not Intersect(loc, rng) Is Nothing Then
If xCheckBox.Name <> Application.ActiveSheet.CheckBoxes(n).Name Then
' If Not Intersect(rngShp, rng) Is Nothing Then
xCheckBox.Value = Application.ActiveSheet.CheckBoxes(n).Value
End If
End If
'End With
Next
End Sub
【问题讨论】:
-
你没有检查
xCheckBox的位置。 -
您好@SJR,感谢您的评论,您能说得更具体点吗?
-
你不需要检查
rng和xcheckbox的交集吗? -
另外我建议不要使用
ActiveCell,也不要使用.Select,参见How to avoid using Select in Excel VBA。这很容易失败并且不是很可靠。 -
我尝试更改交点值,但出现错误。我如何编写确切的代码?