Excel VBA Userform - utilisation variable dans IF..THEN
Bonjour à tous,
J'ai une variable string (appelée "myrech") dans laquelle je crée la syntaxe pour la condition du IF, mais si j'essaie de l'utiliser dans le IF ça ne fonctionne pas
Ci-dessous le code que j'ai tenté de mettre en place, ça marche bien jusqu'à if myrech Then mais là je bloque, j'ai tenté avec
If Application.Evaluate("IF(" &myrech & ", true, false)") Then mais échec là aussi...
Vu le nombre de possibilités avec 11 critères optionnels j'aimerais bien arriver à utiliser ma variable "myrech" qui me crée toute la syntaxe de condition du IF. Donc d'avance merci pour votre aide !!!
Précision ce code est appelé depuis un commandButton dans un Userform.
Voilà le code en question
Dim compteur As Integer
Dim cpt As Integer
Dim nbcrit As Integer
Dim ctrl As Control
Dim tabloWhat()
Dim tabloCrit() As String
Dim tabloC() As String
Dim col As String
' Dim rech() As String
Dim myrech As String
Dim tabloRes()
nbcrit = 0
ReDim tabloWhat(11, 3)
With Me
For Each ctrl In .Controls
If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
If ctrl.Name = "txtDateFact" Then
col = "2"
End If
If ctrl.Name = "txtNumFact" Then
col = "1"
End If
If ctrl.Name = "cmbFourniCompo" Then
col = "3"
End If
If ctrl.Name = "txtRefCompo" Then
col = "4"
End If
If ctrl.Name = "cmbTypeMat" Then
col = "5"
End If
If ctrl.Name = "cmbMat" Then
col = "6"
End If
If ctrl.Name = "cmbTypeCompo" Then
col = "7"
End If
If ctrl.Name = "txtNomCompo" Then
col = "8"
End If
If ctrl.Name = "txtQteCompo" Then
col = "9"
End If
If ctrl.Name = "cmbUnits" Then
col = "10"
End If
If ctrl.Name = "txtPrixTHTCompo" Then
col = "11"
End If
If ctrl.Value <> "" Then
nbcrit = nbcrit + 1
tabloWhat(nbcrit, 1) = ctrl.Name
tabloWhat(nbcrit, 2) = col
tabloWhat(nbcrit, 3) = ctrl.Value
End If
End If
Next ctrl
ReDim tabloCrit(1 To nbcrit)
compteur = 0
For i = 1 To UBound(tabloAchats)
For j = 1 To nbcrit
mystring = "tabloWhat(" & j & "," & 3 & ") = tabloAchats(" & i & ", tabloWhat(" & j & ", 2))"
tabloCrit(j) = mystring
Next j
myrech = Join(tabloCrit, " And ")
myrech = mystring
Debug.Print myrech
if myrech Then '<--------------là ça foire !
'do some stuff
Else
'do something else
end if
Next ibonjour,
Ci-dessous le code que j'ai tenté de mettre en place, ça marche bien jusqu'à mais là je bloque, j'ai tenté avec
If Application.Evaluate("IF(" &myrech & ", true, false)") Then mais échec là aussi...
if myrech Thence que tu veux faire n'est pas possible de cette manière
Application.Evaluate("IF(" &myrech & ", true, false)") ceci est correct au niveau de la syntaxe vba, il faut que le reste soit correct au niveau de la syntaxe de la formule excel.
bonjour h2so4
Application.Evaluate("IF(" & myrech & ", true, false)") est effectivement correct au niveau syntaxe vba, je pense que la formule excel l'est aussi le Debug.Print me donne ceci :
tabloWhat(1,3) = tabloAchats(1, tabloWhat(1, 2)) And tabloWhat(2,3) = tabloAchats(1, tabloWhat(2, 2)) And tabloWhat(3,3) = tabloAchats(1, tabloWhat(3, 2))
qui me semble correct mais un Debug.Print Application.Evaluate(myrech) renvoie error 2029
...Comprends pas...
Bon après de nombreux essais, tous infructueux, je m'arrête là dessus pour ce soir...
Je rappelle quand même l'objectif principal :
- un formulaire de recherche avec plusieurs champs (critères) de recherche
- Aucun champ n'est obligatoire i.e. on peut remplir de 1 à 11 critères de recherche
J'arrive très bien à le faire quand tous les critères sont obligatoires, mais là... je sèche !
Toute aide serait la bienvenue
Merci à vous, prenez soin de vous et des autres
bonjour,
une solution que je n'ai pas pu tester (car pas de fichier)
Dim compteur As Integer
Dim cpt As Integer
Dim nbcrit As Integer
Dim ctrl As Control
Dim tabloWhat()
Dim tabloCrit() As String
Dim tabloC() As String
Dim col As Long
Dim myrech As Boolean
Dim tabloRes()
nbcrit = 0
ReDim tabloWhat(11, 3)
With Me
For Each ctrl In .Controls
If TypeName(ctrl) = "TextBox" Or TypeName(ctrl) = "ComboBox" Then
If ctrl.Name = "txtDateFact" Then
col = 2
End If
If ctrl.Name = "txtNumFact" Then
col = 1
End If
If ctrl.Name = "cmbFourniCompo" Then
col = 1
End If
If ctrl.Name = "txtRefCompo" Then
col = 4
End If
If ctrl.Name = "cmbTypeMat" Then
col = 5
End If
If ctrl.Name = "cmbMat" Then
col = 6
End If
If ctrl.Name = "cmbTypeCompo" Then
col = 7
End If
If ctrl.Name = "txtNomCompo" Then
col = 8
End If
If ctrl.Name = "txtQteCompo" Then
col = 9
End If
If ctrl.Name = "cmbUnits" Then
col = 10
End If
If ctrl.Name = "txtPrixTHTCompo" Then
col = 11
End If
If ctrl.Value <> "" Then
nbcrit = nbcrit + 1
tabloWhat(nbcrit, 1) = ctrl.Name
tabloWhat(nbcrit, 2) = col
tabloWhat(nbcrit, 3) = ctrl.Value
End If
End If
Next ctrl
ReDim tabloCrit(1 To nbcrit)
compteur = 0
For i = 1 To UBound(tabloachats)
myrech = True
For j = 1 To nbcrit
If tableauwhat(j, 3) <> tabloachats(i, tabloWhat(j, 2)) Then myrech = False: Exit For
Next j
If myrech Then
'do some stuff
Else
'do something else
End If
Next i