+ Reply to Thread
Results 1 to 7 of 7

Thread: InStr function using an array

  1. #1
    Valued Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    328

    InStr function using an array

    Hi folks,

    I'm trying to use the following code to check for the existence of more than one string (ie (1) or (2) or (3) or (4)) in a filename in order to remove these strings from the filename

    I'm getting a "Data Type Mismatch" error when the routine goes into the function on the line "For i = 0 To UBound(ArrayVals)"

    I suspect it's because I'm looking for a string and not a number.

    Can anyone point me in the right direction please

    Sub CeckArray()
    Dim ArrayVals
    
    ArrayVals = Array((1), (2), (3), (4))
    
    ' start loop here to check wksheet
    If InArray("(3)") Then
      Debug.Print "Value is in Array"
    End If
    
    End Sub
    
    
    Function InArray(strValue)
      Dim i
      For i = 0 To UBound(ArrayVals)
        If ArrayVals(i) = CStr(strValue) Then
          InArray = True
          Exit Function
        End If
      Next
      InArray = False
    End Function

    Many thanks

    Seamus
    Last edited by SOS; 08-18-2011 at 09:00 AM. Reason: Marking thread as Solved

  2. #2
    Forum Guru Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    MS Excel 2010
    Posts
    2,247

    Re: InStr function using an array

    Use regular expressions

    Function StringContains(ByVal LookAt As String, ByVal LookFor As String) As Boolean
        Dim RegEx As Object
        Set RegEx = CreateObject("VBScript.RegExp")
        RegEx.Pattern = "[" & LookFor & "]"
        RegEx.Global = True
        StringContains = RegEx.Test(LookAt)
        Set RegEx = Nothing
    End Function
    use like so

    ?StringContains("Bob12is4her23e", 1234)

  3. #3
    Valued Forum Contributor
    Join Date
    07-16-2010
    Location
    Northumberland, UK
    MS-Off Ver
    Excel 2007 (home), Excel 2010 (work)
    Posts
    1,957

    Re: InStr function using an array

    The problem is probably that your function does not have access to the array variable that you've defined.

    My lightly re-written version of your code seems to work fine...

    Sub Test()
    
    Dim asTestArray()
    
    asTestArray = Array("(1)", "(2)", "(3)")
    
    MsgBox InArray("(3)", asTestArray())
    
    
    End Sub
    
    Function InArray(ByVal sSearchVal As String, asSearchArray()) As Boolean
    
    Dim lArrayLoop As Long
    Dim bTempVal As Boolean
    
    bTempVal = False
    
    For lArrayLoop = LBound(asSearchArray) To UBound(asSearchArray)
      If asSearchArray(lArrayLoop) = sSearchVal Then
        bTempVal = True
      End If
    Next lArrayLoop
    
    InArray = bTempVal
    
    End Function

  4. #4
    Valued Forum Contributor
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2003
    Posts
    2,488

    Re: InStr function using an array

    hi, SOS, for the matter of learning:

    Sub CeckArray()
    Dim ArrayVals
    
    ArrayVals = Array("1", "2", "3", "4")
    
    ' start loop here to check wksheet
    If InArray(ArrayVals, "3") Then
      Debug.Print "Value is in Array"
    End If
    
    End Sub
    Function InArray(x, strValue)
      Dim i
      For i = 0 To UBound(x)
        If x(i) = strValue Then
          InArray = True
          Exit Function
        End If
      Next
      InArray = False
    End Function

  5. #5
    Valued Forum Contributor
    Join Date
    11-11-2008
    Location
    Euro
    MS-Off Ver
    2003, 2007
    Posts
    384

    Re: InStr function using an array

    Quote Originally Posted by SOS View Post
    Hi folks,


    Can anyone point me in the right direction please
    Plz, mention that whether "ArrayVals " is a public variable?
    because the program need access "ArrayVals" in the both the sub and fuction
    Best regard, -)iger-/iger
    If you are pleased with a solution mark your post SOLVED.

  6. #6
    Forum Guru snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,151

    Re: InStr function using an array

    You can't use instr for an array, use 'match' instead.

    Sub snb()
      sn = Array("(1)", "(2)", "(3)", "(4)")
      msgbox Application.Match("(3)", sn, 0)
    End Sub
    You can use instr for a string
    Sub snb2()
      sn = Array("(1)", "(2)", "(3)", "(4)")
      msgbox instr(Join(sn),"(3)")
    End Sub
    Last edited by snb; 08-18-2011 at 09:01 AM.



  7. #7
    Valued Forum Contributor SOS's Avatar
    Join Date
    01-26-2004
    Location
    Glasgow, Scotland
    MS-Off Ver
    Excel 2003
    Posts
    328

    Re: InStr function using an array

    Wow, what can I say? I knew this was the place to come for an answer. Such speedy (and correct) replies.

    Many thanks

    Seamus

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0