<?xml version="1.0" encoding="utf-8" standalone="no"?>
<!DOCTYPE document PUBLIC "-//CNX//DTD CNXML 0.5//EN" "http://cnx.rice.edu/technology/cnxml/schema/dtd/0.5/cnxml_plain.dtd">
<document xmlns="http://cnx.rice.edu/cnxml" xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="new">
  <name xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">Hyperlink Search and Replace in Microsoft Excel</name>
  <metadata xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
  <md:version xmlns:bib="http://bibtexml.sf.net/">1.2</md:version>
  <md:created xmlns:bib="http://bibtexml.sf.net/">2008/07/17 10:20:39 GMT-5</md:created>
  <md:revised xmlns:bib="http://bibtexml.sf.net/">2008/08/04 13:29:49.265 GMT-5</md:revised>
  <md:authorlist xmlns:bib="http://bibtexml.sf.net/">
      <md:author xmlns:bib="http://bibtexml.sf.net/" id="advplc">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Trent</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Kelly</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">advplc@yahoo.com</md:email>
    </md:author>
  </md:authorlist>

  <md:maintainerlist xmlns:bib="http://bibtexml.sf.net/">
    <md:maintainer xmlns:bib="http://bibtexml.sf.net/" id="advplc">
      <md:firstname xmlns:bib="http://bibtexml.sf.net/">Trent</md:firstname>
      
      <md:surname xmlns:bib="http://bibtexml.sf.net/">Kelly</md:surname>
      <md:email xmlns:bib="http://bibtexml.sf.net/">advplc@yahoo.com</md:email>
    </md:maintainer>
  </md:maintainerlist>
  
  <md:keywordlist xmlns:bib="http://bibtexml.sf.net/">
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">Excel</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">hyperlink</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">macro</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">Microsoft</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">URL</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">VBA</md:keyword>
    <md:keyword xmlns:bib="http://bibtexml.sf.net/">visual basic for applications</md:keyword>
  </md:keywordlist>

  <md:abstract xmlns:bib="http://bibtexml.sf.net/">Many MS Excel users put hyperlinks in spreadsheet cells.  If there is need to update a large number of hyperlink addresses in a spreadsheet due to reorganization or renaming of a server, it can be very time consuming to edit and update all the hyperlinks.  This example code will locate and replace a designated group of characters in all the anchor text hyperlink addresses within a spreadsheet.  If the URL has been typed directly into the cell, it can be found and replaced with the normal Excel search and replace function (Ctrl-h).  If anchor text has been typed into the cell and then turned into a hyperlink, this code can search and replace the hidden URL's that normal search and replace will not find.</md:abstract>
</metadata>
  <content xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/">
    <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="aa">
    <code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">Public Sub ReplaceHyperlinkURL(FindString As String, ReplaceString As String)
Dim LinkURL, PreStr, PostStr, NewURL As String
Dim FindPos, ReplaceLen, URLLen As Integer
Dim MyDoc As Worksheet
Dim MyCell As Range
On Error GoTo ErrHandler

Set MyDoc = ActiveSheet
For Each MyCell In MyDoc.UsedRange
If MyCell.Hyperlinks.Count &gt; 0 Then
 LinkURL = MyCell(1).Hyperlinks(1).Address
 FindPos = InStr(1, LinkURL, FindString)
 If FindPos &gt; 0 Then 'If FindString is found
  ReplaceLen = Len(FindString)
  URLLen = Len(LinkURL)
  PreStr = Mid(LinkURL, 1, FindPos - 1)
  PostStr = Mid(LinkURL, FindPos + ReplaceLen, URLLen)
  NewURL = PreStr &amp; ReplaceString &amp; PostStr
  MyCell(1).Hyperlinks(1).Address = NewURL 'Change the URL
  End If
 End If
Next MyCell
Exit Sub
ErrHandler:
MsgBox ("ReplaceHyperlinkURL error")
End Sub
</code>
</para>
    <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="ab">The ReplaceHyperlinkURL code must be placed in a VBA code module.  From a spreadsheet, Press Alt+F11 to open the VBA Editor.  Then select Insert - Module from the menu.  Copy the code and paste it into the module.  Then save the module.
</para>   
    <para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="ac">In order to run the procedure, create a macro that contains following line and run the macro in Excel.  Be sure to replace the FindText with the portion of the address you want to find and ReplaceText with the text you want to replace it with. 
</para><code xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" type="block">Call ReplaceHyperlinkURL("FindText", "ReplaceText")
 </code><para xmlns:md="http://cnx.rice.edu/mdml/0.4" xmlns:bib="http://bibtexml.sf.net/" id="element-503">Please be sure to make a backup copy of your spreadsheet before running the macro just in case an error is made in the FindText or ReplaceText. </para>  
  </content>
  
</document>
