[VBA] Comparer variable avec un tableau

suistrop

XLDnaute Impliqué
Bonjour,

J'aurai voulu savoir si il existe une fonction sous VBA permettant de rechercher si une chaine de caractere se trouve à l'intérieur d'un tableau.
Comme ci dessous

Code:
if "toto" in array("tata","tutu","titit") then top = 1

Je n'ai pas envie de faire :
Code:
tableau = array("tata","tutu","titit") 
for z = ubound(tableau) to ubound(tableau)
if "toto" = tableau(z) then top = 1
next z

Merci d'avance
 

pierrejean

XLDnaute Barbatruc
Re : [VBA] Comparer variable avec un tableau

bonjour suistrop

Si le tableau n'est pas utilisé par ailleurs:

Code:
laliste="tata;tutu;titit;"
if instr(laliste,"toto" & ";" )<> 0 then top=1

D'autre part il vaut mieux

Code:
tableau = array("tata","tutu","titit") 
for z = [COLOR=blue]lbound[/COLOR](tableau) to ubound(tableau)
if "toto" = tableau(z) then top = 1
next z

Edit : Salut JB
 

mromain

XLDnaute Barbatruc
Re : [VBA] Comparer variable avec un tableau

bonjour suistrop

Si le tableau n'est pas utilisé par ailleurs:

Code:
laliste="tata;tutu;titit;"
if instr(laliste,"toto" & ";" )<> 0 then top=1
D'autre part il vaut mieux

Code:
tableau = array("tata","tutu","titit") 
for z = [COLOR=blue]lbound[/COLOR](tableau) to ubound(tableau)
if "toto" = tableau(z) then top = 1
next z
Edit : Salut JB
bonjour suistrop, pierrejean, JB,

Une autrre solution à partir de la première solution proposée par pierrejean :
Code:
tableau = array("tata","tutu","titit")
if instr(Join(tableau, ";"),"toto" & ";" )<> 0 then top=1

a+
 

suistrop

XLDnaute Impliqué
Re : [VBA] Comparer variable avec un tableau

Salut pierrejean,

Tu laisses rien passé, meme pas le droit de faire une erreur en tapant un code bidon :D

Ton idée est pas mal pour comparer. Je me suis pas encore pencher sur les différentes méthodes de comparaison en VBA...
 

Discussions similaires

Réponses
2
Affichages
241

Statistiques des forums

Discussions
312 232
Messages
2 086 461
Membres
103 220
dernier inscrit
samira2024