Convert multiple hyperlinks to buttons

L

Legacy 32120

Guest
Hello folks!

Can hyperlinks be converted to buttons please?
I've a large number of links so a global conversion is what I'm hoping for.

Thanks muchly....

Max :)

P.S. I'm running Excel 2010
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I don't have 2010 but the sample(s) below work for 2007.

Hope it helps.

Gary

Change the range in the code line shown below to point to your hyperlinks.

Set oLinks = Range("A1:A10")

The buttons will be placed in the column immediately to the right of the links.

Rich (BB code):
Public Sub Make_Buttons()
 
Dim oActive As Worksheet
Dim oLinks As Range
Dim oCell As Range
Dim oOffset As Range
Dim oLink As Hyperlink
Dim oButton As Shape
 
Set oActive = ActiveSheet
Set oLinks = Range("A1:A10")
 
For Each oCell In oLinks
    If oCell.Hyperlinks.Count <> 0 Then
    
        Set oOffset = oCell.Offset(0, 1)
        Set oButton = oActive.Shapes.AddShape(msoShapeRectangle, oOffset.Left + 2, oOffset.Top + 2, oOffset.Width - 4, oOffset.Height - 4)
            
        With oButton
            .Fill.ForeColor.RGB = RGB(255, 255, 255)
            .Line.ForeColor.RGB = RGB(0, 0, 0)
            .TextFrame.Characters.Font.ColorIndex = 0
            .TextFrame.Characters.Font.Size = 8
            .TextFrame.Characters.Text = oCell.Hyperlinks(1).TextToDisplay
            .Line.Style = msoLineThinThin
            'Adjust additional properties for desired appearance
        End With
        
        oActive.Hyperlinks.Add Anchor:=oButton, Address:=oCell.Hyperlinks(1).Address
        
        'Or:
        
        'Set oLink = oActive.Hyperlinks.Add(Anchor:=oButton, Address:=oCell.Hyperlinks(1).Address)
        'With oLink
           
        'End With
        
    End If
Next
 
End Sub

Caution: This will delete all rectangles on the sheet. Not just the buttons created by the above.

Rich (BB code):
Public Sub Delete_Buttons()
 
Dim oActive As Worksheet
Dim oShape As Shape
 
Set oActive = ActiveSheet
 
'Warning, this will delete ALL rectangles on the sheet
For Each oShape In oActive.Shapes
    If oShape.AutoShapeType = msoShapeRectangle Then
        oShape.Delete
    End If
Next oShape
 
End Sub
 
Upvote 0
Hi Gary!

Terrific! Many thanks...

Can this be also done with the =HYPERLINK() type of link?

Max Kramer :)
 
Upvote 0
Max,

The short answer is yes.

The long answer is I've never used =HYPERLINK() and don't often mix VBA and worksheet functions. According to help, the display text and URL can be references to other cells. It seems that it could get complicated.

I'm not sure what the best way to go about it is and will probably be stepping into a mine field. I'm hoping someone else will bail us out. If not, I'll give it a shot this evening (USA EST).

Gary
 
Upvote 0
Hi Max,

As Gary pointed out, the sheet function HYPERLINK would seem problematic if points to another cell that contains the address. So if your links are built like:

=HYPERLINK(Sheet3!D10,"OOPS")

Then I do not see a reasonably easy way of getting this done. If the links are like:

=HYPERLINK("http://www.cpearson.com/excel/","Chip's")

Then using Gary's code, maybe like:
Rich (BB code):
Public Sub Make_Buttons()
    
Dim oActive As Worksheet
Dim oLinks As Range
Dim oCell As Range
Dim oOffset As Range
Dim oLink As Hyperlink
Dim oButton As Shape
Dim strFormula As String
Dim strLink As String
Dim strFriendly As String
Dim REX As Object ' RegExp
    
Set oActive = ActiveSheet
Set oLinks = Range("A1:A10")
    
'=HYPERLINK("http://www.cpearson.com/excel/","Chip's")
Const PATTERN_LEFT As String = "=HYPERLINK\("""
Const PATTERN_RIGHT As String = ","""
    
Set REX = CreateObject("VBScript.RegExp")
With REX
    .Global = False
    .IgnoreCase = True
End With
    
For Each oCell In oLinks
    
    If oCell.HasFormula Then
        If oCell.Formula Like "=HYPERLINK(*" Then
            
            strFormula = oCell.Formula
            REX.Pattern = PATTERN_LEFT
            strLink = REX.Replace(strFormula, "")
            strFriendly = Mid(strLink, InStr(1, strLink, PATTERN_RIGHT) + 2, 255)
            strFriendly = Left(strFriendly, Len(strFriendly) - 2)
            strLink = Mid(strLink, 1, InStr(1, strLink, PATTERN_RIGHT) - 2)
            
            Set oOffset = oCell.Offset(0, 1)
            Set oButton = oActive.Shapes.AddShape(msoShapeRectangle, oOffset.Left + 2, oOffset.Top + 2, oOffset.Width - 4, oOffset.Height - 4)
            
            With oButton
                .Fill.ForeColor.RGB = RGB(255, 255, 255)
                .Line.ForeColor.RGB = RGB(0, 0, 0)
                '// not sure why problems here, I'm in 2000 and intellisense was showing?
                '// un-comment the next two lines if they were working w/o error
                '.TextFrame.Characters.Font.ColorIndex = 0
                '.TextFrame.Characters.Font.Size = 8
                .TextFrame.Characters.Text = strFriendly 'oCell.Hyperlinks(1).TextToDisplay
                .Line.Style = msoLineThinThin
                'Adjust additional properties for desired appearance
            End With
            
            oActive.Hyperlinks.Add Anchor:=oButton, Address:=strLink ', TextToDisplay:=strFriendly
        End If
    End If
Next
End Sub

A bit sloppy (Sorry Gary), but see if it seems to grab the addresses reliably (in a junk copy of your wb!)

Mark
 
Upvote 0
GTO,

Thanks for the feedback. Maybe my short answer should be no.

I was hoping there was some kind of mechanism similar to "paste special" that would resolve the formulas down to the actual value (URL).

There doesn't seem to be any advantage to doing it as follows ("Hard coded" values for lack of a better term):

=HYPERLINK("http://www.google.com","Google")

IMO it be easier to just right click on the cell and assign the link directly rather than use the formula.

It must be primarily intended to be used with refs to other cells. That could get out of control very quickly. Especially if there is no consistency to the formula(s).

In "A2"
=HYPERLINK(B2,C2)

In "B2"
=D2

In "D2"
http://www.google.com

One may have to drill down through numerous references to get to the URL or Text. A "one size fits all" macro doesn't seem very likely.

Gary
 
Upvote 0
GTO,

Thanks for the feedback. Maybe my short answer should be no.

...

It must be primarily intended to be used with refs to other cells. That could get out of control very quickly. Especially if there is no consistency to the formula(s).

In "A2"
=HYPERLINK(B2,C2)

In "B2"
=D2

In "D2"
http://www.google.com

One may have to drill down through numerous references to get to the URL or Text. A "one size fits all" macro doesn't seem very likely.

Gary

Hi Gary,

Well, I did admit that it didn't seem reasonably easy unless the target address and friendly were in string form.

That said, of course it bugged me... Started with drilling via .Precedents. I think .Precedents could work, but about the time I was getting that figured out, Evaluate suddenly seemed like the ticket.

See what you think :)

@makka:

Please test in a copy of your wb just in case, but I believe this may work.

Before, including "drill-downs" if present:
Excel Workbook
ABCDE
1Old LinkButtonLink PrecedentFriendly PrecedentLink Precedent
2TestTest
3http://vbaexpress.com/forum/forumdisplay.php?f=17http://vbaexpress.com/forum/forumdisplay.php?f=17
4
5Debra'shttp://contextures.com/
6
7My Doc
8Test1D:\110609M\011010\Blacklist.xlsTest1
9
10Test 2D:\110609M\011010\Blacklist.xls
11
12Chip Pearson'shttp://cpearson.comChip Pearson'shttp://cpearson.com
Sheet2


In a Standard Module:
<font face=Courier New><SPAN style="color:#00007F">Option</SPAN> <SPAN style="color:#00007F">Explicit</SPAN><br>    <br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Make_The_Damn_Buttons_No_Matter_What()<br>    <br><SPAN style="color:#00007F">Dim</SPAN> oActive             <SPAN style="color:#00007F">As</SPAN> Worksheet<br><SPAN style="color:#00007F">Dim</SPAN> oLinks              <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> oCell               <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> oOffset             <SPAN style="color:#00007F">As</SPAN> Range<br><SPAN style="color:#00007F">Dim</SPAN> oLink               <SPAN style="color:#00007F">As</SPAN> Hyperlink<br><SPAN style="color:#00007F">Dim</SPAN> oButton             <SPAN style="color:#00007F">As</SPAN> Shape<br>    <br><SPAN style="color:#00007F">Dim</SPAN> REX                 <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Object</SPAN> <SPAN style="color:#007F00">'<--- RegExp</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strLink             <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strFriendly         <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> strRaw              <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br><SPAN style="color:#00007F">Dim</SPAN> bolHasFriendlyName  <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Boolean</SPAN><br>        <br>    <SPAN style="color:#00007F">Set</SPAN> oActive = ActiveSheet<br>    <SPAN style="color:#00007F">Set</SPAN> oLinks = Range("A1:A12")<br>        <br>    <SPAN style="color:#00007F">Set</SPAN> REX = CreateObject("VBScript.RegExp")<br>    <SPAN style="color:#00007F">With</SPAN> REX<br>        .Global = <SPAN style="color:#00007F">True</SPAN><br>        .IgnoreCase = <SPAN style="color:#00007F">True</SPAN><br>        .Pattern = "=HYPERLINK\(|\)"<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        <br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> oCell <SPAN style="color:#00007F">In</SPAN> oLinks<br>        <br>        <SPAN style="color:#00007F">If</SPAN> oCell.HasFormula <SPAN style="color:#00007F">Then</SPAN><br>            <SPAN style="color:#00007F">If</SPAN> oCell.Formula <SPAN style="color:#00007F">Like</SPAN> "=HYPERLINK(*" <SPAN style="color:#00007F">Then</SPAN><br>                <br>                <SPAN style="color:#007F00">'// Rip everything between the parenthesis                              //</SPAN><br>                strRaw = REX.Replace(oCell.Formula, vbNullString)<br>                <br>                <SPAN style="color:#007F00">'// See if there's a comma in there, which I think we should be safe in //</SPAN><br>                <SPAN style="color:#007F00">'// presuming this means a friendlyname exists.                         //</SPAN><br>                bolHasFriendlyName = <SPAN style="color:#00007F">Not</SPAN> InStr(1, strRaw, ",") = 0<br>                <br>                <SPAN style="color:#00007F">If</SPAN> bolHasFriendlyName <SPAN style="color:#00007F">Then</SPAN><br>                    <SPAN style="color:#007F00">'// Use Evaluate to get the string(s), regardless of by reference   //</SPAN><br>                    <SPAN style="color:#007F00">'// to another cell or by string in the HYPERLINK function.         //</SPAN><br>                    strLink = Evaluate("=" & Split(strRaw, ",", 2)(0))<br>                    strFriendly = Evaluate("=" & Split(strRaw, ",", 2)(1))<br>                <SPAN style="color:#00007F">Else</SPAN><br>                    strLink = Evaluate("=" & strRaw)<br>                    strFriendly = "My Button" <SPAN style="color:#007F00">' or strLeft</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>                <br>                <SPAN style="color:#00007F">Set</SPAN> oOffset = oCell.Offset(0, 1)<br>                <SPAN style="color:#00007F">Set</SPAN> oButton = oActive.Shapes.AddShape(msoShapeRectangle, _<br>                                                      oOffset.Left + 2, _<br>                                                      oOffset.Top + 2, _<br>                                                      oOffset.Width - 4, _<br>                                                      oOffset.Height - 4)<br>                <SPAN style="color:#00007F">With</SPAN> oButton<br>                    .Fill.ForeColor.RGB = RGB(255, 255, 255)<br>                    .Line.ForeColor.RGB = RGB(0, 0, 0)<br>                    <br>                    .TextFrame.Characters.Text = strFriendly<br>                    <SPAN style="color:#007F00">'// I moved this  to after there are characters //</SPAN><br>                    .TextFrame.Characters.Font.ColorIndex = 0<br>                    .TextFrame.Characters.Font.Size = 8<br>                    <br>                    .Line.Style = msoLineThinThin<br>                    <SPAN style="color:#007F00">'Adjust additional properties for desired appearance</SPAN><br>                <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>                <br>                oActive.Hyperlinks.Add Anchor:=oButton, Address:=strLink<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

Hope that helps,

Mark
 
Upvote 0
Thanks Gary & Mark,

It converts 'function' links to 'embedded' links with buttons just fine!

Let me guess, can't have buttons with 'function' links because 'function' only works in cells?

Max :)
 
Upvote 0
Thanks Gary & Mark,

It converts 'function' links to 'embedded' links with buttons just fine!

Let me guess, can't have buttons with 'function' links because 'function' only works in cells?

Max :)

Max -

I'm very glad that worked. It was extremely educational for me!

I am afraid I do not understand your question :(

Very nice.

Thanks for bailing me out Mark. Now I can take the night off :biggrin:

Gary

Hey Gary,

Thank you very much. Took longer than it should have (last day of vacation(!), argh!!!!

That said, was most educational :) and interesting!

Mark
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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