+ Reply to Thread
Results 1 to 3 of 3

Macro to convert string to hyperlink

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Macro to convert string to hyperlink

    Hi everyone,

    I have the following code:

    Sub Apply_Hyperlinks1()
    Dim r As Range
    Dim myval As String
       For Each r In Range("K1:K3000")
          If InStr(r, "http://") > 0 Or InStr(r, "www.") > 0 Then
             myval = r.Value
                Range(r.Address).Select
                   ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:=myval, TextToDisplay:=myval
          End If
       Next r
       Range("K1").Select
    End Sub
    This code is supposed to go through column K and convert any test strings within each cell starting with http:// or www. from a string to hyperlink. It is possible that other text will be in the cell which should not be converted to a hyperlink; on text that starts with http:// or www.

    When I run the code I get run-time error 13 and the if InStr line is indicated. Any ideas?

    Thanks!

  2. #2
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Macro to convert string to hyperlink

    Is this code (from my stash) something you can use?
    Sub ConvertTxt2Hyperlink()
    Dim cCell As Range
    
    For Each cCell In selection.Cells
    
       If cCell.Hyperlinks.Count = 0 Then
          On Error Resume Next
          ActiveSheet.Hyperlinks.Add _
             Anchor:=cCell, _
             Address:=cCell.Value, _
             TextToDisplay:=cCell.Value
       End If
    Next cCell
    End Sub
    • Select a range of cells
    • ALT+F8...Select: ConvertTxt2Hyperlink...Click: Run
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  3. #3
    Forum Contributor
    Join Date
    07-29-2013
    Location
    Oshawa
    MS-Off Ver
    Excel 2010
    Posts
    660

    Re: Macro to convert string to hyperlink

    hi Ron,

    Thanks for the reply and your code. Unfortunately your code converts the entire text in the cell to a hyperlink. What I'm trying to do is work out a macro that will convert only text in the cell starting with http:// or www. into a hyperlink and leave the other text as text. Any ideas?

    Thanks,

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. MACRO help - convert cell text to hyperlink
    By excelmonster in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-18-2012, 05:19 AM
  2. [SOLVED] convert hyperlink to string or html
    By scrabtree23 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2012, 08:24 AM
  3. Need to create a Macro which convert the values in a column to Hyperlink.
    By excel_001 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 02-18-2012, 01:38 PM
  4. Macro to convert the text in to string.
    By mithil in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-02-2010, 11:47 AM
  5. macro to convert those filepaths into a ready-to-click hyperlink
    By jhestler in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 04-09-2008, 12:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1