Results 1 to 15 of 15

Thread: Checkbox question

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Checkbox question

    I have 20 checkboxes, and 20 objects. I want to load the user form with the checkboxes, and for each checkbox selected, I want to select that object. So CheckBox1 corresponds to Object1 and so forth to 20. Any ideas?

  2. #2
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Checkbox question

    Quote Originally Posted by mike3847
    I have 20 checkboxes, and 20 objects. I want to load the user form with the checkboxes, and for each checkbox selected, I want to select that object. So CheckBox1 corresponds to Object1 and so forth to 20. Any ideas?
    What kind of objects? And what do you exactly mean by "select that object"?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  3. #3
    Addicted Member Piller's Avatar
    Join Date
    Oct 2004
    Location
    california
    Posts
    177

    Re: Checkbox question

    Assuming that you're checkbox's are in a array you can use this.

    VB Code:
    1. Private Sub Check1_Click(Index As Integer)
    2. If Check1(Index).Value = vbChecked Then
    3.     'Code here
    4. End If
    5. End Sub

    Checks to see if the check box is Checked, then runs the code you want it to.
    Are we alive or just breathing?

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Checkbox question

    They're just autoshapes. Like I want to use:

    ActiveSheet.Shapes("Shape1").Select

    but only if CheckBox 1 is checked on the user form.

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Checkbox question

    I tryed the above code, but cannot get it to work. I can't even figure out how to get the value of the checkbox (1, true, 0, or false). Why is this wrong?

    sub test()
    Load StateSelect
    CheckBox1.Value = True
    StateSelect.Show
    end sub()

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checkbox question

    Is this Excel VBA?

  7. #7
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Checkbox question

    Quote Originally Posted by mike3847
    I tryed the above code, but cannot get it to work. I can't even figure out how to get the value of the checkbox (1, true, 0, or false). Why is this wrong?

    sub test()
    Load StateSelect
    CheckBox1.Value = True
    StateSelect.Show
    end sub()
    Values of checkboxes are expressed as 'checked' or 'unchecked' which I think represent 0 or 1...
    Why it isn't a boolean like an option box, I dunno.... I guess some weird microsoft thing....

  8. #8
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Checkbox question

    you would end up like
    VB Code:
    1. sub test()
    2.    Load StateSelect
    3.    CheckBox1.Value = Checked
    4.       'checked instead of true
    5.    StateSelect.Show
    6. end sub()

  9. #9
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checkbox question

    Quote Originally Posted by TheBigB
    Values of checkboxes are expressed as 'checked' or 'unchecked' which I think represent 0 or 1...
    Why it isn't a boolean like an option box, I dunno.... I guess some weird microsoft thing....
    Actually, they are either vbChecked of vbUnChecked which represent 0 and 1. Both of these will work
    VB Code:
    1. Private Sub Command1_Click()
    2. If Check1.Value = 0 Then
    3.    MsgBox "No"
    4. Else
    5.    MsgBox "Yes"
    6. End If
    7. End Sub
    8.  
    9. Private Sub Command1_Click()
    10. If Check1.Value = vbUnChecked Then
    11.    MsgBox "No"
    12. Else
    13.    MsgBox "Yes"
    14. End If
    15. End Sub
    But, I would recommend always using the vb constant as it makes the code much, much easier to understand when reviewing it.

  10. #10

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Checkbox question

    doesn't work. Says "Object Required"

  11. #11
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checkbox question

    Quote Originally Posted by mike3847
    doesn't work. Says "Object Required"
    What doesn't work?

    Are you using VB6 or VBA?

  12. #12

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Checkbox question

    I am using VB6 within Excel 2000.

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Checkbox question

    Quote Originally Posted by mike3847
    I am using VB6 within Excel 2000.
    VB6 is not within anything. It is a completely stand-along development platform that is totally outside of any Office product. That is why my code didn't work. You really aren't using VB6. You are using Excel VBA, and there are many, many features of VB that VBA does not support.

    I'm moving your VBA question to Office Development.

  14. #14

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    9

    Re: Checkbox question

    I was just going off what my Visual Basic says, and that is 6.0. It is within excel. I wasn't aware that it was referred to as VBA.

  15. #15
    Frenzied Member zaza's Avatar
    Join Date
    Apr 2001
    Location
    Borneo Rainforest Habits: Scratching
    Posts
    1,486

    Re: Checkbox question

    Incidentally, in VBA the checkbox does not have a Checked property. It has a Value property, which is boolean.
    I use VB 6, VB.Net 2003 and Office 2010



    Code:
    Excel Graphing | Excel Timer | Excel Tips and Tricks | Add controls in Office | Data tables in Excel | Gaussian random number distribution (VB6/VBA,VB.Net) | Coordinates, Vectors and 3D volumes

Posting Permissions

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



Click Here to Expand Forum to Full Width