Run an UDF by click on a cell

m_hakimi_a

New Member
Joined
Oct 16, 2024
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi, I'm beginner. I wrote an user defined function in excel and it works good.
I want to run this UDF by click on cell(1,1). How can I do that?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
A UDF is a Function that takes arguments and returns a value, and is typically written so that is used in a cell, and that value is returned to a formula. Running code by a click is an entirely different thing. You cannot invoke a Function with arguments by using a click. We need more information about what you want to do, and it would also help to have the code for your function (paste into your post using CODE tags).
 
Upvote 0
A UDF is a Function that takes arguments and returns a value, and is typically written so that is used in a cell, and that value is returned to a formula. Running code by a click is an entirely different thing. You cannot invoke a Function with arguments by using a click. We need more information about what you want to do, and it would also help to have the code for your function (paste into your post using CODE tags).
Thank you for reply
My UDF has no argument, I want to run it by click on a cell A1.
 
Upvote 0
Thank you for reply
My UDF has no argument, I want to run it by click on a cell A1.
Sounds like you have a Subroutine and not a UDF (User Defined Function). UDF's are called just like any other built-in function. By typing something like; =SUM(A1,B1) in a cell. A1 and B1 are the arguments.

Subroutine:

VBA Code:
Sub mySub()
MsgBox "Hello", vbOKOnly, "Subroutine"
End Sub

UDF

VBA Code:
Function myFunc(arg1,arg2)
myFunc = arg1 + arg2
End Function
 
Upvote 0
Anyways, if you want to run a Subroutine by clicking in cell then you have to call your Subroutine from the Sheet code module.


VBA Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = Cells(1, 1).Address Then Call [YourSubroutineNameHere]  'no brackets around your Subroutine name
End Sub
 
Upvote 0

Forum statistics

Threads
1,222,526
Messages
6,166,549
Members
452,052
Latest member
Harris4Naomi

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