VBA: 在表单控件中触发一个事件
VBA: Trigger an Event in Form Control
我正在 Excel 中使用复选框处理 WBS。
我有以下内容:
[checkbox1] Level A
---------[checkBox2] item 1
---------[checkBox3] item 2
[checkbox4] Level B
---------[checkBox5] item 3
当我取消选中复选框 2 时,它会在项目 1 旁边的单元格中放置一个 X
如果我勾选 checkbox2,它会删除 X.
如果我取消选中复选框 1,它将取消选中复选框 2 和复选框 3,但它不会在项目 1 和 2 旁边的单元格中放置 X。它只是取消选中两个复选框而不触发事件。我如何 link 该事件到 checkBox1?
如果无法在表单控件中触发此类事件,我的其他问题将是知道如何知道复选框所在的行和列
在 ActiveX 控件中?
在 Form Control 中我们可以使用 sheets("sheet1").checkboxes(application.caller)
,但这在 ActiveX Control 中不起作用。
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
checkbox1 的代码:
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
澄清后更改了答案。
我认为没有表单控件的事件。而且 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
我正在 Excel 中使用复选框处理 WBS。
我有以下内容:
[checkbox1] Level A
---------[checkBox2] item 1
---------[checkBox3] item 2
[checkbox4] Level B
---------[checkBox5] item 3
当我取消选中复选框 2 时,它会在项目 1 旁边的单元格中放置一个 X 如果我勾选 checkbox2,它会删除 X.
如果我取消选中复选框 1,它将取消选中复选框 2 和复选框 3,但它不会在项目 1 和 2 旁边的单元格中放置 X。它只是取消选中两个复选框而不触发事件。我如何 link 该事件到 checkBox1?
如果无法在表单控件中触发此类事件,我的其他问题将是知道如何知道复选框所在的行和列 在 ActiveX 控件中?
在 Form Control 中我们可以使用 sheets("sheet1").checkboxes(application.caller)
,但这在 ActiveX Control 中不起作用。
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
checkbox1 的代码:
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
澄清后更改了答案。
我认为没有表单控件的事件。而且 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