+ Reply to Thread
Results 1 to 5 of 5

Can I assign a macro to an autoshape using VBA?

  1. #1
    Forum Contributor
    Join Date
    09-16-2004
    Posts
    100

    Can I assign a macro to an autoshape using VBA?

    Basically, I've got some code which dynamically generates macros based on the amount of autoshapes on my worksheet - what I now need to do is get the code to assign these macros to the autoshapes - I've tried recording a macro and then assigning the macros manually, but all this comes up with is

    ActiveSheet.Shapes("shape").select
    Selection.OnAction = "whateversub"

    The autoshapes all have the same name, but a different number (I'm using a variable to go through them 1 by 1, so I've amended it to 'ActiveSheet.Shapes("shape" & a).select')

    This doesn't seem to do the trick, though.

    Any ideas?

  2. #2
    Don Guillett
    Guest

    Re: Can I assign a macro to an autoshape using VBA?

    Here is one I use to name the shapes from a list of shapes in col A and the
    name in col B
    Should be easy to modify

    Sub NameShapes()
    Sheets("checks").Select
    For Each c In [setup!a4:a15]
    ActiveSheet.Shapes(c).TextFrame. _
    Characters.Text = c.Offset(0, 1)
    Next c
    End Sub



    --
    Don Guillett
    SalesAid Software
    [email protected]
    "madbloke" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Basically, I've got some code which dynamically generates macros based
    > on the amount of autoshapes on my worksheet - what I now need to do is
    > get the code to assign these macros to the autoshapes - I've tried
    > recording a macro and then assigning the macros manually, but all this
    > comes up with is
    >
    > ActiveSheet.Shapes("shape").select
    > Selection.OnAction = "whateversub"
    >
    > The autoshapes all have the same name, but a different number (I'm
    > using a variable to go through them 1 by 1, so I've amended it to
    > 'ActiveSheet.Shapes("shape" & a).select')
    >
    > This doesn't seem to do the trick, though.
    >
    > Any ideas?
    >
    >
    > --
    > madbloke
    > ------------------------------------------------------------------------
    > madbloke's Profile:
    > http://www.excelforum.com/member.php...o&userid=14422
    > View this thread: http://www.excelforum.com/showthread...hreadid=527616
    >




  3. #3
    Forum Contributor
    Join Date
    09-16-2004
    Posts
    100
    Cheers, Don, but I'm not trying to name the shapes - I'm trying to assign a macro to them via VBA - i.e. I want to assign macro 1 to autoshape 1, macro 2 to autoshape 2 etc.

    Quote Originally Posted by Don Guillett
    Here is one I use to name the shapes from a list of shapes in col A and the
    name in col B
    Should be easy to modify

    Sub NameShapes()
    Sheets("checks").Select
    For Each c In [setup!a4:a15]
    ActiveSheet.Shapes(c).TextFrame. _
    Characters.Text = c.Offset(0, 1)
    Next c
    End Sub



    --
    Don Guillett
    SalesAid Software
    [email protected]
    "madbloke" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Basically, I've got some code which dynamically generates macros based
    > on the amount of autoshapes on my worksheet - what I now need to do is
    > get the code to assign these macros to the autoshapes - I've tried
    > recording a macro and then assigning the macros manually, but all this
    > comes up with is
    >
    > ActiveSheet.Shapes("shape").select
    > Selection.OnAction = "whateversub"
    >
    > The autoshapes all have the same name, but a different number (I'm
    > using a variable to go through them 1 by 1, so I've amended it to
    > 'ActiveSheet.Shapes("shape" & a).select')
    >
    > This doesn't seem to do the trick, though.
    >
    > Any ideas?
    >
    >
    > --
    > madbloke
    > ------------------------------------------------------------------------
    > madbloke's Profile:
    > http://www.excelforum.com/member.php...o&userid=14422
    > View this thread: http://www.excelforum.com/showthread...hreadid=527616
    >

  4. #4
    Don Guillett
    Guest

    Re: Can I assign a macro to an autoshape using VBA?


    >> Should be easy to modify

    untested but should work. Try it where the name of the macro is in col D.
    ActiveSheet.Shapes(c).OnAction = c.offset(,3)' "yourmacroname"


    --
    Don Guillett
    SalesAid Software
    [email protected]
    "madbloke" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Cheers, Don, but I'm not trying to name the shapes - I'm trying to
    > assign a macro to them via VBA - i.e. I want to assign macro 1 to
    > autoshape 1, macro 2 to autoshape 2 etc.
    >
    > Don Guillett Wrote:
    >> Here is one I use to name the shapes from a list of shapes in col A and
    >> the
    >> name in col B
    >> Should be easy to modify
    >>
    >> Sub NameShapes()
    >> Sheets("checks").Select
    >> For Each c In [setup!a4:a15]
    >> ActiveSheet.Shapes(c).TextFrame. _
    >> Characters.Text = c.Offset(0, 1)
    >> Next c
    >> End Sub
    >>
    >>
    >>
    >> --
    >> Don Guillett
    >> SalesAid Software
    >> [email protected]
    >> "madbloke" <[email protected]>
    >> wrote in
    >> message news:[email protected]...
    >> >
    >> > Basically, I've got some code which dynamically generates macros

    >> based
    >> > on the amount of autoshapes on my worksheet - what I now need to do

    >> is
    >> > get the code to assign these macros to the autoshapes - I've tried
    >> > recording a macro and then assigning the macros manually, but all

    >> this
    >> > comes up with is
    >> >
    >> > ActiveSheet.Shapes("shape").select
    >> > Selection.OnAction = "whateversub"
    >> >
    >> > The autoshapes all have the same name, but a different number (I'm
    >> > using a variable to go through them 1 by 1, so I've amended it to
    >> > 'ActiveSheet.Shapes("shape" & a).select')
    >> >
    >> > This doesn't seem to do the trick, though.
    >> >
    >> > Any ideas?
    >> >
    >> >
    >> > --
    >> > madbloke
    >> >

    >> ------------------------------------------------------------------------
    >> > madbloke's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=14422
    >> > View this thread:

    >> http://www.excelforum.com/showthread...hreadid=527616
    >> >

    >
    >
    > --
    > madbloke
    > ------------------------------------------------------------------------
    > madbloke's Profile:
    > http://www.excelforum.com/member.php...o&userid=14422
    > View this thread: http://www.excelforum.com/showthread...hreadid=527616
    >




  5. #5
    Forum Contributor
    Join Date
    09-16-2004
    Posts
    100
    Thanks! Little bit of tweaking and that worked perfectly - I'd tried similar and failed, so I was wondering whether the OnAction command was right for the job. Clearly it is! Thanks again!

    Quote Originally Posted by Don Guillett

    >> Should be easy to modify

    untested but should work. Try it where the name of the macro is in col D.
    ActiveSheet.Shapes(c).OnAction = c.offset(,3)' "yourmacroname"


    --
    Don Guillett
    SalesAid Software
    [email protected]
    "madbloke" <[email protected]> wrote in
    message news:[email protected]...
    >
    > Cheers, Don, but I'm not trying to name the shapes - I'm trying to
    > assign a macro to them via VBA - i.e. I want to assign macro 1 to
    > autoshape 1, macro 2 to autoshape 2 etc.
    >
    > Don Guillett Wrote:
    >> Here is one I use to name the shapes from a list of shapes in col A and
    >> the
    >> name in col B
    >> Should be easy to modify
    >>
    >> Sub NameShapes()
    >> Sheets("checks").Select
    >> For Each c In [setup!a4:a15]
    >> ActiveSheet.Shapes(c).TextFrame. _
    >> Characters.Text = c.Offset(0, 1)
    >> Next c
    >> End Sub
    >>
    >>
    >>
    >> --
    >> Don Guillett
    >> SalesAid Software
    >> [email protected]
    >> "madbloke" <[email protected]>
    >> wrote in
    >> message news:[email protected]...
    >> >
    >> > Basically, I've got some code which dynamically generates macros

    >> based
    >> > on the amount of autoshapes on my worksheet - what I now need to do

    >> is
    >> > get the code to assign these macros to the autoshapes - I've tried
    >> > recording a macro and then assigning the macros manually, but all

    >> this
    >> > comes up with is
    >> >
    >> > ActiveSheet.Shapes("shape").select
    >> > Selection.OnAction = "whateversub"
    >> >
    >> > The autoshapes all have the same name, but a different number (I'm
    >> > using a variable to go through them 1 by 1, so I've amended it to
    >> > 'ActiveSheet.Shapes("shape" & a).select')
    >> >
    >> > This doesn't seem to do the trick, though.
    >> >
    >> > Any ideas?
    >> >
    >> >
    >> > --
    >> > madbloke
    >> >

    >> ------------------------------------------------------------------------
    >> > madbloke's Profile:
    >> > http://www.excelforum.com/member.php...o&userid=14422
    >> > View this thread:

    >> http://www.excelforum.com/showthread...hreadid=527616
    >> >

    >
    >
    > --
    > madbloke
    > ------------------------------------------------------------------------
    > madbloke's Profile:
    > http://www.excelforum.com/member.php...o&userid=14422
    > View this thread: http://www.excelforum.com/showthread...hreadid=527616
    >

+ 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