Results 1 to 12 of 12

Thread: so simple, yet so difficult (checkbox selection)

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    so simple, yet so difficult (checkbox selection)

    Hey guys,

    Im pretty decent with vba. I'm having a problem.

    Im trying to run a FOR loop and go through my checkboxes which are named
    "graph1" ,"graph2","graph3"...

    basically I need to be able to call the checkbox from the worksheet, they arent in a form.

    I've been trying activesheet.shapes("graph1").selection and thats not working
    Ive also tried sheet1.checkboxes and sheet1.checkbox. Its really stumped me.

    I know I need to be able to take "graph" & i and put that into the checkbox call. What should I do?

  2. #2
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: so simple, yet so difficult (checkbox selection)

    G'Day gotskill10, Welcome to the Forum

    Here is a simple NON ROBUST way - get you going at least (for Excel).
    VB Code:
    1. Dim objShp As Shape
    2.  
    3.     For Each objShp In ActiveSheet.Shapes
    4.         If InStr(objShp.Name, "graph") <> 0 Then
    5.             'CheckBox
    6.             MsgBox objShp.Name
    7.         End If
    8.     Next
    Or
    VB Code:
    1. Dim objShp As Shape
    2.  
    3.     For Each objShp In ActiveSheet.Shapes
    4.         If Left$(LCase(objShp.Name), 5) = "graph" Then
    5.             'CheckBox
    6.             MsgBox objShp.Name
    7.         End If
    8.     Next


    I will post back a better solution soon

    Last edited by Bruce Fox; May 30th, 2006 at 08:33 PM.

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    Re: so simple, yet so difficult (checkbox selection)

    thanks for the help. I tried out your solution. I was able to get it to cycle through those checkboxes, however, how can i reference the checkboxes value (either true or false)?

    i tried sheet1.objshp
    or sheet1.checkbox(objshp.name)

    How else can I do this?

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: so simple, yet so difficult (checkbox selection)

    use objShp.Value

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    Re: so simple, yet so difficult (checkbox selection)

    i tried that as well, it doesnt work, there is no value to the shapes library

  6. #6
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: so simple, yet so difficult (checkbox selection)

    Here you go... (note: I changed the Variable Name to better reflect Hungarian Notation - now shpObj):

    VB Code:
    1. Dim shpObj As Shape
    2.  
    3.     For Each shpObj In ActiveSheet.Shapes
    4.         If InStr(shpObj.Name, "graph") <> 0 Then
    5.             'CheckBox
    6.             MsgBox shpObj.Name
    7.             [b]MsgBox OLEObjects(shpObj.Name).Object.Value[/b]
    8.         End If
    9.     Next
    Or
    VB Code:
    1. Dim shpObj As Shape
    2.  
    3.     For Each shpObj In ActiveSheet.Shapes
    4.         If Left$(LCase(shpObj.Name), 5) = "graph" Then
    5.             'CheckBox
    6.             MsgBox shpObj.Name
    7.             [b]MsgBox OLEObjects(shpObj.Name).Object.Value[/b]
    8.         End If
    9.     Next

    And using the OLEObjects Collectio (opposed to Shapes):
    VB Code:
    1. Dim oleObj As OLEObject
    2.  
    3.     For Each oleObj In OLEObjects
    4.         If Left$(LCase(oleObj.Name), 5) = "graph" Then
    5.             'CheckBox
    6.             MsgBox oleObj.Name
    7.             MsgBox OLEObjects(oleObj.Name).Object.Value
    8.         End If
    9.     Next
    Last edited by Bruce Fox; May 30th, 2006 at 08:46 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    Re: so simple, yet so difficult (checkbox selection)

    Quote Originally Posted by Bruce Fox
    Here you go... (note: I changed the Variable Name to better reflect Hungarian Notation - now shpObj):

    VB Code:
    1. Dim shpObj As Shape
    2.  
    3.     For Each shpObj In ActiveSheet.Shapes
    4.         If InStr(shpObj.Name, "graph") <> 0 Then
    5.             'CheckBox
    6.             MsgBox shpObj.Name
    7.             [b]MsgBox OLEObjects(shpObj.Name).Object.Value[/b]
    8.         End If
    9.     Next
    Or
    VB Code:
    1. Dim shpObj As Shape
    2.  
    3.     For Each shpObj In ActiveSheet.Shapes
    4.         If Left$(LCase(shpObj.Name), 5) = "graph" Then
    5.             'CheckBox
    6.             MsgBox shpObj.Name
    7.             [b]MsgBox OLEObjects(shpObj.Name).Object.Value[/b]
    8.         End If
    9.     Next

    And using the OLEObjects Collectio (opposed to Shapes):
    VB Code:
    1. Dim oleObj As OLEObject
    2.  
    3.     For Each oleObj In OLEObjects
    4.         If Left$(LCase(oleObj.Name), 5) = "graph" Then
    5.             'CheckBox
    6.             MsgBox oleObj.Name
    7.             MsgBox OLEObjects(oleObj.Name).Object.Value
    8.         End If
    9.     Next
    I tried this, none of them work, for MsgBox OLEObjects(shpObj.Name).Object.Value, there is no object in the oleobjects, and i looked at the oleobjects library, there is no way to get a value.

    With your last code:

    For Each oleObj In OLEObjects

    It gives me an error here, even with the dim oleobj as oleobject.

    Are there any other solutions?

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

    Re: so simple, yet so difficult (checkbox selection)

    What error did it give you?

  9. #9

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    Re: so simple, yet so difficult (checkbox selection)

    for MsgBox OLEObjects(shpObj.Name).Object.Value

    it says "sub or function not defined," but thats because of the .object.value, if you put OLEObjects(shpObj.Name).select, it works, but doesn't let me do selection.value.


    on

    Dim oleObj As OLEObject

    For Each oleObj In OLEObjects
    If Left$(LCase(oleObj.Name), 5) = "graph" Then
    'CheckBox
    MsgBox oleObj.Name
    MsgBox OLEObjects(oleObj.Name).Object.Value
    End If
    Next

    I get an object required error on For Each oleObj In OLEObjects, I have the Dim oleObj As OLEObject in there.

  10. #10
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: so simple, yet so difficult (checkbox selection)

    What version of Excel are you running? I successfully tested on Office 2003.

  11. #11

    Thread Starter
    New Member
    Join Date
    May 2006
    Posts
    6

    Re: so simple, yet so difficult (checkbox selection)

    im using excel 2003, sp2 which exact code did you try?

  12. #12
    INXSIVE Bruce Fox's Avatar
    Join Date
    Sep 2001
    Location
    Melbourne, Australia
    Posts
    7,429

    Re: so simple, yet so difficult (checkbox selection)

    All.....

    I re test.

    EDIT: Works....
    Do you get an intellisense of OLEObjects when you Dim x As [[[OLEO.....]]]
    Last edited by Bruce Fox; May 31st, 2006 at 09:19 PM.

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