+ Reply to Thread
Results 1 to 3 of 3

VBA Find Partial String in String Array and Output the Found String Array Value

Hybrid View

  1. #1
    Registered User
    Join Date
    04-25-2013
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    9

    VBA Find Partial String in String Array and Output the Found String Array Value

    Okay, so I am trying to do a sort of index match thing using VBA. What I am attempting to do is to use the prefix of a long number and try to find that exact prefix in a string array, and output that string array value. So this is what I have so far as a test:

    Function abc(s As String) As String
    Dim PRE(11) As String
    Dim str As String
    
    
    PRE(0) = "PREFIXAAA"
    PRE(1) = "PREFIXB"
    PRE(2) = "PREFIXCCCCC"
    PRE(3) = "PREFIX1AAA"
    
    ....
    
    str = Cells(1, 1).Text
    
        If IIA(str, PREFIX) = True Then
            MsgBox ("YES")
          
            Else
            MsgBox ("NO")
            End If
    
    End Function
    
    
    
    Function IIA(stringToBeFound As String, arr As Variant) As Boolean
        IIA = Not IsError(Application.Match(stringToBeFound, arr, 0))
    End Function
    So I can match the text exactly so if I put PREFIXB in cell A1 in this example, i will get the msg box saying "YES", but if I make it PREFIXB1231k4j3jj1kj32cj, it will display "NO". I want to get it so that PREFIXB will be displayed in the cell that I put the formula in. So if A1 = "PREFIX1AAA100CF" and cell B1 = "=ABC(A1)", cell B1 will display "PREFIX1AAA".

    Now the thing is that these prefixes can have different lengths, but will never encompass the exact prefix of another. So if I had a prefix of: PRE1AB, I won't have a prefix of PRE1A.

    Thanks for any help!

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: VBA Find Partial String in String Array and Output the Found String Array Value

    How is the input parameter s being used??
    Gary's Student

  3. #3
    Registered User
    Join Date
    04-25-2013
    Location
    Somewhere
    MS-Off Ver
    Excel 2010
    Posts
    9

    Re: VBA Find Partial String in String Array and Output the Found String Array Value

    I am not really using s as an input parameter for this code. For some reason the code only works with (s as string) in there. I came up with my own solution to this problem (any input/advice would be appreciated); however, it isn't very elegant:

    Function abc(S As String) As String
    Dim PREFIX(11) As String
    Dim LENGTH(11) As String
    Dim min As Long
    Dim max As Long
    Dim Full As String
    Dim Part As String
    Dim Ans As String
    
    'Define Array Values
    PREFIX(0) = "PRE1"
    PREFIX(1) = "PRE2"
    PREFIX(2) = "PRE3A"
    PREFIX(3) = "PRE44444"
    PREFIX(4) = "PRE5"
    PREFIX(5) = "PRECCCCCCCCC"
    ...
    
    ' Choose Cell Value to Compare to Array
    Full = Cells(1, 1).Text
    
    ' Obtain Array Text Length (LBound, UBound)
    For j = LBound(PREFIX) To UBound(PREFIX)
    LENGTH(j) = Len(PREFIX(j))
    Next j
    
    
    ' Find Min and Max Text Length
    min = 100
    max = -100
    
    For k = LBound(PREFIX) To UBound(PREFIX)
        If LENGTH(k) > max Then max = LENGTH(k)
        If LENGTH(k) < min Then min = LENGTH(k)
    Next
    
    ' Find Match in Array
    For i = min To max
    
        Part = Left(Full, i)
        
        If IIA(Part, PREFIX) = True Then
            Ans = Application.WorksheetFunction.Index(PREFIX, Application.WorksheetFunction.Match(Part, PREFIX, 0))
            abc = Ans
        Else
        End If
    Next i
    
        
    End Function
    
    
    
    Function IIA(stringToBeFound As String, arr As Variant) As Boolean
        IIA = Not IsError(Application.Match(stringToBeFound, arr, 0))
    End Function

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Trying to match partial string to an array, need to output related cell
    By vijaijohn in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-23-2013, 03:04 PM
  2. [SOLVED] Partial Match Cell To String Array
    By amazinglazers in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 08-09-2013, 05:56 AM
  3. [SOLVED] problem finding string using .find method when string to be found contains ~ (a tilde)
    By welchs101 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-01-2013, 07:38 AM
  4. [SOLVED] Vlookup, Match (Search or Find) partial string within string in a Cell
    By dluhut in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 05-28-2013, 12:40 PM
  5. [SOLVED] Return matching string in array if cell contains string contained in the array
    By AaronsZ in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-03-2013, 01:55 PM

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