Results 1 to 5 of 5

Thread: FindAll Function In VBA

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10

    Lightbulb FindAll Function In VBA

    Hi All,

    Here is a Function which return found range. You could use this function to delete,hide,format etc. the range. It's faster than the native Find command in VBA.

    Paste this code in a standard module.

    Code:
    Public Enum xl_LookAt
        xl_Whole = 1
        xl_Part = 2
    End Enum
    Function FINDALL(ByRef RangeToLook As Range, ByVal SearchWhat As String, _
                                Optional ByVal Look_At As xl_LookAt = xl_Whole, _
                                Optional ByVal Match_Case As Boolean = False) As Range
        
        Dim r           As Long
        Dim c           As Long
        Dim UB1         As Long
        Dim UB2         As Long
        Dim strAddress  As String
        Dim k
        
        k = RangeToLook
        
        If IsArray(k) Then
            UB1 = UBound(k, 1)
            UB2 = UBound(k, 2)
            For r = 1 To UB1
                For c = 1 To UB2
                    If Look_At = xl_Whole Then
                        If Match_Case Then
                            If k(r, c) = SearchWhat Then
                                strAddress = strAddress & "," & Cells(r, c).Address(0, 0)
                                If Len(strAddress) > 245 Then
                                    strAddress = Mid$(strAddress, 2)
                                    If FINDALL Is Nothing Then
                                        Set FINDALL = RangeToLook.Range(CStr(strAddress))
                                    Else
                                        Set FINDALL = Union(FINDALL, RangeToLook.Range(CStr(strAddress)))
                                    End If
                                    strAddress = vbNullString
                                End If
                            End If
                        Else
                            SearchWhat = LCase$(SearchWhat)
                            If LCase$(k(r, c)) = SearchWhat Then
                                strAddress = strAddress & "," & Cells(r, c).Address(0, 0)
                                If Len(strAddress) > 245 Then
                                    strAddress = Mid$(strAddress, 2)
                                    If FINDALL Is Nothing Then
                                        Set FINDALL = RangeToLook.Range(CStr(strAddress))
                                    Else
                                        Set FINDALL = Union(FINDALL, RangeToLook.Range(CStr(strAddress)))
                                    End If
                                    strAddress = vbNullString
                                End If
                            End If
                        End If
                    Else
                        If Match_Case Then
                            If InStr(1, k(r, c), SearchWhat, 0) Then
                                strAddress = strAddress & "," & Cells(r, c).Address(0, 0)
                                If Len(strAddress) > 245 Then
                                    strAddress = Mid$(strAddress, 2)
                                    If FINDALL Is Nothing Then
                                        Set FINDALL = RangeToLook.Range(CStr(strAddress))
                                    Else
                                        Set FINDALL = Union(FINDALL, RangeToLook.Range(CStr(strAddress)))
                                    End If
                                    strAddress = vbNullString
                                End If
                            End If
                        Else
                            SearchWhat = LCase$(SearchWhat)
                            If InStr(1, LCase$(k(r, c)), SearchWhat, 0) Then
                                strAddress = strAddress & "," & Cells(r, c).Address(0, 0)
                                If Len(strAddress) > 245 Then
                                    strAddress = Mid$(strAddress, 2)
                                    If FINDALL Is Nothing Then
                                        Set FINDALL = RangeToLook.Range(CStr(strAddress))
                                    Else
                                        Set FINDALL = Union(FINDALL, RangeToLook.Range(CStr(strAddress)))
                                    End If
                                    strAddress = vbNullString
                                End If
                            End If
                        End If
                    End If
                Next
            Next
            If Len(strAddress) > 1 Then
                strAddress = Mid$(strAddress, 2)
                If FINDALL Is Nothing Then
                    Set FINDALL = RangeToLook.Range(CStr(strAddress))
                Else
                    Set FINDALL = Union(FINDALL, RangeToLook.Range(CStr(strAddress)))
                End If
                strAddress = vbNullString
            End If
        Else
            If Look_At = xl_Whole Then
                If Match_Case Then
                    If k = SearchWhat Then
                        FINDALL = RangeToLook
                    End If
                ElseIf LCase$(k) = LCase$(SearchWhat) Then
                    FINDALL = RangeToLook
                End If
            Else
                If Match_Case = True Then
                    If InStr(1, k, SearchWhat, 0) Then
                        FINDALL = RangeToLook
                    End If
                Else
                    If InStr(1, LCase$(k), LCase$(SearchWhat), 0) Then
                        FINDALL = RangeToLook
                    End If
                End If
            End If
        End If
    
    End Function
    and use like..

    Code:
    Sub kTest()
        
        Dim r As Range
        Dim c As Range, t
        
        t = Timer
        Set r = Range("a1:a50000")
        
        Set c = FINDALL(r, "k")
        
        c.Interior.Color = 255
        Debug.Print Timer - t
        
    End Sub
    Enjoy !!
    Last edited by Admin; 08-18-2012 at 02:54 PM.
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

Similar Threads

  1. UDF (user defined function) replacement for Excel's DATEDIF function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 21
    Last Post: 03-07-2015, 09:47 PM
  2. IsDate() Function : VBA
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 06-03-2013, 10:00 PM
  3. CHR() Function VBA
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 1
    Last Post: 05-20-2013, 08:50 AM
  4. CurDir() function VBA
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-17-2013, 12:32 AM
  5. FindAll function
    By Rick Rothstein in forum Rick Rothstein's Corner
    Replies: 1
    Last Post: 06-12-2012, 02:37 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •