Vba to create hyperlinks to web addresses

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
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

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
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
 
Upvote 0
Solution

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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