跳到主要内容

如何只在Excel中的一组复选框中仅选中一个复选框?

如下面的屏幕截图所示,对于第2行中列出的一组复选框,当仅选中或选中一个复选框时,其他复选框将被禁用。 如何实现呢? 本文中的VBA代码可以为您提供帮助。

仅使用VBA代码选中一个复选框


仅使用VBA代码选中一个复选框

您可以运行以下VBA代码,每次仅选择一个复选框组中的一个复选框。 请执行以下操作。

1.首先,请根据需要插入复选框。 在这里,您应该插入 ActiveX控件复选框 如下图所示:

2.然后按 其他 + F11键同时打开 Microsoft Visual Basic应用程序 窗口。

3.在开幕 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 类模块.

4.将班级名称更改为 香港中文大学 在里面 (名字)的盒子 查看房源 窗格,然后将下面的VBA代码复制并粘贴到相应的 代码 窗口。 看截图:

VBA代码1:每次仅选择一个复选框

Option Explicit
Public WithEvents Chk As MSForms.CheckBox
Private Sub Chk_Click()
Call SelOneCheckBox(Chk)
End Sub

Sub SelOneCheckBox(Target As Object)
Dim xObj As Object
Dim I As String
Dim n As Integer
If Target.Object.Value = True Then

    I = Right(Target.Name, Len(Target.Name) - 8)
    For n = 1 To ActiveSheet.OLEObjects.Count
      If n <> Int(I) Then
        Set xObj = ActiveSheet.OLEObjects.Item(n)
        xObj.Object.Value = False
        xObj.Object.Enabled = False
      End If
    Next
Else
    I = Right(Target.Name, Len(Target.Name) - 8)
    For n = 1 To ActiveSheet.OLEObjects.Count
      If n <> Int(I) Then
        Set xObj = ActiveSheet.OLEObjects.Item(n)
        xObj.Object.Enabled = True
      End If
    Next
End If
End Sub

5.现在点击 插页 > 模块,然后将下面的VBA代码复制并粘贴到 模块 窗口。

VBA代码2:每次仅选择一个复选框

Dim xCollection As New Collection
Public Sub ClsChk_Init()
Dim xSht As Worksheet
Dim xObj As Object
Dim xChk As ClsChk
   Set xSht = ActiveSheet
   Set xCollection = Nothing
    For Each xObj In xSht.OLEObjects
        If xObj.Name Like "CheckBox**" Then
            Set xChk = New ClsChk
            Set xChk.Chk = CallByName(xSht, xObj.Name, VbGet)
            xCollection.Add xChk
        End If
    Next
    Set xChk = Nothing
End Sub

6。 按 F5 键来运行代码。

从现在起,检查工作表中的复选框中的任何一个时,其他复选框将自动关闭,并可以取消其再次激活所有复选框。

备注:如果将新复选框添加到复选框组,请重新运行VBA代码以再次激活所有复选框。 从复选框组中删除复选框也需要重新运行代码。


R兴高采烈的文章:

最佳办公生产力工具

🤖 Kutools 人工智能助手:基于以下内容彻底改变数据分析: 智能执行   |  生成代码  |  创建自定义公式  |  分析数据并生成图表  |  调用 Kutools 函数...
热门特色: 查找、突出显示或识别重复项   |  删除空白行   |  合并列或单元格而不丢失数据   |   不使用公式进行四舍五入 ...
超级查询: 多条件VLookup    多值VLookup  |   跨多个工作表的 VLookup   |   模糊查询 ....
高级下拉列表: 快速创建下拉列表   |  依赖下拉列表   |  多选下拉列表 ....
列管理器: 添加特定数量的列  |  移动列  |  切换隐藏列的可见性状态  |  比较范围和列 ...
特色功能: 网格焦点   |  设计图   |   大方程式酒吧    工作簿和工作表管理器   |  资源库 (自动文本)   |  日期选择器   |  合并工作表   |  加密/解密单元格    按列表发送电子邮件   |  超级筛选   |   特殊过滤器 (过滤粗体/斜体/删除线...)...
前 15 个工具集12 文本 工具 (添加文本, 删除字符,...)   |   50+ 图表 类型 (甘特图,...)   |   40+ 实用 公式 (根据生日计算年龄,...)   |   19 插入 工具 (插入二维码, 从路径插入图片,...)   |   12 转化 工具 (小写金额转大写, 货币兑换,...)   |   7 合并与拆分 工具 (高级组合行, 分裂细胞,...)   |   ... 和更多

使用 Kutools for Excel 增强您的 Excel 技能,体验前所未有的效率。 Kutools for Excel 提供了 300 多种高级功能来提高生产力并节省时间。  单击此处获取您最需要的功能...

产品描述


Office Tab 为 Office 带来选项卡式界面,让您的工作更加轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,发布者,Access,Visio和Project。
  • 在同一窗口的新选项卡中而不是在新窗口中打开并创建多个文档。
  • 每天将您的工作效率提高50%,并减少数百次鼠标单击!
Comments (14)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Bonjour, Merci pour ce tuto tout fonctionne. Cependant, je suis obligé d'éxécuter manuellement le "ClsChk" à chaque fois pour que cela fonctionne, mon fichier est un fichier groupe pour la société, serait-il possible qu'il se lance automatiquement ? Merci d'avance.
This comment was minimized by the moderator on the site
I want to get this feacture just row by row and not entire sheet.How should i fix this code alittle? I am not familiar with vba too much.If can, plz help me.
This comment was minimized by the moderator on the site
Hi Min Ko Ko,
If you want to select only one option row by row, I recommend you to use the combination of Option Button (Form Control) and Group Box (Form Control).
1. Just draw a Group Box in the worksheet.
2. Keep the Group Box selected, and then draw Option Buttons inside the box.
3. Repeat the steps to insert new Group box and Option Buttons in a new row.
4. As the group box has title and borders, you can apply the below VBA code in the Module code window to hide all titles and borders of group boxes in current worksheet.
<div data-tag="code">Sub ToggleVisible()
Dim myGB As GroupBox
For Each myGB In ActiveSheet.GroupBoxes
myGB.Visible = False
Next myGB
End Sub
This comment was minimized by the moderator on the site
You really need to explain how to access the class name in order to change it. BTW folks, it's F4 to open the window on the left side of the screen and then edit the class name.
This comment was minimized by the moderator on the site
hi,
the error appears under the first code.
xObj.Object.Value = False
This comment was minimized by the moderator on the site
Hi Arnold, did you get any prompt box? and which Excel version are you using?
This comment was minimized by the moderator on the site
It works fine if all you are using is checkboxes.
If you add any other activeX items and assign controls to them, it throws the error Arnold mentioned.
This comment was minimized by the moderator on the site
when i close the Excel and open the file again the checkboxes stop working how do I fix this pls?
This comment was minimized by the moderator on the site
Hi,
Please save the workbook as an Excel Macro-enabled Workbook in order to save the codes in workbook. But when you reopen the workbook, you need to get into the code window to manually run the code in the Module window to activate it.
This comment was minimized by the moderator on the site
Hi, is there a way around? I would like to use this for multiple people, and I am sure they will not run the code aftre opening....
This comment was minimized by the moderator on the site
Bonjour, Je suis dans la même situation. Est-il possible que le module de classe "ClsChk" se lance automatiquement au démarrage ? 
This comment was minimized by the moderator on the site
Hi, I have the same questionIs there any option to open the Excel with the function working?
This comment was minimized by the moderator on the site
Hi,Sorry for the trouble. You can follow the steps below to solve the problem.1. Save the workbook as an Excel Macro-enabled Workbook (Click File > Save as > select a destination folder > select "Excel Macro-Enabled Workbook" in the Save as type drop-down list > Save);2. Add the following code into the ThisWorkbook code editor as shown in the screenshot below.;3. Save the code.<div data-tag="code">Private Sub Workbook_Open()
On Error Resume Next
ClsChk_Init
End Sub

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
On Error Resume Next
ClsChk_Init
End Sub

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
ClsChk_Init
End Sub
This comment was minimized by the moderator on the site
Hi! Thanks for this useful guide! I have an additional question: Im working with both columns and rows. If one checkbox value is true, all checkboxes in the same row and column has to be disabled. How do I do that? Best, Morten
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations