+ Reply to Thread
Results 1 to 7 of 7

Excel 2007 : Assign Macro to Cell Range?

  1. #1
    Registered User
    Join Date
    10-18-2010
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    73

    Assign Macro to Cell Range?

    I have the following macro assigned to a key stroke. I would like to assign it to a double click for a cell range. Is that possible?

    For example, if I double click A2 it will run this macro across Row 2. If I double click A500, it will run this macro across row 500, etc...

    Does anyone know how to do this?

    Thanks,
    Steve

    Please Login or Register  to view this content.
    Last edited by stevetothink; 09-15-2011 at 10:13 AM.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Assign Macro to Cell Range?

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code. Posting code without them makes your code hard to read and difficult to be copied for testing. Highlight your code and click the # at the top of your post window. For more information about these and other tags, found here
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Assign Macro to Cell Range?

    Hi stevetothink,

    I have a very good and quick answer but you need to do this before I can give it to you.

    Edit your original post by clicking on the green EDIT button below it. Then click on "Go Advanced" below the message area. Then select all the text in the message that is code. With the text selected click on the "#" Icon above the advanced message area. This will put code tags around your text and
    Please Login or Register  to view this content.
    Save you changes.

    We will see you have complied with the formum rules and then we can answer your question.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Registered User
    Join Date
    10-18-2010
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    73

    Re: Assign Macro to Cell Range?

    Quote Originally Posted by MarvinP View Post
    Hi stevetothink,

    I have a very good and quick answer but you need to do this before I can give it to you.

    Edit your original post by clicking on the green EDIT button below it. Then click on "Go Advanced" below the message area. Then select all the text in the message that is code. With the text selected click on the "#" Icon above the advanced message area. This will put code tags around your text and
    Please Login or Register  to view this content.
    Save you changes.

    We will see you have complied with the formum rules and then we can answer your question.
    Thanks guys. Sorry about the violation. I'm still learning the rules around here. I'm looking forward to seeing your solutions to my problem.

    Steve

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Assign Macro to Cell Range?

    Hey Steve - good job on finding Code Tags.

    Here is the answer for your question.
    Code behind an individual worksheet is called Event Code. This code can be triggered to run, depending on the event that is being performed on that sheet. In the IDE/VBA editor you need to double click on the Sheet Name in the VBA Project window. This will then show a blank VBA screen. At the top where it says "(General)" , click this dropdown and select "WorkSheet". Now the dropdown to the right lists all the possibe events that can be triggered on this sheet. Drop down this list and select "BeforeDoubleClick". This will now, in the VBA code, give you a Private Sub that will be triggered by a double click anywhere on the worksheet.

    You want to only do your code if you double click on a cell between A2 and A500. To do this you need this code:
    Please Login or Register  to view this content.
    Target is the cell you clicked on and it is checked against A2 to A500. If the Target is in this range, you do the code. If it isn't in this range it will do nothing.

    Read http://www.cpearson.com/excel/Events.aspx for a good introduction to Event code.

    I hope this helps.

  6. #6
    Registered User
    Join Date
    10-18-2010
    Location
    NJ
    MS-Off Ver
    Excel 2007
    Posts
    73

    Re: Assign Macro to Cell Range?

    Quote Originally Posted by MarvinP View Post
    Hey Steve - good job on finding Code Tags.

    Here is the answer for your question.
    Code behind an individual worksheet is called Event Code. This code can be triggered to run, depending on the event that is being performed on that sheet. In the IDE/VBA editor you need to double click on the Sheet Name in the VBA Project window. This will then show a blank VBA screen. At the top where it says "(General)" , click this dropdown and select "WorkSheet". Now the dropdown to the right lists all the possibe events that can be triggered on this sheet. Drop down this list and select "BeforeDoubleClick". This will now, in the VBA code, give you a Private Sub that will be triggered by a double click anywhere on the worksheet.

    You want to only do your code if you double click on a cell between A2 and A500. To do this you need this code:
    Please Login or Register  to view this content.
    Target is the cell you clicked on and it is checked against A2 to A500. If the Target is in this range, you do the code. If it isn't in this range it will do nothing.

    Read http://www.cpearson.com/excel/Events.aspx for a good introduction to Event code.

    I hope this helps.
    That's awesome. Thank you very much for your help. I really appreciate it.

    Steve

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,169

    Re: Assign Macro to Cell Range?

    Hi Steve

    You shouldn't Quote the previous post as it just makes our forum harder to read. If you would....
    Edit your post and remove the Quoted text. It will save my old eyes from trying to read the same thing twice. I also fall into that groundhog day thing and think I've already read that someplace before if you quote the post above. (Rule #12 I find)

    Thanks and glad you're learning Event Programming. I remember being scolded for using ByRef instead of ByVal (or vice versa) in my code by one of the real gurus. I just found a good explaination of what that does at:
    http://www.tushar-mehta.com/publish_...%20ByRef.shtml

+ 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