Vba to create hyperlinks to web addresses

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,063
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

Down column J I have data, and some of that data are websites
I'd like a macro that can go down the column and if an address is a website create a hyperlink to that website.
if it easyier then it can do it to all cells in column J that are not blank or contain the words "Sales and marketing" or "Home and Away" (full celll values) as these are the only other data a cell might contain than a web address.

Please help if you can

Thanks

Tony
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Siddharth Rout

Active Member
Joined
Jan 7, 2022
Messages
359
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
When you manually type something in Excel and if it contains say WWW. or HTTP:// or HTTPS:// then Excel automatically converts the text into a link.

Taking advantage of that, we can use this code

VBA Code:
Option Explicit

Sub Sample()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim lRow As Long
    Dim rng As Range, aCell As Range
    
    Set wb = ThisWorkbook
    '~~> Change this to the relevant worksheet
    Set ws = Sheet1
    
    With ws
        '~~> Find last row in column J
        lRow = .Range("J" & .Rows.Count).End(xlUp).Row
        
        Set rng = .Range("J1:J" & lRow)
    
        For Each aCell In rng
            '~~> Check for the existence of WWW. or HTTP:// or HTTPS://
            Select Case True
                Case UCase(Left(aCell.Value, 4)) = "WWW.", _
                     UCase(Left(aCell.Value, 7)) = "HTTP://", _
                     UCase(Left(aCell.Value, 8)) = "HTTPS://"
                    
                    '~~> Add hyperlink
                    .Hyperlinks.Add Anchor:=aCell, _
                    Address:=aCell.Value2, _
                    TextToDisplay:=aCell.Value2
            End Select
        Next aCell
    End With
End Sub

Before And After

1641564561080.png
 
Solution

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,063
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Thanks Siddharth Rout,
This works pefectly :)
Cheers
Tony
 

Forum statistics

Threads
1,176,042
Messages
5,901,092
Members
434,870
Latest member
michud08

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
Top