Consulting

Results 1 to 9 of 9

Thread: Checking Checkbox values

  1. #1

    Checking Checkbox values

    Hi.
    I've got a large number (91) of checkboxes and I need to figure out a way to loop through them and check their values. They are control checkboxes, and I've named them using this paradigm: chkTime# (where the # are numbered 1 to 91).
    Any help would be great.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by psittacus
    Hi.
    I've got a large number (91) of checkboxes and I need to figure out a way to loop through them and check their values. They are control checkboxes, and I've named them using this paradigm: chkTime# (where the # are numbered 1 to 91).
    Any help would be great.
    Here is some code that shows how to uncheck them all

    Sub Uncheck()
    Dim oCB As OLEObject 
        With Sheets("Sheet1")
            For Each oCB In .OLEObjects
                If TypeName(oCB.Object) = "CheckBox" Then
                    oCB.Object.Value = False
                End If
            Next CB
        End With
    End Sub

  3. #3
    Are you talking about checking and unchecking them or are you talking about checking the value as in yes it is checked or no it is not checked ?

  4. #4
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Why do you have so many?

  5. #5
    you could start with something like this
    I found it on the web and edited it a bit but it will give you a msg box each time a box is checked

    Option Explicit 
     
    Sub check_checkbox() 
              Dim obj As OLEObject 
    For Each obj In ActiveSheet.OLEObjects 
                    If obj.progID = "Forms.CheckBox.1" Then 
                         If obj.Object.Value = True Then 
                              MsgBox obj.Name & " is checked" 
                End If 
            End If 
        Next obj 
    End Sub

  6. #6
    Sorry, I should have been more clear. I meant checking their values (Yes or No).

    It's really to keep track of which are checked and then store that info in an array.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by psittacus
    Sorry, I should have been more clear. I meant checking their values (Yes or No).

    It's really to keep track of which are checked and then store that info in an array.
    They don't have a value of Yes or N o, they are either checked, True, or not, False. Both solutions show you how to do it, so try it.

  8. #8
    VBAX Newbie
    Joined
    Apr 2005
    Posts
    1
    Location
    'scan all control in your userform
     For Each Control In frmF1.Controls
    If Mid(Control.Name, 1, 3) = "chk" Then
    ' check value of current control If Control.Value = True Then
    Dowhatever
    End If ' the same thing is to check the corresponding cell str1 = Mid(Control.ControlSource, 1, 6) If Worksheets("S1").Range(str1) = True Then
    Dowhatever
    End If
    End If
    Next Control

  9. #9
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Here's an old KB item I never got round to submitting. It may include some code/methods you can make use of.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •