+ Reply to Thread
Results 1 to 4 of 4

Search for a value

Hybrid View

  1. #1
    Registered User
    Join Date
    10-20-2010
    Location
    Belfast
    MS-Off Ver
    Excel 2003
    Posts
    1

    Search for a value

    Hi,
    I have a workbook with multiple sheets along the bottom. Within each sheet is a list of policy numbers.
    Can anyone supply a simple code which will provide a message box titled 'Search' and when I input a policy number and press enter it will search all sheets and if it locates the correct policy number, it goes directly to that particular cell.

    Your help would be appreciated!

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search for a value

    This should do it for you:
    Option Explicit
    
    Sub SearchALLSheets()
    'Author:    Jerry Beaucaire, ExcelForum.com
    'Date:      10/17/2010
    'Summary:   A string is searched for in all sheets, the value searched must be
    '           the whole cell value, partial strings are not matched
    
    Dim MyVal As String
    Dim ws As Worksheet
    Dim vFind As Range
    
    MyVal = Application.InputBox("Enter Policy Number to locate:", "Policy Locator", Type:=2)
    If MyVal = "False" Then Exit Sub
    On Error Resume Next
    
    For Each ws In Worksheets
        Set vFind = ws.Cells.Find(MyVal, LookIn:=xlValues, LookAt:=xlWhole, MatchCase:=False)
        If Not vFind Is Nothing Then
            ws.Activate
            vFind.Select
            Exit Sub
        End If
    Next ws
    
    MsgBox "Policy " & MyVal & " was not found."
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Search for a value

    There's a more robust version of this concept published on my site.

    '3D Find
    I have a macro that may be "ready to use" for searching all sheets in an Excel file for any string and jumping to that cell automatically if it is found.
    3D FIND

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Search for a value

    Why have data split like that?

    Option Explicit
    
    Sub SearchAll()
    Dim wSht As Worksheet
    Dim rCl As Range
    Dim sFind As String
    
    sFind = InputBox("Enter policy number")
    If sFind <> "" Then
    For Each wSht In ThisWorkbook.Worksheets
    Set rCl = wSht.UsedRange.Find(sFind, LookIn:=xlValues, LookAt:=xlWhole)
    If Not rCl Is Nothing Then
    Application.GoTo rCl, True
    Exit For
    End If
    Next wSht
    Else: MsgBox "You must enter a policy number", vbCritical, "Input required"
    Exit Sub
    End If
    End Sub
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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