انتقل إلى المحتوى الرئيسي

كيفية إنشاء مربع اختيار واحد فقط ليتم تحديده في مجموعة مربعات الاختيار في Excel؟

كما هو موضح في لقطة الشاشة أدناه ، بالنسبة لمجموعة من مربعات الاختيار المدرجة في الصف 2 ، عند تحديد أو تحديد خانة اختيار واحدة فقط ، سيتم تعطيل مربعات الاختيار الأخرى. كيف يتم تحقيق ذلك؟ يمكن أن يساعدك رمز VBA في هذه المقالة.

قم بعمل مربع اختيار واحد فقط ليتم تحديده برمز VBA


قم بعمل مربع اختيار واحد فقط ليتم تحديده برمز VBA

يمكنك تشغيل رموز VBA أدناه لتحديد خانة اختيار واحدة فقط في مجموعة مربعات الاختيار في كل مرة. الرجاء القيام بما يلي.

1. أولاً ، يرجى إدخال مربعات الاختيار حسب حاجتك. هنا ، يجب عليك إدخال ملف خانات الاختيار عنصر تحكم ActiveX كما تظهر لقطة الشاشة التالية:

2. ثم اضغط قديم + F11 في نفس الوقت لفتح ملف ميكروسوفت فيسوال باسيك للتطبيقات نافذة.

3. في الافتتاح ميكروسوفت فيسوال باسيك للتطبيقات الإطار، انقر فوق إدراج > وحدة الفصل.

4. قم بتغيير اسم الفصل إلى ClsChk في ال (الاسم) مربع من عقارات ، ثم انسخ والصق رمز فبا أدناه في الملف المقابل رمز نافذة او شباك. انظر لقطة الشاشة:

كود فبا 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 أدناه في ملف وحدة نافذة.

كود فبا 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 رسم الأنواع (مخطط جانت، ...)   |   40+ عملي الصيغ (احسب العمر على أساس تاريخ الميلاد، ...)   |   19 إدخال الأدوات (أدخل رمز الاستجابة السريعة, إدراج صورة من المسار، ...)   |   12 تحويل الأدوات (أرقام إلى كلمات, نتيجة تحويل عملة، ...)   |   7 دمج وتقسيم الأدوات (الجمع بين الصفوف المتقدمة, تقسيم الخلايا، ...)   |   ... و اكثر

عزز مهاراتك في Excel باستخدام Kutools for Excel، واختبر كفاءة لم يسبق لها مثيل. يقدم Kutools for Excel أكثر من 300 ميزة متقدمة لتعزيز الإنتاجية وتوفير الوقت.  انقر هنا للحصول على الميزة التي تحتاجها أكثر...

الوصف


يجلب Office Tab الواجهة المبوبة إلى Office ، ويجعل عملك أسهل بكثير

  • تمكين التحرير والقراءة المبوبة في Word و Excel و PowerPointوالناشر والوصول و 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