Results 1 to 2 of 2

Thread: FindAll function

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #2
    Senior Member
    Join Date
    Jun 2012
    Posts
    337
    Rep Power
    14
    Hi Rock,

    Intriguing code.

    I tried to rewrite the function and to do the same thing in a macro.
    I'm not sure whether you prefer all separate address entries as result or entries the way Excel describes ranges/areas, using :

    In the macro I introduced the use of Evaluate (in the [ ] form).

    Code:
    Sub tst1()
      MsgBox findall_snb1("cut", "A1:E5", True, True)
      MsgBox findall_snb1("cut", "A1:E5", True, False)
      MsgBox findall_snb1("cut", "A1:E5", False, True)
      MsgBox findall_snb1("cut", "A1:E5", False, False)
    End Sub
    
    Function findall_snb1(c01, c02, Optional opt_1 As Boolean, Optional opt_2 As Boolean)
      ' c01 search string
      ' c02 range to be searched in
      ' opt_1 whole match=true, partial match=false
      ' opt_2 case sensitive=true, case insensitive=false
    
      For Each cl In Range(c02)
        If opt_1 And opt_2 And StrComp(cl, c01, vbTextCompare) = 0 Then findall_snb1 = findall_snb1 & "_" & cl.Address
        If opt_1 And opt_2 = False And cl = c01 Then findall_snb1 = findall_snb1 & "_" & cl.Address
        If opt_1 = False And opt_2 And InStr(cl, c01) Then findall_snb1 = findall_snb1 & "_" & cl.Address
        If opt_1 = False And opt_2 = False And InStr(1, cl, c01, vbTextCompare) Then findall_snb1 = findall_snb1 & "_" & cl.Address
      Next
    End Function
    Code:
    Sub tst2()
      findall_snb2 "cut", "A1:E5", True, True
      findall_snb2 "cut", "A1:E5", True, False
      findall_snb2 "cut", "A1:E5", False, True
      findall_snb2 "cut", "A1:E5", False, False
    End Sub
    
    Sub findall_snb2(c01, c02, opt_1, opt_2)
      ' c01 search string
      ' c02 range to be searched in
      ' opt_1 whole match=true, partial match=false
      ' opt_2 case sensitive=true, case insensitive=false
    
      Names.Add "snb_1", "=" & Chr(34) & c01 & Chr(34)
      Range(c02).Name = "snb_2"
      Names.Add "snb_3", "=" & 1 - opt_1 - 2 * opt_2
    
      sn = [if(choose(snb_3,iserror(search(snb_1,snb_2)),not(snb_1=snb_2),iserror(find(snb_1,snb_2)),not(exact(snb_1,snb_2))),"",address(row(snb_2),column(snb_2)))]
    
      For Each cl In sn
        If cl <> "" Then
          If IsEmpty(c03) Then
             Set c03 = Application.Union(Range(cl), Range(cl))
          Else
             Set c03 = Application.Union(c03, Range(cl))
          End If
        End If
      Next
    
      Debug.Print [snb_3] & "__" & c03.Address
    End Sub
    Last edited by snb; 06-12-2012 at 02:39 AM.

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. FindAll Function In VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 4
    Last Post: 02-19-2012, 04:11 PM

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
  •