1

我正在使用带有复选框的 Excel 中的 WBS。

我有以下内容:

[checkbox1] A 级
---------[checkBox2] 项目 1
---------[checkBox3] 项目 2
[checkbox4] B 级
---------[checkBox5]第 3 项

当我取消勾选 checkbox2 时,它会在项目 1 旁边的单元格中放置一个 X 如果我勾选 checkbox2,它将删除 X。

如果我取消勾选 checkbox1,它将取消勾选 checkbox2 和 checkbox3,但它不会在项目 1 和 2 旁边的单元格中放置 X。它只是取消勾选两个复选框而不触发事件。如何将该事件链接到 checkBox1?

如果不可能在表单控件中触发这种事件,我的其他问题是知道如何知道复选框在 ActiveX 控件中所在的行和列?

在表单控件中我们可以使用sheets("sheet1").checkboxes(application.caller),但这在 ActiveX 控件中不起作用。

checkbox2 或 checkbox3 的代码:

Sub CheckBoxLine()
Dim ws As Worksheet
Dim chk As CheckBox
Dim lColD, myCol As Long
Dim lColChk As Long
Dim lRow As Long
Dim rngD As Range
lColD = 1 'number of columns to the right
Set ws = ActiveSheet
Set chk = ws.CheckBoxes(Application.Caller)
lRow = chk.TopLeftCell.Row
lColChk = chk.TopLeftCell.Column
Set rngD = ws.Cells(lRow, lColChk + lColD)

Select Case chk.Value
Case 1   'box is checked
  rngD.Value = "X"
Case Else   'box is not checked
  rngD.Value = "X"

End Select

End Sub

复选框1的代码:

Select Case chk.Value
Case 1   'box is checked
  For Each cb In ws.CheckBoxes
    If cb.Name = "Check box 2" Then
        cb.Value = 1
    End If

    If cb.Name = "Check box 3" Then
        cb.Value = 1
    End If
  Next cb

Case Else   'box is not checked
  For Each cb In ws.CheckBoxes
    If cb.Name = "Check box 2" Then
        cb.Value = 0
    End If

    If cb.Name = "Check box 3" Then
        cb.Value = 0
    End If
  Next cb
End Select
4

1 回答 1

0

澄清后答案改变了。

我认为表单控件没有事件。AFAIK 获取 ActiveX 控件所在的单元格有点复杂。我在我的一个工作簿中完成了它,但是它需要在一个附加的 Class 模块中添加一些代码,我不记得如何实现它。

由于您使用表单控件复选框,我认为使用以下代码会更容易,而不是使用新创建的 ActiveX 复选框中的事件。我希望它能按你的需要工作。

Option Explicit
Dim ws As Worksheet

Sub CheckBoxLine(Optional strChkName As String)
    Dim chk As CheckBox
    Dim lColD, myCol As Long
    Dim lColChk As Long
    Dim lRow As Long
    Dim rngD As Range

    lColD = 1 'number of columns to the right
    If ws Is Nothing Then Set ws = ActiveSheet
    If strChkName = vbNullString Then
        Set chk = ws.CheckBoxes(Application.Caller)
    Else
        Set chk = ws.CheckBoxes(strChkName)
    End If
    lRow = chk.TopLeftCell.Row
    lColChk = chk.TopLeftCell.Column
    Set rngD = ws.Cells(lRow, lColChk + lColD)

    Select Case chk.Value
    Case 1   'box is checked
      rngD.Value = vbNullString
    Case Else   'box is not checked
      rngD.Value = "X"
    End Select

    Set chk = Nothing
    Set ws = Nothing
End Sub

Sub Code_for_Checkbox1()
    Set ws = ActiveSheet
    ws.CheckBoxes("Check Box 2").Value = ws.CheckBoxes(Application.Caller).Value
    ws.CheckBoxes("Check Box 3").Value = ws.CheckBoxes(Application.Caller).Value
    Call CheckBoxLine("Check Box 2")
    Call CheckBoxLine("Check Box 3")
End Sub
于 2015-12-29T07:43:58.887 回答