+ Reply to Thread
Results 1 to 4 of 4

Using Find to locate data.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-31-2013
    Location
    Philadelphia, Pa
    MS-Off Ver
    Excel 2010
    Posts
    170

    Using Find to locate data.

    Greetings all,

    I wrote the following script to find an entry in a long list based on two values found in cells V12 & W12. Essentially, W12 contains either "Last Name", or "First Name", while V12 contains the actual text that needs to be found. Based on the entry in W12 it'll select a different column to search for a match to the V12 data. When I tested this code, it works fine when searching for "First Name", but constantly fails to find any match when searching for "Last Name". There is no error code, it simply fails to find any match, valid or not.

    Sub Find_Name_Search()
    'Find the first or last name and select the table row
    Dim x, x1 As Range
    Dim ws As Worksheet
    
    On Error GoTo ErrMsg
    
    Set ws = Worksheets("Master List")
    Set x = ws.Range("V12")
    Set x1 = ws.Range("W12")
    
    ws.Select
    If x1 = "First Name" Then
        Columns("C:C").Select
    ElseIf x1 = "Last Name" Then
        Columns("B:B").Select
    End If
    Selection.Find(what:=x, after:=ActiveCell, LookIn:=xlFormulas, _
        lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, _
        MatchCase:=True, searchformat:=False).Activate
    
    If x1 = "First Name" Then
        Range(ActiveCell.Offset(0, -1), ActiveCell.Offset(0, 4)).Select
    ElseIf x1 = "Last Name" Then
        Range(ActiveCell.Offset(0, 5)).Select
    End If
    
    Exit Sub
    
    ErrMsg:
        MsgBox ("Cannot Find Match")
    
    End Sub
    Thanks in advance.

  2. #2
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: Using Find to locate data.

    Probably, this is the problem

    Change
    Range(ActiveCell.Offset(0, 5)).Select
    to
     ActiveCell.Offset(0, 5).Select

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,529

    Re: Using Find to locate data.

    This seems to work as well,

    Sub Find_Name_Search()
    'Find the first or last name and select the table row
        Dim x As Range, x1 As Range
        Dim ws As Worksheet
        Dim Rng As Range, Fnd As Range
        On Error GoTo ErrMsg
    
        Set ws = Worksheets("Master List")
        Set x = ws.Range("V12")
        Set x1 = ws.Range("W12")
    
        ws.Select
        If x1 = "First Name" Then
            Set Rng = ws.Range("C:C")
        ElseIf x1 = "Last Name" Then
            Set Rng = ws.Range("B:B")
    
        End If
        Set Fnd = Rng.Find(what:=x, lookat:=xlWhole)
        If x1 = "First Name" Then
            Range(Fnd.Offset(0, -1), Fnd.Offset(0, 4)).Select
        ElseIf x1 = "Last Name" Then
            Fnd.Offset(0, 5).Select
        End If
    
        Exit Sub
    
    ErrMsg:
        MsgBox ("Cannot Find Match")
    
    
    End Sub

  4. #4
    Forum Contributor
    Join Date
    05-31-2013
    Location
    Philadelphia, Pa
    MS-Off Ver
    Excel 2010
    Posts
    170

    Re: Using Find to locate data.

    Thanks! Such a simple error.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] VBA - Find/Locate Cell Macro
    By clprdctn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-30-2015, 12:01 PM
  2. Locate Value, then find a value 18 columns to right in same row
    By JasonTheLucky in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 10-15-2014, 09:00 PM
  3. Find and Locate Option
    By fadym in forum Excel General
    Replies: 6
    Last Post: 11-10-2012, 05:42 PM
  4. Can't get Cells.Find to locate integers
    By SJB in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-09-2009, 10:39 AM
  5. Using FIND function to locate space between first and last name
    By Dennis_in_nh in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 04-19-2006, 03:20 PM
  6. USING EDIT FIND to locate cell references
    By Ron Wilson in forum Excel General
    Replies: 3
    Last Post: 09-15-2005, 09:05 PM
  7. Find and Locate Macro
    By abeard in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-20-2005, 11:29 AM

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