Ciao..scusa per il ritardo ma ho dovuto riscriverla in quanto non la trovavo più..
Per usarla occorrono delle piccole istruzioni:
codice:
Public Function Cerca(ByVal List As ListBox, ByVal WordToSearch As String, SearchType As Byte, PartialType As Byte, IgnoreShift As Boolean, ElementIndex() As Variant)
WordToSearch = parola da cercare
SearchType = 0--> ricerca integrale della parola, ossia restituisce l'index degli elementi formati dalla parola che cerchi
1--> ricerca graduale: se WordToSearch è per esempio "Ciao" puoi scegliere nel seguente argomento quante lettere di questa parola devi far cercare: ESEMPIO
SearchType = 1; PartialType = da 1 a 4 in quanto sono i caratteri della parola da cercare: se metti per esempio 2 trova tutti gli elementi il cui text è "ci". Questo serve quando si cerca una parola lunga e non viene trovato il risultato, allora si cerca graduatamente..
IgnoreShift = Booleano. Se posto uguale a True--> ignora i caratteri minuscoli e maiuscoli, quindi "CIaO" = "ciAO"; al contrario per avere un'uguaglianza esatta occorre mettere questo argomento uguale a False.
ElementIndex = matrice in cui vengono salvati gli index degli elementi che corrispondono alla ricerca.
codice:
Public Function Cerca(ByVal List As ListBox, ByVal WordToSearch As String, SearchType As Byte, PartialType As Byte, IgnoreShift As Boolean, ElementIndex() As Variant)
If SearchType <> 0 And SearchType <> 1 Then msg = "Argomento non valido: deve essere 0 o 1": GoTo Messaggi
If SearchType = 0 Then
If IgnoreShift = True Then
k = 0
For i = 0 To List.ListCount - 1
If UCase(List.List(i)) = UCase(WordToSearch) Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
Else
k = 0
For i = 0 To List.ListCount - 1
If List.List(i) = WordToSearch Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
End If
ElseIf SearchType = 1 Then
Select Case PartialType
Case Is = 0 'con funz. Instr
If IgnoreShift = True Then
k = 0
For i = 0 To List.ListCount - 1
If InStr(1, UCase(List.List(i)), UCase(WordToSearch), vbTextCompare) <> 0 Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
Else
For i = 0 To List.ListCount - 1
If InStr(1, List.List(i), WordToSearch, vbBinaryCompare) <> 0 Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
End If
Case Is <> 0
If Len(WordToSearch) < PartialType Then msg = "Il valore dell'argomento PartialType deve essere compreso" _
& Chr(13) & "tra 0 e la lunghezza dell'argomento WordToSearch": GoTo Messaggi
If IgnoreShift = True Then
k = 0
For i = 0 To List.ListCount - 1
If Left(UCase(WordToSearch), PartialType) = UCase(List.List(i)) Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
Else
For i = 0 To List.ListCount - 1
If Left(WordToSearch, PartialType) = (List.List(i)) Then
ReDim Preserve ElementIndex(k)
ElementIndex(k) = i
k = k + 1
End If
Next i
End If
End Select
End If
Exit Function
Messaggi:
MsgBox msg, vbCritical, "Funzione Cerca"
End Function
codice:
'Si usa così:
Dim matr() As Variant
Cerca List1, "dD", 1, 0, True, matr
On Error GoTo risolvi
For i = 0 To UBound(matr)
MsgBox matr(i)
Next i
Exit Sub
risolvi:
If Err.Number = 9 Then MsgBox "Non sono stati trovati elementi!"
E' probabile che tu non capisca tutto immediatamente perchè ho scritto velocemente, cmq fammi sapere..
ciao