+ Reply to Thread
Results 1 to 16 of 16

Assign numbers to shapes excel 2007

Hybrid View

  1. #1
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Assign numbers to shapes excel 2007

    Good morning all,
    I have been searching the net for a way of assigning numbers to 10 shapes that I will be using as buttons. I would like them to behave like 10 option buttons (form controls buttons), when the first shape is clicked I would get the number 1 in a specific cell (link cell). When the second button is clicked, I would get number 2 and so on. The selected shape would also need to change color until the next button is selected.

    many thanks

  2. #2
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Assign numbers to shapes excel 2007

    This code returns the number of the shape in "A1" and turns the selected shape "Red" and the others "Green"
    Paste the first code in each of the codes for the ten shapes as below
    Sub Rectangle10_Click()
    Call shps(Application.Caller)
    End Sub
    Place this code in a seperate Basic "Module"
    Sub shps(Nam)
    Dim Shp As Shape
    Range("A1") = Split(Nam, " ")(1)
    For Each Shp In ActiveSheet.Shapes
        If Shp.Name = Nam Then
            Shp.Fill.ForeColor.SchemeColor = 10
        Else
            Shp.Fill.ForeColor.SchemeColor = 3
        End If
    Next Shp
    End Sub
    Regards Mick

  3. #3
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    Hello and thanks you for your help.
    I think I managed to put it into action but I must have not followed the right procedure as I get the word "rectangle" in cell a1. I also have other shapes in the sheet and they all turn the colour of the buttons. Any suggestion?

  4. #4
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Assign numbers to shapes excel 2007

    You could try this line below to get the number.
    The code seems to think theres an extra space there. I assumed the shape names where like "Rectangels 2" etc.
    Nb:- If there is an extra space you could just changing the (1) at the end to (2).

    Other shapes:-
    Are the other shapes rectangles, or other shapes types, as you need to differentiate between the ones you wish to colour and the rest.
    Let me know and I'll alter the code !!
    Range("A1") = Split(Trim(Nam), " ")(1)

  5. #5
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    Changing the number worked although I needed the cell to count from one. I get the shape label number, I thinkCapture2.PNG

    In Total I have 30 identical shapes, 2 groups of 10 and the rest are of different sizes. Ideally I would like to have 2 groups work indipendently so as to produce numbers from 1 to 10 in one cell and 1 to 10 in another cell

  6. #6
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Assign numbers to shapes excel 2007

    What you need to do is form a "Group" of each set of shapes.
    First to give each shape a unique number alter the basic code for each of the rectangles in each group as below.
    Notice the "1" after "Application caller" , this is the first number of the set and in my code will appear in "A1"
    Sub Rectangle1_Click()
    Call shps(Application.Caller, 1)
    End Sub
    Alter the code as above, Change the numbers as required.
    When you have done that alter the lines in the "Module " code a shown in Red.

    Sub shps(Nam, n)
    Dim Shp As Shape
    Range("A1") = n
    For Each Shp In ActiveSheet.Shapes("Group 15").GroupItems   
     If Shp.Name = Nam Then
    To form a Group of shapes, hold down the "Ctrl" key and select each Shape in the Group.
    On the last shape after selecting, Right Click and select "Grouping", "Group" .
    The shapes shoud be part of a Group. Look to the formula Box for its name.
    In the Module code change the Group 15" to the new shape name.
    Try the code out.
    Be careful when forming a Group, I found that Shapes close to the group but not wanted in the group somehow got seleted. so keep those temporarily away from the group.
    Regards Mick
    Last edited by MickG; 07-03-2012 at 06:04 AM.

  7. #7
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    I also tried leaving no space between name and number:
    Case "Group13"
    Select Case nam
    Case "Rectangle1": [A1] = 1
    Case "Rectangle2": [A1] = 2
    Case "Rectangle3": [A1] = 3
    Case "Rectangle4": [A1] = 4

  8. #8
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    If ActiveSheet.Shapes(nam).OLEFormat.Object.Name = oShape.Name Then

    I get nam=error 2023 (hovering on (nam)

  9. #9
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Assign numbers to shapes excel 2007

    Have a look at this Example:-
    Attached Files Attached Files

  10. #10
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    Not working. Bet the excel file format.... Running your .xls in compatibility mode on Excel 2007, no luck. Converted in all possible ways, no luck. Still trying....
    Last edited by mammagamma; 07-04-2012 at 08:02 AM.

  11. #11
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    Attachment 165943Attachment 165942

    Thats what I get if I run the macro. Phoning Bill......

  12. #12
    Registered User
    Join Date
    07-02-2012
    Location
    london
    MS-Off Ver
    Excel 2007
    Posts
    23

    Re: Assign numbers to shapes excel 2007

    Using form control buttons works beautifully. Thank you. Still trying to figure out why Excel 2007 throws tantrums

  13. #13
    Forum Expert MickG's Avatar
    Join Date
    11-23-2007
    Location
    Banbury,Oxfordshire
    Posts
    2,650

    Re: Assign numbers to shapes excel 2007

    Sorry you could not open the file , but seems as if your Ok anyway.
    Thanks for the feedback
    Regards
    Mick

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1