XL: How to Search All Worksheets in a Workbook

Last reviewed: February 3, 1998
Article ID: Q146865
The information in this article applies to:
  • Microsoft Excel for Windows, versions 5.0, 5.0c
  • Microsoft Excel for Windows NT, version 5.0
  • Microsoft Excel for the Macintosh, versions 5.0, 5.0a
  • Microsoft Excel for Windows 95, versions 7.0, 7.0a
  • Microsoft Excel 97 for Windows
  • Microsoft Excel 98 Macintosh Edition

SUMMARY

In Microsoft Excel, you can search on an individual worksheet by clicking Find on the Edit menu. However, to search all of the worksheets in a workbook in a convenient manner requires that you use a Microsoft Visual Basic for Applications procedure. This article includes a Visual Basic macro (Sub procedure) that you can use to accomplish this task. As an alternative to using Visual Basic, you can group the worksheets before you start a search.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied, including, but not limited to, the implied warranties of merchantability and/or fitness for a particular purpose. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and 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 needs. If you have limited programming experience, you may want to contact the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/default.asp

Sample Visual Basic Procedure

   Sub FindData()

       ' This Sub prompts you for a value and searches each worksheet
       ' for the value. If it finds the search value, the routine 
       ' prompts you to continue the search on the sheet. If it does not
       ' find the value on the sheet, it goes to the next sheet. You can
       ' cancel the search on the sheet by answering No to "Look for
       ' another value : ?" and then answering Yes to "Cancel the
       ' search ...".

       testValue = InputBox("Enter the value to search for : ")
       For x = 1 To ActiveWorkbook.Worksheets.Count
           Worksheets(x).Select
           Set foundcell = ActiveSheet.Cells.Find(testValue)
           If foundcell Is Nothing Then
               MsgBox "The word was not found"
           Else
               MsgBox "The word was found in cell " & foundcell.Address
               Range(foundcell.Address).Select

       LookAgain:
               response = MsgBox _
                   ("Look for another value on this sheet?", vbYesNo)

               ' If response = 6, we will not continue searching on
               ' this sheet.
               If response = 6 Then

                   ' Part2
                   Set foundcell = _
                       ActiveSheet.Cells.FindNext(after:=ActiveCell)
                   Range(foundcell.Address).Select
                   GoTo LookAgain
               End If

               If response = 7 Then
                   response = MsgBox("Cancel search ? ", vbYesNo)
                   If response = 6 Then End
                   GoTo NextSheet
               End If
           End If

       NextSheet:
       Next x
       MsgBox "Search is complete ....."

   End Sub

REFERENCES

For more information about Looping, click the Index tab in Microsoft Excel 97 Visual Basic Help, type the following text

   For Each

and then double-click the selected text to go to the "For Each...Next statement" topic.

"Visual Basic User's Guide," page 145-146


Additional query words: 5.00 7.00 8.00 XL98 XL97 XL7 XL5 search value
Keywords : kbcode kbprg PgmHowto
Version : WINDOWS:5.0,7.0,97; MACINTOSH:5.0,98
Platform : MACINTOSH WINDOWS
Issue type : kbhowto


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: February 3, 1998
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.