+ Reply to Thread
Results 1 to 6 of 6

Assign Macro to active cell

  1. #1
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    378

    Assign Macro to active cell

    Hello,

    I am trying to figure out how to assign a macro to the active cell.

    I want to trigger a macro (say MyMacro) when I click on the active / selected cell (not "OnSelect" but "OnClick").

    The "trigger" needs to be assigned to the cell programmatically through a macro, so I am visualizing something like:


    Sub CellTriggering
    'this macro performs various operations within the active cell.

    'the last procedure is the assigning of a macro to the active cell:

    'Run MyMacro when click on active cell


    End Sub

    Is this possible?

    Thanks

  2. #2
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham
    MS-Off Ver
    365
    Posts
    8,483

    Re: Assign Macro to active cell

    You could use the double click event
    Please Login or Register  to view this content.

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

    Re: Assign Macro to active cell

    Hi plans,

    On Click isn't one of the normal events so I believe you need to put that event in a Class Module. See information for older versions of Excel at:
    https://docs.microsoft.com/en-us/off...-event-handler

    I use double click events to trigger my code on a cell. I don't know how to do it on a simple click in that cell.
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Contributor
    Join Date
    03-02-2014
    Location
    usa
    MS-Off Ver
    Excel 2016
    Posts
    378

    Re: Assign Macro to active cell

    I understand and I could use the "double click event".
    The issue I have is that the event has to be assigned to a cell programmatically.

    For example, I am running the "Sub CellTriggering" while the active cell is X100 and at the end of the macro, I need to assign the event programmatically to cell X100.

    Next time I will be running the macro while cell Y500 is active, at that point, I need to assign the event programmatically to cell Y500.

    Could you please advise?
    Thanks

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

    Re: Assign Macro to active cell

    Hey plans,

    If there were an event for "Click in Cell" then I'd use a Global Variable of Last cell click in and update it with each click of my mouse. Then if I clicked in a cell a second time, this would show that the last click was the same as this click and run my macro. Something like:

    If not intersect(Target, LastClickCell) is nothing Then

    Good luck!!

    But there is no event of "click" so you might need to do what I suggested above and figure out how to do a Class Module Event.

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

    Re: Assign Macro to active cell

    I keep learning things in Excel I never knew. The event of Selection_Change does capture what cell is clicked in. I think you can use this for a "Click Event" . Try this code behind sheet 1.
    Please Login or Register  to view this content.
    Clicking around Sheet1 will show which cell was clicked on.
    I guess I was confused with this and the event behind the WorkBook called:
    Workbook_SheetChange()

    Still learning after all these years.
    Thanks for the question and the rep.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Assign value or point for each active hour..
    By 1053857 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 02-07-2019, 08:09 PM
  2. [SOLVED] Simple macro/VBA to copy cell above active cell and then move active cell one right...
    By planetjed in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-15-2018, 02:02 PM
  3. Assign Macro to Active X Button
    By Excel Guy 123 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 09-12-2014, 08:17 AM
  4. [SOLVED] Macro to look up and down from the active cell, copying the active "section" of the sheet
    By mikkola in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-12-2013, 07:21 PM
  5. Highlight Active Row macro, and Bold the Active Cell
    By StudentTeacher in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-08-2013, 07:43 PM
  6. Assign active cell in macro with Dynamic Cell choice
    By scaffdog845 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-29-2009, 03:58 PM

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