Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Word VBA - finding which order checkboxes are selected 1

Status
Not open for further replies.

stevio

Vendor
Jul 24, 2002
78
AU
Hi there, I have a form which has a number of checkboxes. Is it possible to capture and store the order in which the checkboxes were selected in an array?

So if there are 20 checkboxes and the user selects CheckBox1, CheckBox3, Checkbox18, I want to capture the Control.Value (which I assume is the Checkbox name) and place into an array in the same order it was selected

The code so far is:

Code:
Dim Ctl As Control
           For Each Ctl In DocProperties.Controls
            If TypeOf Ctl Is MSForms.CheckBox Then
                If Ctl Then ChkCount = ChkCount + 1
                        If Ctl.Value = True Then
                            ReDim Preserve myArray(ChkCount)
                            myArray(ChkCount) = Ctl.Caption
                            
                        End If
                End If
            Next
 
Sorry, a VBA Userform in Word.

Cheers
Steve
 
What happens when the user checks/unchecks the same checkbox multiple times?

Cheers
Paul Edstein
[MS MVP - Word]
 
Here's one example, which assumes that the last time an item is checked defines where it appears in the order (given macropod's apt question).It assumes you have a user form with several checkboxes, a command button and a listbox:

Code:
[blue]Option Explicit

Public CheckArray As New Collection

Private Sub CheckBox1_Click()
    CheckMe ActiveControl
End Sub

Private Sub CheckBox2_Click()
    CheckMe ActiveControl
End Sub

Private Sub CheckBox3_Click()
    CheckMe ActiveControl
End Sub

Private Sub CheckBox4_Click()
    CheckMe ActiveControl
End Sub

Private Sub CommandButton1_Click()
    Dim myControl
    ListBox1.Clear
    For Each myControl In CheckArray
        ListBox1.AddItem myControl.Name
    Next
End Sub

Public Sub CheckMe(chkbox As Control)
    If chkbox = True Then
        CheckArray.Add chkbox, chkbox.Name
    Else
        CheckArray.Remove chkbox.Name
    End If
End Sub[/blue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top