Comment trouver si un tableau contient une chaîne

ForumBot
Messages : 26117
Inscription : mer. avr. 22, 2026 5:33 pm

Comment trouver si un tableau contient une chaîne

Message par ForumBot »

Doublon possible :


How to search for string in MS Access VBA array


Je travaille actuellement sur une macro Excel, et je n’ai pas trouvé de moyen de faire comme

if array.contains(mystring)


J’ai écrit ce qui suit, et cela me donne le message “Invalid Qualifier” et surligne le Mainfram juste après If


Dim Mainfram(4) As String

Mainfram(0) = "apple"

Mainfram(1) = "pear"

Mainfram(2) = "orange"

Mainfram(3) = "fruit"

For Each cel In Selection
If Mainfram.Contains(cel.Text) Then
Row(cel.Row).Style = "Accent1"
End If
Next cel


La sélection est une colonne.


Quelqu’un peut-il m’aider ?


Bonjour, JP

J’ai essayé votre suggestion, et cela a dit Object required. Et a surligné le

If IsInArray(cell.Text, Mainfram) Then

Voici mon code complet :


Sub changeRowColor()

Columns("B:B").Select

Dim cel As Excel.Range
Dim Mainfram(4) As String

Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "Banana"

For Each cel In Selection
If IsInArray(cell.Value, Mainfram) Then
Rows(cel.Row).Style = "Accent1"
End If
Next cel

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean

IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)

End Function


Peu importe, j’ai trouvé cette erreur stupide…

Merci quand même.

ayi
Site Admin
Messages : 13176
Inscription : mer. avr. 22, 2026 5:21 pm

Re: Comment trouver si un tableau contient une chaîne

Message par ayi »

En utilisant le code de ma réponse à une question très similaire :


Sub DoSomething()
Dim Mainfram(4) As String
Dim cell As Excel.Range

Mainfram(0) = "apple"
Mainfram(1) = "pear"
Mainfram(2) = "orange"
Mainfram(3) = "fruit"

For Each cell In Selection
If IsInArray(cell.Value, MainFram) Then
Row(cell.Row).Style = "Accent1"
End If
Next cell

End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

Répondre

Revenir à « Excel & VBA »