[Excel VBA] Mastering Checkbox Properties on Sheets: Caption and Value

Checkboxes in Excel’s “Form Controls” are essential UI (User Interface) elements for confirming task completion or toggling options on and off.

To control these via VBA, understanding their basic properties is crucial. This article explains the roles and usage of the two most important properties with sample code: Caption and Value.

目次

1. Caption Property

The Caption property gets or sets the descriptive text (label) displayed next to the checkbox.

Sample Code

' Change the caption of a specific checkbox
Sub ChangeCheckBoxCaption()
    Dim targetCheckBox As CheckBox
    
    ' Get the checkbox named "ConfirmCheck"
    Set targetCheckBox = ActiveSheet.CheckBoxes("ConfirmCheck")
    
    ' Change the display text using the .Caption property
    targetCheckBox.Caption = "Final Confirmation Complete"
    
    ' Display the current caption in a message box
    MsgBox "Changed caption to: " & targetCheckBox.Caption
End Sub

Explanation: By assigning a string to the .Caption property, you can freely modify the label text.

2. Value Property

The Value property gets or sets the current state of the checkbox. This property returns one of the following three values:

StateVBA ConstantNumeric ValueDescription
OnxlOn1The box is checked.
OffxlOff-4146The box is unchecked (default).
MixedxlMixed2State is mixed (common when linked to multiple cells).

Sample Code

The following code loops through all checkboxes on the sheet and outputs their names and states (On/Off) to the Immediate Window.

' Check the status of all checkboxes on the sheet
Sub CheckAllCheckBoxValues()
    Dim chkBox As CheckBox
    Dim statusText As String
    
    Debug.Print "--- Checkbox Status List ---"
    
    ' Loop through all checkboxes using the CheckBoxes collection
    For Each chkBox In ActiveSheet.CheckBoxes
        ' Determine status string based on the .Value property
        Select Case chkBox.Value
            Case xlOn
                statusText = "ON"
            Case xlOff
                statusText = "OFF"
            Case Else
                statusText = "MIXED"
        End Select
        
        ' Output the checkbox caption and status
        Debug.Print chkBox.Caption & ": " & statusText
    Next chkBox
End Sub

Explanation: The For Each loop iterates through every checkbox in the ActiveSheet.CheckBoxes collection, evaluating the .Value property to determine the current check state.

Summary

In this article, we covered the two fundamental properties for controlling checkboxes on a sheet via VBA:

  • .Caption: Controls the visible label text.
  • .Value: Controls the check state (xlOn, xlOff, xlMixed).

Mastering these properties allows you to create dynamic tools, such as changing cell values based on user selection or resetting all check states with a single click.

よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次