Results 1 to 5 of 5

'Find' not working as expected (Excel vba).

Threaded View

staggers47 'Find' not working as... 01-18-2013, 10:47 AM
JosephP Re: 'Find' not working as... 01-18-2013, 11:17 AM
111StepsAhead Re: 'Find' not working as... 01-18-2013, 11:34 AM
staggers47 Re: 'Find' not working as... 01-18-2013, 11:59 AM
staggers47 Re: 'Find' not working as... 01-18-2013, 12:01 PM
  1. #3
    Forum Contributor
    Join Date
    04-11-2011
    Location
    Columbus, Ohio
    MS-Off Ver
    Excel 2007
    Posts
    325

    Re: 'Find' not working as expected (Excel vba).

    Try this code instead. You call the function from another routine as needed. You will have to change the worksheet "Your worksheet of data" to whatever sheet you want to search.

    Sub main22()
        findItem "blue", "Your worksheet of data"
    End Sub
    
    Function findItem(SearchItem As String, wksName As String)
        Dim x As Long
        Dim y As Long
        Dim last_Row As Long
        Dim last_Column As Long
        Dim wksName As String
        
        'wksName = "Your worksheet of data"
        
        Worksheets(wksName).Activate
        last_Row = Cells.Find("*", Searchorder:=xlByRows, SearchDirection:=xlPrevious).Row
        last_Column = Cells.Find(what:="*", After:=[A1], Searchorder:=xlByColumns, SearchDirection:=xlPrevious).Column
        
        For x = 1 To last_Column
            For y = last_Row To 1 Step -1
                If Cells(y, x).Value = SearchItem Then
                    Cells(y, x).Activate
                    MsgBox "Found item in column " & x & " row " & y
                    Exit Function
                Else
                    'do nothing
                End If
            Next y
        Next x
        MsgBox "Item not found", vbOKOnly
    End Function
    EDIT: Also, if you are searching for the same string of text everytime then of course you will find it in the same place. Hopefully that's not the issue here.
    Last edited by 111StepsAhead; 01-18-2013 at 11:41 AM. Reason: Code change. Now function works with many routines.

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