+ Reply to Thread
Results 1 to 10 of 10

How to use the Excel Find command in VBA

  1. #1
    Registered User
    Join Date
    10-26-2010
    Location
    Clonakilty, Co Cork, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    19

    Angry How to use the Excel Find command in VBA

    I want to be able to incorporate the Excel FIND command into a VBA subroutine so as to be able to find (but not replace) all instances of a user-specified text value in whatever area of a workbook that I select. The values in all cells of the workbook are text - words or phrases; what I am searching for are complete cell values. I then want to display all the matching cells and their addresses.

    The obvious way to get started is to record a macro of running the Excel command. But I have tried to do this several times in different ways and always failed . When I go to edit the completely-recorded macro, I find that it has the expected headers and footers but no code!

    Can anyone explain why this is and how it can be avoided/corrected? If it cannot, what is the basic VBA code for searching a range and returning all the matching values?

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to use the Excel Find command in VBA

    hershmab,

    One way...
    Please Login or Register  to view this content.
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Valued Forum Contributor
    Join Date
    02-12-2011
    Location
    The Netherlands
    MS-Off Ver
    365
    Posts
    818

    Re: How to use the Excel Find command in VBA

    Or:

    You can change de code with a inputbox.
    Please Login or Register  to view this content.
    Attached Files Attached Files
    Kind regards, Harry.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to use the Excel Find command in VBA

    Hello hershmab and tigeravatar,

    The Message Dialog Box has a maximum character limit of 1024 characters. This may not be adequate to display all the results found in the workbook.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  5. #5
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to use the Excel Find command in VBA

    Agreed, which is why my code shows each result 1 messagebox at a time. User didn't specify where/how to display the data, so I put that in there so hershmab could adjust it as needed.

  6. #6
    Registered User
    Join Date
    10-26-2010
    Location
    Clonakilty, Co Cork, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: How to use the Excel Find command in VBA

    Many thanks to both TigerAvatar and HSV for useful parts to a solution and to Leith Ross for his very practical warning.

    Using the Excel FIND command would quite possibly be a lot faster for large searches - my workbook has over 250,000 cells! It would avoid some of the looping.

    So it would still be helpful if I knew why recording it in a macro was fruitless and if there is any way round that.

  7. #7
    Registered User
    Join Date
    10-26-2010
    Location
    Clonakilty, Co Cork, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: How to use the Excel Find command in VBA

    This is my first venture into deep VBA programming so I need to ask for a bit more help with the Find method.

    Searching VBA Help in Excel 2010, the only article I could find was headed "Find Method (VBA Add-In Object Model)". From the documentation this is very different from the Find method that both TigerAvatar and HSV employ:
    1. It returns TRUE if the search value is found, not the found value(s) themselves
    2. It has five arguments specifying the range to search and three optional arguments specifying how to search

    Clearly this method is not what I need. But how and where can I find the specification of the Find method employed by your code?

  8. #8
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to use the Excel Find command in VBA


  9. #9
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: How to use the Excel Find command in VBA

    Hello hershmab,

    This may help you understand it better. This is from the VBA Help files. If you have any questions are reading this, I will be happy to answer them.


    Finds specific information in a range, and returns a Range object that represents the first cell where that information is found. Returns Nothing if no match is found. Doesn’t affect the selection or the active cell.

    For information about using the Find worksheet function in Visual Basic, see Using Worksheet Functions in Visual Basic.

    expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
    expression Required. An expression that returns a Range object.

    What Required Variant. The data to search for. Can be a string or any Microsoft Excel data type.

    After Optional Variant. The cell after which you want the search to begin. This corresponds to the position of the active cell when a search is done from the user interface. Note that After must be a single cell in the range. Remember that the search begins after this cell; the specified cell isn’t searched until the method wraps back around to this cell. If you don’t specify this argument, the search starts after the cell in the upper-left corner of the range.

    LookIn Optional Variant. The type of information.

    LookAt Optional Variant. Can be one of the following XlLookAt constants: xlWhole or xlPart.

    SearchOrder Optional Variant. Can be one of the following XlSearchOrder constants: xlByRows or xlByColumns.

    SearchDirection Optional XlSearchDirection. The search direction.

    XlSearchDirection can be one of these XlSearchDirection constants.
    xlNext default
    xlPrevious

    MatchCase Optional Variant. True to make the search case sensitive. The default value is False.

    MatchByte Optional Variant. Used only if you’ve selected or installed double-byte language support. True to have double-byte characters match only double-byte characters. False to have double-byte characters match their single-byte equivalents.

    SearchFormat Optional Variant. The search format.

    Remarks
    The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each time you use this method. If you don’t specify values for these arguments the next time you call the method, the saved values are used. Setting these arguments changes the settings in the Find dialog box, and changing the settings in the Find dialog box changes the saved values that are used if you omit the arguments. To avoid problems, set these arguments explicitly each time you use this method.

    You can use the FindNext and FindPrevious methods to repeat the search.

    When the search reaches the end of the specified search range, it wraps around to the beginning of the range. To stop a search when this wraparound occurs, save the address of the first found cell, and then test each successive found-cell address against this saved address.

    To find cells that match more complicated patterns, use a For Each...Next statement with the Like operator. For example, the following code searches for all cells in the range A1:C5 that use a font whose name starts with the letters Cour. When Microsoft Excel finds a match, it changes the font to Times New Roman.
    Please Login or Register  to view this content.
    Example
    This example finds all cells in the range A1:A500 on worksheet one that contain the value 2 and changes it to 5.
    Please Login or Register  to view this content.
    ]

  10. #10
    Registered User
    Join Date
    10-26-2010
    Location
    Clonakilty, Co Cork, Ireland
    MS-Off Ver
    Excel 2010
    Posts
    19

    Re: How to use the Excel Find command in VBA

    Just what I needed, tigeravatar, thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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