How to Create a Custom Checkbox using only the Cells in Microsoft Excel

You might be creating templates with Microsoft Excel, and one of the feature you want is add a check boxes control where users can choose their option. 

We'll, if you want to have another look aside from the default check box control. You can still create and customize your check box controls with a little VBA code to get it done.

In this example, I have here choices for the reason of a leave application form. I make use of the Microsoft Excel's Cell as a check box. It's just a matter of resizing the rows and columns to make the size of the cell equal. 

1.) Now, let's create first the list of choices, as you can see below I've resized the cells and add borders on the cells which we'll set as a check-cell box control.

2.) Right Click on the Worksheet, then Click "View Code".
3.) Then Copy and Paste the following codes below into the code editor window.

The following code will set the Target Range of when the Check-Cell box will be triggered. I set the columns to G & H or it's numeric equivalent 7 & 8. Then create case statement of what rows it will run. The columns and rows may vary depending on your actual template. So you can set it to match your template.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    With Target       
        'SET THE TARGET COLUMN TO COLUMNS 7 AND 8
        If Target.Column >= 7 And Target.Column <= 8 Then
            'SELECT CASE OF WHAT ROW WILL BE SELECTED
            Select Case Target.Row
                Case 19 'SICK LEAVE
                    CheckSelected 19, 7
                Case 21 'VACATION LEAVE
                    CheckSelected 21, 7
                Case 23 'PERSONAL
                    CheckSelected 23, 7
                Case 25 'EMERGENCY
                    CheckSelected 25, 7
                Case 27 'APPOINTMENT
                    CheckSelected 27, 7
                Case 29 'OTHERS
                    CheckSelected 29, 7
                Case Default
            End Select
        End If
    End With
End Sub



'CHECK IF THE REASON IS CURRENTLY SELECTED
Sub CheckSelected(Row As Integer, Col As Integer)
    With Cells(Row, Col)
        If .Value = "+" Then
            .Value = ""
            ResetSelectedColor Row, Col
        Else
            .Value = "+"
            ApplySelectedColor Row, Col
        End If
    End With
End Sub



'CHANGE THE BACKGROUND OF THE SELECTION
Sub ApplySelectedColor(Row As Integer, Col As Integer)
    With Cells(Row, Col).Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent1
        .TintAndShade = 0.599993896298105
        .PatternTintAndShade = 0
    End With
End Sub


'REMOVE THE BACKGROUND OF THE SELECTION
Sub ResetSelectedColor(Row As Integer, Col As Integer)
    With Cells(Row, Col).Interior
        .Pattern = xlNone
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With
End Sub


4.) Now, if you select the Cells it should change the background with the "+" value. You may also change the value to whatever you want.

Comments

Post a Comment

Popular posts from this blog

How to Create a Configuration.INI Files in VB6

How to Set Windows Form Always on Top of Other Applications in VB6

Populate Data Into DataGridView using For-Loop in C#/CSharp