anon
anon

Reputation:

VB6: Grabbing String Starting with x From an Array

Let's say I have the following Array, arr that contains a maximum of 20 substrings. In this example, the array will have 4 substrings:

arr = {"AAAA: 1234567" , "BBBB: 2345678" , "CCCC: 98765432" , "DDDD: 87654321"}

Note: Capitalization is not important.

I am needing assistance with finding a substring inside an array (again, up to 20 strings possible), that Starts With CCCC:, and then assigning that entire string to it's own variable, let's call it Var1.

I understand I could assign Var1 = arr(2), but I need to figure out how to determine the index number in the array that meets my criteria.

What I would really appreciate (although any method I will be happy with), is making a stand alone function such as:

Function ArrayIndex(ArrayInput as Variant, StartsWith as String) as Byte
   'Arguments Here
End Function

Then I could just use this in my Subroutine:

Var1 = arr(ArrayIndex(arr, "CCCC:"))

Update 1

Here is a snippet of my current code

Sub T_Report
    '<Shortcut = Shift+Ctrl+T>

    Dim Data as String, DataArray as Variant
    With ActiveSession
        .Copy 0, 2, 80, 21  'Just a big block of text with multiple lines, copied to clipboard
        Data = Clipboard    'Set Data to the clipboard value
        DataArray = Split(Data,vbCrLf)  'This is "Data" in an Array, separated by line breaks

        'Just checking to see if the Array was successful (it is) 
        Debug.Print DataArray(0) & vbNL & DataArray(1) & vbNL & DataArray(2) & _
            vbNL & DataArray(3) & vbNL & DataArray(4) & vbNL & vbNL


        'Misc code here


        Dim sF1 as String, sF2 as String, sF3 as String
        Dim Rslt1 as String, Rslt2 as String, Rslt3 as String
        sF1 = "Field1:"
        sF2 = "Field2:"
        sF3 = "Field3:"

        MsgBox DataArray(0)     ' This works fine, giving me first substring
        Rslt1 = FindMyString(sF1, DataArray)

        ' Misc Code

    End With
End Sub

However, when I use the following function, I get Type Mismatch error on the MsgBox arr(0) line, although it should be giving me the very first substring of DataArray in the above sub (should be an exact match, the array has not been modified).. However, when I do MsgBox DataArray(0) above, I do get the first substring.

Private Function FindMyString(strToFind As String, ParamArray arr() As Variant) As String

    Dim i As Integer
    Dim iLen As Integer
    Dim strArr As String

    FindMyString = ""   ' Returns Blank String if not found

    ' I get type mismatch here (Doesn't appear Array from sub loaded into this function)
    MsgBox arr(0)   

    iLen = Len(strToFind)
    For i = 0 To UBound(arr)
    strArr = CStr(arr(i))
        If strToFind = Left$(strArr, iLen) Then
            FindMyString = strArr
            Exit Function
        End If
    Next i

End Function

Upvotes: 1

Views: 128

Answers (1)

dbmitch
dbmitch

Reputation: 5386

EDIT - FIX YOUR ARRAY LOAD

Change this (it MUST give you an error!):

arr = {"AAAA: 1234567" , "BBBB: 2345678" , "CCCC: 98765432" , "DDDD: 87654321"}

To This:

Dim arr As Variant
arr = Split("AAAA: 1234567,BBBB: 2345678,CCCC: 98765432,DDDD: 87654321", ",")

If you want to use a function to do your bidding you can pass an array of data using the ParamArray type. It has to be the last parameter in your function

This should do what you want:

Private Function FindMyString(strToFind as string, ParamArray arr() As Variant) As String

   Dim i As Integer
   Dim iLen as Integer
   Dim strArr as String

   FindMyString = "" ' Returns Blank String if not found '

   Debug.Print arr(0)(0) ' Print first element of array

   iLen = Len(strToFind)
   For i = 0 To UBound(arr(0))
      strArr = CStr(arr(0)(i))
      If strToFind = Left$(strArr, iLen) Then
          FindMyString = strArr
          Exit Function
      End If
   Next i

End Function

In your example you can test it by:

Var1 = FindMyString("CCCC:", arr)

Upvotes: 2

Related Questions