Convert macro output (formula) to VBA

Dr. Demento

Well-known Member
Joined
Nov 2, 2010
Messages
618
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I'm stuck in how to replace a term with the appropriate VBA code. The goal is to have a dynamic hyperlink that changes not only anchor but also the Display Text. I realize the code as it stands is circular, but I can figure out how to make it work if I could only get the syntax correct. :confused:

The original looks like this:
Code:
Function Hyperlink_dynamic(gnrAnchor As range, _
                           rtsDisplayText As String) As String
' ~~ Puts a dynamic hyperlink formula in the destination cell. _
     Formula automatically adjusts if its target is moved.
' [URL]http://www.vbaexpress.com/kb/getarticle.php?kb_id=237[/URL]
Dim strPath As String
  
   'This next statement avoids an error if the target workbook hasn't been saved
  If gnrAnchor.Parent.Parent.path <> vbNullString Then _
    strPath = gnrAnchor.Parent.Parent.path & Application.PathSeparator
    
  Hyperlink_dynamic = "=HYPERLINK(""[" & strPath & gnrAnchor.Parent.Parent.name & "]"" & " & _
                      "CELL(""address"",'" & gnrAnchor.Parent.name & "'!" & gnrAnchor.Address & "),""" & _
                      [B][COLOR=#ff0000]rtsDisplayText [/COLOR][/B]& """)"
End Function

However, I want replace rtsDisplayText term with the VBA equivalent of this code:
Code:
   Hyperlink_dynamic = "=HYPERLINK(""[Book1]"" & _
                          CELL(""address"",Sheet2!R11C11), _
                          [B][COLOR=#0000ff]CELL(""address"",Sheet2!R11C11))"[/COLOR][/B]
where Sheet2!R11C11 = gnrAnchor.Parent.name & "'!" & gnrAnchor.Address

Thanks y'all.
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
Any takers?? Is additional info needed? Let me know if it's unclear.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,198
Members
453,022
Latest member
RobertV1609

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