VBA an enhanced function of the Find method

Source: Internet
Author: User

'This function is an enhanced function of the Find method, which, combined with the FindNext and FindPrevious methods, can return a set of eligible cells;'This function returns a collection object, which stores the found cells;'This function has two required parameters:'1.SearchRange is used to store the area to be searched;'2.FindWhat is used to store the value to be searched;'The rest of the parameters are optional and are the same as the find method parameter;'returns a collection object regardless of whether a cell is found;'we use the collection.count=0, to judge, did not find the matching cell;Option ExplicitFunctionFindplus (Searchrange asRange, FindWhat asVariant, _OptionalAfter asRange, _OptionalLookIn asVariant =Xlformulas, _OptionalLookAt asVariant =Xlpart, _OptionalSearchOrder asVariant =Xlbyrows, _OptionalSearchDirection asVariant =Xlnext, _OptionalMatchCase asVariant =False, _                  OptionalMatchByte asVariant =True, _                  OptionalSearchFormat asVariant =False) asCollectionDimFoundcell asRange'stores the range of cells found;DimAftercell asRange'the starting cell where the lookup is stored;DimFoundcol asCollection'stores the collection of found cell ranges;DimFirstaddress as String 'Store the address of the cell that was found for the first timeSetFoundcol =NewCollection'Collecion class instantiation to Object'The following judgment statement completes the control of the after parameter valueIfAfter is  Nothing  ThenElse  SetAftercell = after'if the after parameter is not empty, this is provided with the after parameter valueEnd If'find the first qualifying valueSetFoundcell = Searchrange.find (What:=findwhat, after:=Aftercell, _ LookIn:=LookIn, _ LookAt:=LookAt, _ SearchOrder:=SearchOrder, _ SearchDirection:=SearchDirection, _ MatchCase:=MatchCase, _ MatchByte:=MatchByte, _ SearchFormat:=SearchFormat)If  notFoundcell is  Nothing  Thenfirstaddress= Foundcell.address'if the first value is found, then the cell address is assigned to the firstaddress variable        'The following loops are constantly looking for the desired values in the area and are constantly added to the Foundcol collection     DoFoundcol.add Foundcell'assign the found cell to the Foundcol object            'depending on the searchdirection parameter, determine whether to search up or down      IfSearchDirection = Xlnext Then        SetFoundcell = Searchrange.findnext (after:=Foundcell)Else        SetFoundcell = searchrange.findprevious (after:=Foundcell)End If    Loop Until(Foundcell is  Nothing)Or(foundcell.address = firstaddress)'Classic usage, as long as the found cell and the first found cell address is not the same, it has been circulatingEnd IfSetFindplus = Foundcol'assigning a Collection object to a function nameEnd Function

VBA an enhanced function of the Find method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.