How can I assign a macro to a cell with text in it, using the right mouse click or any other method?

TedX

Board Regular
Joined
Apr 18, 2021
Messages
122
Office Version
  1. 365
Platform
  1. Windows
Hi, how can I have a word written in a cell, assigned to a macro, I believe one way to do it is via using the before right mouse click action on your worksheet? Can anyone show me the way? For the purpose of this request, please consider the word 'Next' will be written in cell "U5" and the macro name is 'top3wa' Thanks in advance for any assistance.
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter "Next" in U5 and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "U5" Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "Next" Then
        Call top3wa
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Copy and paste this macro into the worksheet code module. Do the following: right click the tab name for your sheet and click 'View Code'. Paste the macro into the empty code window that opens up. Close the code window to return to your sheet. Enter "Next" in U5 and press the ENTER key.
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "U5" Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "Next" Then
        Call top3wa
    End If
    Application.ScreenUpdating = True
End Sub

Firstly, I love your name mumps 😁 Okay, I did everything you said and it worked perfectly. Not to be a pain but, this is a one-time action, I typed in the word Next again and hit enter again and it worked a second time. What would be really cool is if I could just click on the U5 cell now and every time I clicked on it, it would run the macro. So essentially be like assigning a macro to a button except not a button this time but a cell with text in it. I hope that's easy to understand, it was painful to write :rolleyes:
 
Upvote 0
I would suggest a double click on U5 instead of a simple click because if the macro runs on a click, this would prevent you from entering the cell without running the macro. See if this works for you using a double click. Of course, U5 must contain "Next" when you double click it.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "U5" Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "Next" Then
        Call top3wa
    End If
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
I would suggest a double click on U5 instead of a simple click because if the macro runs on a click, this would prevent you from entering the cell without running the macro. See if this works for you using a double click. Of course, U5 must contain "Next" when you double click it.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "U5" Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "Next" Then
        Call top3wa
    End If
    Application.ScreenUpdating = True
End Sub

Absolute genius, fair dinkum legend, that is exactly what I have been after for ages, wahoooooooooooooo can't thank you enough. I marked your post as the solution and am a very happy chappy even at 3.52 AM. Thanks mumps 🙏 🙏 🙏
 
Upvote 0
I would suggest a double click on U5 instead of a simple click because if the macro runs on a click, this would prevent you from entering the cell without running the macro. See if this works for you using a double click. Of course, U5 must contain "Next" when you double click it.
VBA Code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
    If Target.CountLarge > 1 Then Exit Sub
    If Target.Address(0, 0) <> "U5" Then Exit Sub
    Application.ScreenUpdating = False
    If Target = "Next" Then
        Call top3wa
    End If
    Application.ScreenUpdating = True
End Sub
This was genious! I have been playing with this but unsuccessfully able to use this code to enable multiple cells with texts on the same page to run a macro.

I have a doc, that has columns pulling content from another sheet. Each column has a title, and I want to be able to click on each title to sort by the macro I have created for each option. Below shows 2, but htere are 6 columns that I would like to have the text be connected to a macro.

Until I found this page, I had to use individual images/icons.

Thank you much in advance! :)

eg-
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.CountLarge > 1 Then Exit Sub
If Target.Address(0, 0) <> "C10,E10" Then Exit Sub
Application.ScreenUpdating = False
If Target = "Store List" Then
Call Sort_Store_List_By_Store_Name
If Target = "Last Visit 1" Then
Call Sort_Store_List_By_LastVisit_1
End If
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Please start your own new thread. If you send me a private message with a link to your thread, I will have a look at it. :)
 
Upvote 0
Please start your own new thread. If you send me a private message with a link to your thread, I will have a look at it. :)
for some reason, I do not have ability to send DM. Maybe it is because I created this account after spending hours playing with your code to figure this questions out. :/

There are no links for me in your bio on the page or when I search the member pages to get to your as well.

HERE is a link to the new query
 
Upvote 0

Forum statistics

Threads
1,225,628
Messages
6,186,103
Members
453,337
Latest member
fiaz ahmad

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top