Make only one checkbox tickable in a row?

夙愿已清 提交于 2019-12-19 12:24:12

问题


I have 5 to 6 check boxes (active x) in a row and I have 50 rows.

Is there a way to make only one checkbox tick-able in a row ( only one answer either 1,2,3,4 or 5)?.

Any simple vba to do this , I don't want to write a code to every checkbox.


回答1:


Yes, it can be done BUT Why use ActiveX Controls and so much extra coding? Why not data validation list? See this screenshot

If you still want a VBA Solution then I would recommend using FORM Controls and use their ALT Text to configure it. I infact have in one of the SO posts shown how to use group similar controls using Alt Text

EDIT:

If you want to go down the VBA road then here is another alternative, which DOESN'T use any FORM/Active X controls

Arrange the sheet as shown in the image below.

Now paste this code in the Sheet Code Area

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Cells.CountLarge > 1 Then Exit Sub

    If Not Intersect(Target, Columns(2)) Is Nothing Then ClearCells Target.Row, 2
    If Not Intersect(Target, Columns(4)) Is Nothing Then ClearCells Target.Row, 4
    If Not Intersect(Target, Columns(6)) Is Nothing Then ClearCells Target.Row, 6
    If Not Intersect(Target, Columns(8)) Is Nothing Then ClearCells Target.Row, 8
    If Not Intersect(Target, Columns(10)) Is Nothing Then ClearCells Target.Row, 10
End Sub

Sub ClearCells(r As Long, c As Long)
    For i = 2 To 10 Step 2
        If i <> c Then
            With Cells(r, i)
                .Borders(xlDiagonalDown).LineStyle = xlNone
                .Borders(xlDiagonalUp).LineStyle = xlNone
                .ClearContents
            End With
        End If
    Next i
    With Cells(r, c)
        With .Borders(xlDiagonalDown)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThin
        End With
        With .Borders(xlDiagonalUp)
            .LineStyle = xlContinuous
            .ColorIndex = xlAutomatic
            .TintAndShade = 0
            .Weight = xlThin
        End With
    End With
End Sub

Now all the user has to do is select any of the grey cell and it will be Crossed Out. Also if there is any other cross in the same row then it will be removed.

SAMPLE FILE



来源:https://stackoverflow.com/questions/19658251/make-only-one-checkbox-tickable-in-a-row

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!