ไปยังเนื้อหาหลัก

จะทำอย่างไรให้เลือกช่องทำเครื่องหมายเพียงช่องเดียวในกลุ่มช่องทำเครื่องหมายใน Excel

ดังภาพหน้าจอด้านล่างสำหรับกลุ่มของช่องทำเครื่องหมายที่แสดงรายการในแถวที่ 2 เมื่อเลือกหรือเลือกช่องทำเครื่องหมายเพียงช่องเดียวช่องทำเครื่องหมายอื่นจะถูกปิดใช้งาน จะบรรลุได้อย่างไร? รหัส VBA ในบทความนี้สามารถช่วยคุณได้

เลือกช่องทำเครื่องหมายเพียงช่องเดียวด้วยรหัส VBA


เลือกช่องทำเครื่องหมายเพียงช่องเดียวด้วยรหัส VBA

คุณสามารถเรียกใช้รหัส VBA ด้านล่างเพื่อเลือกช่องทำเครื่องหมายเพียงช่องเดียวในกลุ่มช่องทำเครื่องหมายต่อครั้ง กรุณาดำเนินการดังนี้

1. ประการแรกโปรดใส่ช่องทำเครื่องหมายตามที่คุณต้องการ ที่นี่คุณควรแทรกไฟล์ กล่องกาเครื่องหมาย ActiveX Control ตามภาพหน้าจอต่อไปนี้:

2. จากนั้นกด อื่น ๆ + F11 พร้อมกันเพื่อเปิดไฟล์ Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่าง

3. ในการเปิด Microsoft Visual Basic สำหรับแอปพลิเคชัน หน้าต่างคลิก สิ่งที่ใส่เข้าไป > โมดูลคลาส.

4. เปลี่ยนชื่อคลาสเป็น ClsChk ใน (Name) กล่องของ อสังหาริมทรัพย์ บานหน้าต่างจากนั้นคัดลอกและวางรหัส 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 AI ผู้ช่วย: ปฏิวัติการวิเคราะห์ข้อมูลโดยยึดตาม: การดำเนินการที่ชาญฉลาด   |  สร้างรหัส  |  สร้างสูตรที่กำหนดเอง  |  วิเคราะห์ข้อมูลและสร้างแผนภูมิ  |  เรียกใช้ฟังก์ชัน Kutools...
คุณสมบัติยอดนิยม: ค้นหา เน้น หรือระบุรายการที่ซ้ำกัน   |  ลบแถวว่าง   |  รวมคอลัมน์หรือเซลล์โดยไม่สูญเสียข้อมูล   |   รอบโดยไม่มีสูตร ...
การค้นหาขั้นสูง: VLookup หลายเกณฑ์    VLookup หลายค่า  |   VLookup ข้ามหลายแผ่น   |   การค้นหาที่ไม่ชัดเจน ....
รายการแบบเลื่อนลงขั้นสูง: สร้างรายการแบบหล่นลงอย่างรวดเร็ว   |  รายการแบบหล่นลงขึ้นอยู่กับ   |  เลือกหลายรายการแบบหล่นลง ....
ผู้จัดการคอลัมน์: เพิ่มจำนวนคอลัมน์เฉพาะ  |  ย้ายคอลัมน์  |  สลับสถานะการมองเห็นของคอลัมน์ที่ซ่อนอยู่  |  เปรียบเทียบช่วงและคอลัมน์ ...
คุณสมบัติเด่น: กริดโฟกัส   |  มุมมองการออกแบบ   |   บาร์สูตรใหญ่    สมุดงานและตัวจัดการชีต   |  ห้องสมุดทรัพยากร (ข้อความอัตโนมัติ)   |  เลือกวันที่   |  รวมแผ่นงาน   |  เข้ารหัส/ถอดรหัสเซลล์    ส่งอีเมลตามรายการ   |  ซุปเปอร์ฟิลเตอร์   |   ตัวกรองพิเศษ (กรองตัวหนา/ตัวเอียง/ขีดทับ...) ...
ชุดเครื่องมือ 15 อันดับแรก12 ข้อความ เครื่องมือ (เพิ่มข้อความ, ลบอักขระ, ... )   |   50 + แผนภูมิ ประเภท (แผนภูมิ Gantt, ... )   |   40+ ใช้งานได้จริง สูตร (คำนวณอายุตามวันเกิด, ... )   |   19 การแทรก เครื่องมือ (ใส่ QR Code, แทรกรูปภาพจากเส้นทาง, ... )   |   12 การแปลง เครื่องมือ (ตัวเลขเป็นคำ, การแปลงสกุลเงิน, ... )   |   7 ผสานและแยก เครื่องมือ (แถวรวมขั้นสูง, แยกเซลล์, ... )   |   ... และอื่น ๆ

เพิ่มพูนทักษะ Excel ของคุณด้วย Kutools สำหรับ Excel และสัมผัสประสิทธิภาพอย่างที่ไม่เคยมีมาก่อน Kutools สำหรับ Excel เสนอคุณสมบัติขั้นสูงมากกว่า 300 รายการเพื่อเพิ่มประสิทธิภาพและประหยัดเวลา  คลิกที่นี่เพื่อรับคุณสมบัติที่คุณต้องการมากที่สุด...

รายละเอียด


แท็บ Office นำอินเทอร์เฟซแบบแท็บมาที่ Office และทำให้งานของคุณง่ายขึ้นมาก

  • เปิดใช้งานการแก้ไขและอ่านแบบแท็บใน Word, Excel, PowerPoint, ผู้จัดพิมพ์, Access, Visio และโครงการ
  • เปิดและสร้างเอกสารหลายรายการในแท็บใหม่ของหน้าต่างเดียวกันแทนที่จะเป็นในหน้าต่างใหม่
  • เพิ่มประสิทธิภาพการทำงานของคุณ 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