A blog about software development, primarily in Java and about web applications.

Tuesday, January 12, 2010

Change a text cell into a Hyperlink in Excel

I recently imported a CSV file I was given into Excel. The file had two columns (with many rows of data) that contained URLs in the format http://foo.com/bar?phuid=1023, etc. I wanted to make these links clickable. Excel provides a straight forward, but manual way to do this. I asked around for an automated way to do this and was quickly given this macro:
Sub URL_List()
For Each cell In Selection
If cell.Value <> "" Then
If Left(cell.Value, 7) = "http://" Then
URL = cell.Value
Else
URL = "http://" + cell.Value
End If
ActiveSheet.Hyperlinks.Add Anchor:=cell, _
Address:=URL, TextToDisplay:=cell.Value
End If
Next cell
End Sub

I don't know the source of this macro it proved useful today. I hope you can make some use of it.

The directions on creating a macro in Excel can be found in Excel's help which I'll repeat here:

Create a macro using Microsoft Visual Basic



  1. On the Tools menu in Microsoft Excel, point to Macro, and then click Visual Basic Editor.

  2. On the Insert menu, click Module.

  3. Type or copy your code into the code window of the module.

  4. If you want to run the macro (macro: An action or a set of actions that you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) from the module window, press F5.

  5. When you're finished writing your macro, click Close and Return to Microsoft Excel on the File menu.



No comments: