FIND IN ARRAY

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

FIND IN ARRAY

Post by sal21 »

I rfill array with:

....

Code: Select all

 ReDim Preserve ARRAY_IMGLIST(P1)
            ARRAY_IMGLIST(P1) = P1 & "|" & Split(STATO, "-")(1)
            P1 = P1 + 1
...

is a good idea to find item with:

If InStr(1, Join((ARRAY_IMGLIST)), vbNullChar, TR) >= 1 Then
Debug.Print IDX
End If

in my case i need to get the P1

NOTE:
ARRAY_IMGLIST is Variant dimensioned
TR is the second part of array ( Split(STATO, "-")(1))

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FIND IN ARRAY

Post by HansV »

What is IDX?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

HansV wrote:
27 Feb 2022, 13:19
What is IDX?
i need to fill IDX with the result, sorry, i have used for test

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FIND IN ARRAY

Post by HansV »

Code: Select all

    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        IDX = Mid(S, Q + 1, P - Q - 1)
        Debug.Print IDX
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

HansV wrote:
27 Feb 2022, 14:57

Code: Select all

    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        IDX = Mid(S, Q + 1, P - Q - 1)
        Debug.Print IDX
    End If
Super! :hairout:

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

sal21 wrote:
27 Feb 2022, 20:03
HansV wrote:
27 Feb 2022, 14:57

Code: Select all

    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        IDX = Mid(S, Q + 1, P - Q - 1)
        Debug.Print IDX
    End If
Super! :hairout:
Hummmmm

But you use and if and if, but Is to check if item existst or not?
In this case all item are il list...

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FIND IN ARRAY

Post by HansV »

Sorry, what exactly are you asking?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

HansV wrote:
27 Feb 2022, 20:20
Sorry, what exactly are you asking?
'M WRONG, SORRY.
ALL IS OK.

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

HansV wrote:
27 Feb 2022, 14:57

Code: Select all

    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        IDX = Mid(S, Q + 1, P - Q - 1)
        Debug.Print IDX
    End If
bro...
possible to use this code as function, to pass the parameter TR and return F?

User avatar
HansV
Administrator
Posts: 78391
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: FIND IN ARRAY

Post by HansV »

Code: Select all

Function GetIndex(TR)
    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        GetIndex = Mid(S, Q + 1, P - Q - 1)
    End If
End Function
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

HansV wrote:
09 Mar 2022, 21:03

Code: Select all

Function GetIndex(TR)
    Dim S As String
    Dim P As Long
    Dim Q As Long
    S = "|" & Join(ARRAY_IMGLIST, "|") & "|"
    P = InStr(S, "|" & TR & "|")
    If P > 0 Then
        Q = InStrRev(S, "|", P - 1)
        GetIndex = Mid(S, Q + 1, P - Q - 1)
    End If
End Function
tks.
:clapping:

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: FIND IN ARRAY

Post by SpeakEasy »

Find in (string) array? This is the sort of thing the Filter method is designed for!

e.g.

Code: Select all

    Dim filterresult() As String
    filterresult = Filter(ARRAY_IMGLIST(), TR)
    If UBound(filterresult) <> -1 Then IDX = Split(filterresult(0), "|")(0) 'IDX

User avatar
sal21
PlatinumLounger
Posts: 4343
Joined: 26 Apr 2010, 17:36

Re: FIND IN ARRAY

Post by sal21 »

SpeakEasy wrote:
10 Mar 2022, 11:43
Find in (string) array? This is the sort of thing the Filter method is designed for!

e.g.

Code: Select all

    Dim filterresult() As String
    filterresult = Filter(ARRAY_IMGLIST(), TR)
    If UBound(filterresult) <> -1 Then IDX = Split(filterresult(0), "|")(0) 'IDX

nice!
Tks

but for function, for pass TR?

User avatar
SpeakEasy
4StarLounger
Posts: 544
Joined: 27 Jun 2021, 10:46

Re: FIND IN ARRAY

Post by SpeakEasy »

Code: Select all

Function GetIndex(TR)
    Dim filterresult() As String
    filterresult = Filter(ARRAY_IMGLIST(), TR)
    If UBound(filterresult) <> -1 Then GetIndex = Split(filterresult(0), "|")(0)
End Function

User avatar
rory
5StarLounger
Posts: 817
Joined: 24 Jan 2010, 15:56

Re: FIND IN ARRAY

Post by rory »

Just bear in mind that Filter returns any items that contain the searched value. They don't have to be an exact match. So for example filtering for "test" would match "latest" too.
Regards,
Rory