+ Reply to Thread
Results 1 to 7 of 7

macro in cell

  1. #1
    Registered User
    Join Date
    07-22-2009
    Location
    NYC, NY
    MS-Off Ver
    Excel 2003
    Posts
    22

    macro in cell

    Hi, I was wondering if there was a way to have a Macro associated with a cell. I have 20 cells that have pictures in them, and if you click on the picture, the macro activate. However, there are 20 macros, all of which are the same except for the cell address. If I could make a macro associated with a cell, I could use ActiveCell.FormulaR1C1 to directly edit that cell, and save memory.
    So, is there any way to make a macro associated / part of a cell?

    Okay, so I have posted an example. For each of the 20 cells in the D column (not the 6 cells in row 25) have an UpD## macro, which simply increments them. The macro associated with B1 increments B1 and moves the cursor to D25. I would like there to be only one UpD macro, and it's dependent on what cell you select. If it's possible, I'd also like the UpB1SelectD25 macro not be based in a picture.
    Attached Files Attached Files
    Last edited by nesthead98; 01-26-2010 at 09:39 PM.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro in cell

    Hello nesthead98,

    Pictures and other objects are not in the cell. They are displayed over the cell(s) on the Excel Drawing Layer. If the picture is covering the cell then what is the user editing?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    07-22-2009
    Location
    NYC, NY
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: macro in cell

    Right, I currently have 20 small pictures to the right of the text, so it you can see what has happened.
    But I was wondering if there was a way to make a macro associated with a cell (so, clicking the cell would cause a macro to run).

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro in cell

    Hello nesthead98,

    It can be done. If you post your workbook, it will be easier and faster to answer your question.

  5. #5
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro in cell

    Hello nesthead98,

    The code below has been added to the attached workbook. This will increment the cell count when the picture is clicked.
    Please Login or Register  to view this content.
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    07-22-2009
    Location
    NYC, NY
    MS-Off Ver
    Excel 2003
    Posts
    22

    Re: macro in cell

    Thanks, but I don't seem to understand how it works. Could you explain how I use it?

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: macro in cell

    Hello neasthead98,

    The key to this macro being used by all the "pictures" is identifying which picture was clicked. Excel has a property named Caller that determines how VBA was called from Excel. This property may either be the name of an object or the object itself depending on how Excel invoked the VBA code.

    In this case, the name of the picture, which is actually an AutoShape object, is returned by Application.Caller when the shape is clicked. An object variable is then assigned to the clicked shape.
    Please Login or Register  to view this content.
    This object variable Shp now allows VBA to access the properties and methods of the clicked shape. Objects are referenced in VBA by 2 points: the Upper Left Cell and the Lower Right Cell that the object occupies. Since placement is seldom done precisely, the code takes the average of the current placement to determine the which row the shape is on. the variable R is the row and Cells(R, .TopLeftCell.Column) is the address of the cell next to the "picture". One is then added to contents of this cell.
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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