Direkt zum Inhalt

Wie kann nur ein Kontrollkästchen in einer Gruppe von Kontrollkästchen in Excel aktiviert werden?

Wie der folgende Screenshot zeigt, werden für eine Gruppe von Kontrollkästchen in Zeile 2 die anderen Kontrollkästchen deaktiviert, wenn nur ein Kontrollkästchen ausgewählt oder aktiviert wird. Wie erreicht man das? Der VBA-Code in diesem Artikel kann Ihnen helfen.

Aktivieren Sie nur ein Kontrollkästchen mit VBA-Code


Aktivieren Sie nur ein Kontrollkästchen mit VBA-Code

Sie können die folgenden VBA-Codes ausführen, um jeweils nur ein Kontrollkästchen in einer Kontrollkästchengruppe auszuwählen. Bitte gehen Sie wie folgt vor.

1. Bitte setzen Sie zunächst die Kontrollkästchen nach Bedarf ein. Hier sollten Sie die einfügen Kontrollkästchen für ActiveX Control wie folgt Screenshot gezeigt:

2. Drücken Sie dann Andere + F11 gleichzeitig zum Öffnen der Microsoft Visual Basic für Applikationen Fenster.

3. In der Öffnung Microsoft Visual Basic für Applikationen Klicken Sie im Fenster Insert > Klassenmodul.

4. Ändern Sie den Klassennamen in ClsChk in dem (Name) Box der Ferienhäuser Kopieren Sie den folgenden VBA-Code und fügen Sie ihn in den entsprechenden Code ein Code Fenster. Siehe Screenshot:

VBA-Code 1: Aktivieren Sie jeweils nur ein Kontrollkästchen

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. Klicken Sie nun auf Insert > ModulKopieren Sie dann den folgenden VBA-Code und fügen Sie ihn in das ein Modul Fenster.

VBA-Code 2: Aktivieren Sie jeweils nur ein Kontrollkästchen

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. Drücken Sie die Taste F5 Schlüssel zum Ausführen des Codes.

Wenn Sie von nun an eines der Kontrollkästchen im Arbeitsblatt aktivieren, werden die anderen Kontrollkästchen automatisch deaktiviert, und Sie können es deaktivieren, um alle Kontrollkästchen wieder zu aktivieren.

Note: Wenn der Kontrollkästchengruppe ein neues Kontrollkästchen hinzugefügt wird, führen Sie den VBA-Code erneut aus, um alle Kontrollkästchen erneut zu aktivieren. Wenn Sie das Kontrollkästchen aus der Kontrollkästchengruppe löschen, muss der Code ebenfalls erneut ausgeführt werden.


Rbegeisterte Artikel:

Beste Office-Produktivitätstools

🤖 Kutools KI-Assistent: Revolutionieren Sie die Datenanalyse basierend auf: Intelligente Ausführung   |  Code generieren  |  Erstellen Sie benutzerdefinierte Formeln  |  Analysieren Sie Daten und erstellen Sie Diagramme  |  Rufen Sie Kutools-Funktionen auf...
Beliebte Funktionen: Suchen, markieren oder identifizieren Sie Duplikate   |  Leere Zeilen löschen   |  Kombinieren Sie Spalten oder Zellen, ohne Daten zu verlieren   |   Runde ohne Formel ...
Super-Lookup: VLookup mit mehreren Kriterien    VLookup mit mehreren Werten  |   VLookup über mehrere Blätter hinweg   |   Unscharfe Suche ....
Erweiterte Dropdown-Liste: Erstellen Sie schnell eine Dropdown-Liste   |  Abhängige Dropdown-Liste   |  Mehrfachauswahl Dropdown-Liste ....
Spaltenmanager: Fügen Sie eine bestimmte Anzahl von Spalten hinzu  |  Spalten verschieben  |  Schalten Sie den Sichtbarkeitsstatus ausgeblendeter Spalten um  |  Vergleichen Sie Bereiche und Spalten ...
Ausgewählte Funktionen: Rasterfokus   |  Designansicht   |   Große Formelleiste    Arbeitsmappen- und Blattmanager   |  Ressourcen (Autotext)   |  Datumsauswahl   |  Arbeitsblätter kombinieren   |  Zellen verschlüsseln/entschlüsseln    Senden Sie E-Mails nach Liste   |  Superfilter   |   Spezialfilter (Filter fett/kursiv/durchgestrichen...) ...
Top 15 Toolsets12 Text Tools (Text hinzufügen, Zeichen entfernen, ...)   |   50+ Chart Typen (Gantt-Diagramm, ...)   |   40+ Praktisch Formeln (Berechnen Sie das Alter basierend auf dem Geburtstag, ...)   |   19 Einfügen Tools (QR-Code einfügen, Bild aus Pfad einfügen, ...)   |   12 Umwandlung (Conversion) Tools (Zahlen zu Wörtern, Currency Conversion, ...)   |   7 Zusammenführen & Teilen Tools (Erweiterte Zeilen kombinieren, Zellen teilen, ...)   |   ... und mehr

Verbessern Sie Ihre Excel-Kenntnisse mit Kutools für Excel und erleben Sie Effizienz wie nie zuvor. Kutools für Excel bietet über 300 erweiterte Funktionen, um die Produktivität zu steigern und Zeit zu sparen.  Klicken Sie hier, um die Funktion zu erhalten, die Sie am meisten benötigen ...

Beschreibung


Office Tab Bringt die Oberfläche mit Registerkarten in Office und erleichtert Ihnen die Arbeit erheblich

  • Aktivieren Sie das Bearbeiten und Lesen von Registerkarten in Word, Excel und PowerPoint, Publisher, Access, Visio und Project.
  • Öffnen und erstellen Sie mehrere Dokumente in neuen Registerkarten desselben Fensters und nicht in neuen Fenstern.
  • Steigert Ihre Produktivität um 50 % und reduziert jeden Tag Hunderte von Mausklicks für Sie!
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