Find replace a name with a hyperlink

MelanieB
3StarLounger
Posts: 310
Joined: 19 Apr 2010, 16:18
Location: middle of the state of Washington

Find replace a name with a hyperlink

Post by MelanieB »

I need to search an excel spreadsheet and replace all instances of a person's name with that person's email address and have it show as hyperlink, not text. How do I get it to show as a hyperlink? This is just one person and one email address, but her name is all over the place in this large worksheet so I don't want to have to do it manually.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find replace a name with a hyperlink

Post by HansV »

Where does the e-mail address come from? Is it available in a cell, or do you want a macro that prompts for it, or do you want to hard-code the e-mail address in the macro?
Best wishes,
Hans

MelanieB
3StarLounger
Posts: 310
Joined: 19 Apr 2010, 16:18
Location: middle of the state of Washington

Re: Find replace a name with a hyperlink

Post by MelanieB »

Good question. The email address comes from.. hmm .. well we have to put it in. It does't come from anywhere at this point. It's not already in any other cell.

My first thought is to ask for a one-time prompt for the email address. But, it would also work to have it hard coded into the macro.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find replace a name with a hyperlink

Post by HansV »

Try this:

Code: Select all

Sub AddHyperlinks()
  Dim strName As String
  Dim strEmail As String
  Dim wsh As Worksheet
  Dim rng As Range
  Dim strAddress As String
  strName = InputBox("What name do you want to search for?")
  If strName = "" Then
    MsgBox "No name provided", vbExclamation
    Exit Sub
  End If
  strEmail = InputBox("What is the e-mail address?")
  If strEmail = "" Then
    MsgBox "No e-mail address entered", vbExclamation
    Exit Sub
  End If
  Set wsh = ActiveSheet
  Set rng = wsh.Cells.Find(What:=strName, LookIn:=xlValues, _
    LookAt:=xlWhole)
  If Not rng Is Nothing Then
    strAddress = rng.Address
    Do
      wsh.Hyperlinks.Add Anchor:=rng, _
        Address:="mailto:" & strEmail, _
        TextToDisplay:=rng.Value
      Set rng = wsh.Cells.FindNext(After:=rng)
    Loop While rng.Address <> strAddress
  End If
End Sub
Best wishes,
Hans

MelanieB
3StarLounger
Posts: 310
Joined: 19 Apr 2010, 16:18
Location: middle of the state of Washington

Re: Find replace a name with a hyperlink

Post by MelanieB »

That's so slick Hans! Thank you!! It's perfect!

MelanieB
3StarLounger
Posts: 310
Joined: 19 Apr 2010, 16:18
Location: middle of the state of Washington

Re: Find replace a name with a hyperlink

Post by MelanieB »

Actually, that worked when I tested it using Excel 2010. It didn't work when I used it in Excel 2003. And, the user I'm helping has Excel 2003. Sorry for leaving that bit of info out.

I attached a screenshot of the compile error. I don't know what this means or how to fix it.
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78629
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Find replace a name with a hyperlink

Post by HansV »

The code works in all versions of Excel, but if you look closely, you'll see that somehow the copy/paste process has inserted a blank line between every pair of code lines. This is not your fault, it's something that the browser does.
For many lines it doesn't matter, but the lines that end in an underscore (the so-called line continuation) expect that the code continues on the line immediately below. That is why several lines are highlighted in red. If you remove the blank lines, it should be OK again.
Best wishes,
Hans

MelanieB
3StarLounger
Posts: 310
Joined: 19 Apr 2010, 16:18
Location: middle of the state of Washington

Re: Find replace a name with a hyperlink

Post by MelanieB »

Ah ha. That did it. Thank you!