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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
Even if it doesn't take arguments, it returns a value. Please show the code.
 
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
It depends on what is your UDF doing.
In this sample, If you choose cell A1, cell A2 will call an UDF to sum A3 and A4
PHP:
Option Explicit

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(0, 0) = "A1" Then
    Range("A2").Value = myUDF
End If
End Sub
Function myUDF()
    MsgBox "Hello, this is my UDF"
    myUDF = Range("A3") + Range("A4")
End Function
A2 only be updated when A1 is selected.
If it does not work, try to share your UDF script.
 
Upvote 0
Welcome to the MrExcel Message Board! :)

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at:

There is no need to repeat the link(s) provided above but if you have posted the question at other places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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