"Reference isn't valid" error when using hyperlink to run function

jwbrouse01

New Member
Joined
Jun 2, 2022
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Greetings!

I am trying to use hyperlinks to run a macro which opens a userform. I previously found and adapted the below code to create the hyperlinks

VBA Code:
Sub CreateHyperlinks()

With ActiveSheet
    LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
End With

Range(Cells(2, 6), Cells(LastRow, 6)).Formula = "=HYPERLINK(""#MyUDF()"", ""Sign Up..."")"
  
End Sub


Function MyUDF()
      Set MyUDF = Selection 'This is required for the link to work properly
      MsgBox "The clicked cell address is " & Selection.row
End Function


When I try to click on the hyperlinks, I get an error message "Reference isn't valid" and the rest of the function does not run. I do apologize if this has already been solved elsewhere, but all my searches have brought up that it is critical to add the "Set MyUDF = Selection", otherwise you will get the aforementioned error.

Can someone please assist me? Thanks!
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
The UDF call should not be in quotes or it is treated as a literal piece of text. It should be:

Code:
"=HYPERLINK(MyUDF(), ""Sign Up..."")"
 
Upvote 0
Thank you for the reply.

If I make the recommended change, my hyperlinks display #NAME? and do not function.

I had also previously read that it is important to have the "#" in front of MyUDF(), but if I do that without the quotes, my CreateHyperlinks() sub errors out for a Run-time error '1004': Application-defined or object-defined error
 
Upvote 0
It works here. A Name error usually means you either have the code in the wrong place (it must be in a normal module and in the same workbook or an add in) or macros are disabled
 
Upvote 0
Thank you. That was my problem. I had the function defined in a worksheet.

For my next problem...now, the function runs randomly. Click on the worksheet and leave it sit, nothing happens....but if I start moving my mouse around WITHOUT clicking on anything, it will randomly pop up the Msgbox defined in the MyUDF function.

Perhaps there is an better way to do all of this?

🤷‍♂️
 
Upvote 0
Update: it isn't random, it's whenever my mouse passes over one of the cells containing the hyperlink. I only want it to function when I click on the link.
 
Upvote 0
That's not how this method works. The whole point of it is that it works by mousing over the cell. If that's not what you want, you shoiuld use a different approach, such as actual hyperlinks and a worksheet_followhyperlink event, or just a button.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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