Add Email Hyperlink with VBA

TFCJamieFay

Active Member
Joined
Oct 3, 2007
Messages
480
Hi All,

I have a sheet ("Sheet2") with email addresses in column K down to about 300 or so. I want to write a bit of code to add a email hyperlink to each cell. I want the email address as the cell value and I want the subject to be "Report for " & CompanyName

So far I have the following, I'm just missing the important bit!
Code:
Sub AddHyperlinks()

    Dim CompanyName As String
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Lastrow = Range("D" & Rows.Count).End(xlUp).Row
    
    Range("K2:K" & Latrow).Select
    For Each cell In Selection
        cell.Activate
        CompanyName = cell.Offset(0, -7).Value
        
        '...Insert code to add hyperlink
        
    Next cell
               
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub

Many thanks,

Jay
 
Last edited:

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Hi TFCJamieFay,

You could try something like this

Code:

Set OutApp = CreateObject("Outlook.Application")
On Error Resume Next
OutApp.Session.Logon
Set OutMail = OutApp.CreateItem(0)

On Error Resume Next
With OutMail
.to = Activecell.text
.CC = ""
.BCC = ""
.Subject = "Report for " & CompanyName .body = body
.display
End With
On Error GoTo 0

ColinKJ
 
Upvote 0
Thanks for your reply ColinKJ, but I have been playing around with the macro recorder and have gone with the following. It seems to work OK. Maybe I should have tried that first. Thank you for your help though!

Here's my code to help anyone else out there (if it's not full of holes that is but it worked for me!)
Code:
Sub AddHyperlinks()

    Dim CompanyName As String
    
    With Application
        .ScreenUpdating = False
        .DisplayAlerts = False
    End With

    Lastrow = Range("D" & Rows.Count).End(xlUp).Row
    
    Range("K2:K" & Lastrow).Select
    For Each cell In Selection
        cell.Select
        
        If cell.Value <> "" Then
            CompanyName = cell.Offset(0, -7).Value
            
            ActiveSheet.Hyperlinks.Add Anchor:=ActiveCell, Address:= _
                "mailto:" & cell.Value & "?subject=Report%20for%20" & CompanyName, _
                TextToDisplay:=cell.Value
        End If
        
    Next cell
               
    With Application
        .ScreenUpdating = True
        .DisplayAlerts = True
    End With

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,381
Members
448,888
Latest member
Arle8907

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