Replacing Text in Cell Notes (123574)



The information in this article applies to:

  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 5.0
  • Microsoft Excel for Windows NT 5.0
  • Microsoft Excel for the Macintosh 5.0

This article was previously published under Q123574

SUMMARY

Microsoft Excel does not have a menu command to perform global search and replace operations on cell notes. Choosing Replace from the Formula menu only searches cell contents, not cell notes. The following macro examples demonstrate how to search for and replace information in cell notes.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements. For a similar example using the Excel 4.0 macro language, see 78644.

To create and run this macro, follow these steps:

  1. Enter the following in a module sheet:
     Sub ReplaceCellNotes()
             ' Ask for original string.
             oldstr = InputBox("replace what?")
    
             ' Ask for new string.
             newstr = InputBox("replace with?")
    
             ' If the two numbers are not exactly the same, go into loop.
             If oldstr <> newstr Then
    
                ' For every cell in the selection.
                For Each c In Selection
    
                   ' Find the starting location of the string in the note.
                   location = InStr(1, c.NoteText, oldstr, 1)
    
                   ' If the string has been found, begin the check.
                   If location > 0 Then
    
                      ' Get the section of the note before the found string.
                      begpiece = Left(c.NoteText, location - 1)
    
                      ' Separate out the actual search string.
                      foundpiece = Mid(c.NoteText, location, Len(oldstr))
    
                      ' Get the section of the note after the found string.
                      endpiece = Right(c.NoteText, Len(c.NoteText) _
                         - (location + Len(oldstr) - 1))
    
                      ' Rebuild the note with the first piece, the new
                      ' string, and the last old piece.
                      c.NoteText Text:=begpiece & newstr & endpiece
    
                   End If
    
                ' Next cell.
                Next c
    
             End If
          End Sub
    						
  2. Select the range of cells on your worksheet that contain the cell notes that you wish to replace.
  3. Run the ReplaceCellNotes subroutine.
You will be prompted for the text that you want to find, followed by the text that you want to replace the found text with. The macro make the appropriate changes to your cell notes.

Modification Type:MinorLast Reviewed:8/15/2005
Keywords:kbcode kbhowto kbProgramming KB123574