+ Reply to Thread
Results 1 to 8 of 8

find string pos from strings in an array

Hybrid View

friscokid find string pos from strings... 09-29-2008, 06:54 PM
shg InStr locates a substring... 09-29-2008, 07:04 PM
friscokid Thanks, but match function... 09-30-2008, 11:28 AM
StephenR That's what the match... 09-30-2008, 11:30 AM
friscokid it looks like MATCH does just... 09-30-2008, 11:47 AM
StephenR I don't know where you're... 09-30-2008, 11:51 AM
shg So you are, sorry. You can... 09-30-2008, 11:59 AM
Leith Ross Hello friscokid, This... 09-30-2008, 12:54 PM
  1. #1
    Registered User
    Join Date
    09-11-2008
    Location
    Texas
    Posts
    7

    find string pos from strings in an array

    I am trying to find the string position of a string, and the string I am looking FOR is from an array of terms in another worksheet. I have it looking like this, but am getting a "type mismatch error" on this line...

    termPos = InStr(Cell.Value, termsArray)

    Here is my entire code. Any help will be appreciated.

    Sub find_term_position()
    
    Dim i As Long
    Dim termsArray() 
    
    Dim termsCount As Integer
    Dim prodsCount As Integer
    Dim termPos As Integer
    
    Dim Cell As Range
    
    Dim wsProds As Worksheet
    Dim wsTerms As Worksheet
    
    Set wsProds = Worksheets("products")
    Set wsTerms = Worksheets("terms")
    
    'number of terms
    termsCount = wsTerms.Range("A1").End(xlDown).Row 
    
    ReDim Preserve termsArray(wsTerms.Range("A1:A" & termsCount))
    
    For i = LBound(termsArray) To UBound(termsArray)
        termsArray(i) = wsTerms.Range("A" & i + 1)
    Next i
    
    'number of products
    prodCount = wsProds.Range("A1").End(xlDown).Row 
    
    For Each Cell In wsProds.Range("A1:A" & prodCount)
        
           termPos = InStr(Cell.Value, termsArray)
    
            If termPos > 0 Then
            'string found
            Cell.Offset(0, 1).Value = "Term found at: " & termPos
        Else
            'string not found
            Cell.Offset(0, 1).Value = "Term not found"
       End If
    
    Next Cell
    
    End Sub

  2. #2
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    InStr locates a substring within another string, not in an array.

    You might use the Match function.
    Entia non sunt multiplicanda sine necessitate

  3. #3
    Registered User
    Join Date
    09-11-2008
    Location
    Texas
    Posts
    7
    Thanks, but match function won't work for my needs. I need to have the position so that I can use the mid function to make modifications.

    So to use inStr, I will need to loop through the array. Right?

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    That's what the match function does. Read shg's post again.

  5. #5
    Registered User
    Join Date
    09-11-2008
    Location
    Texas
    Posts
    7
    it looks like MATCH does just the opposite. It looks for a substring within an array. My array contains the substrings, and I need to find substring position within the string. Is there a way to do this?

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    I don't know where you're looking. Why don't you attach a sample of what you're doing?

  7. #7
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689
    So you are, sorry.

    You can do this with an array formula from the user interface, e.g.,

    {=MATCH(TRUE, ISNUMBER(SEARCH(B1:B3, A1)), 0)}

    ... but in VBA, I think you need to iterate through the array.
    Last edited by shg; 09-30-2008 at 12:16 PM.

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

    This version of your macro takes advantage of the Range Objects properties and methods to eliminate the need for nested looping and a dynamic array. This code can be modified to find multiple occurances of the substring quite easily, if you need that feature later.
    Sub find_term_position()
    
      Dim ProdsRng As Range
      Dim termPos As Integer
      Dim TermsRng As Range
      Dim wsProds As Worksheet
      Dim wsTerms As Worksheet
    
        Set wsProds = Worksheets("products")
        Set wsTerms = Worksheets("terms")
    
          Set ProdsRng = wsProds.Cells(1, "A").CurrentRegion.Resize(Columnsize:=1)
          Set TermsRng = wsTerms.Cells(1, "A").CurrentRegion.Resize(Columnsize:=1)
          
            For Each Prod In ProdsRng
              Set Term = Terms.Find(What:=Prod, _
                                    After:=Terms.Cells(1, 1), _
                                    LookIn:=xlValues, _
                                    LookAt:=xlWhole, _
                                    SearchBy:=xlRows, _
                                    SearchDirection:=xlNext, _
                                    MatchCase:=False)
              
              If Not Term Is Nothing Then
                termPos = InStr(1, Prod, Term.Text)
                  If termPos > 0 Then
                    Prod.Offset(0, 1) = "Term found at: " & termPos
                  Else
                    Prod.Offset(0, 1) = "Term not Found"
                  End If
              End If
            Next Prod
    
    End Sub
    Sincerely,
    Leith Ross

+ 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. Find Specific string
    By janjan_376 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 07-30-2009, 12:38 PM
  2. Desire Versatile Alphanumeric Extractor From Any String
    By bxc2739 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-25-2007, 04:12 PM
  3. How to find and extract numbers that may be present in lengthy text strings
    By Langkawi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-15-2007, 03:17 PM
  4. Replies: 2
    Last Post: 02-22-2007, 12:42 PM
  5. Replies: 7
    Last Post: 02-19-2007, 12:17 AM

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