XL 2016 Fonction personnalisé par VBA recherche dans une critère dans un chaine de caractère

Kaka133

XLDnaute Nouveau
Salut
merci de m'aider à trouver une fonction personnalisé par VBA pour rechecher une critère dans un chaine de caractère ci-dessous l’exemple.

merci d’avance
gg.JPG
 

Pièces jointes

  • Tableau de recherche Example.xlsx
    12.4 KB · Affichages: 4
Solution
Re,

Le code qui suit devrait le faire (avec ou sans espace) :
VB:
Function Ville(ByVal xID, xRecherche)
Dim t, ref, i&
   t = xRecherche.Value: xID = Mid(Replace(xID, " ", ""), 4)
   ref = Val(Left(xID, Len(xID) - 3))
   For i = 1 To UBound(t)
      If ref = t(i, 1) Then Ville = t(i, 2): Exit Function
   Next i
End Function

mapomme

XLDnaute Barbatruc
Supporter XLD
Bonjour @Kaka133

Cliquez sur le bouton Hop !
Le code dans module1 :
VB:
Sub Macro()
With Sheets("Feuil1").ListObjects("Tableau24").DataBodyRange.Columns(2)
   .FormulaR1C1 = "=VLOOKUP(VALUE(LEFT(SUBSTITUTE(MID([@ID],5,9),"" "",""   ""),3)),Tableau1,2,FALSE)"
   .Value = .Value
   End With
End Sub
 

Pièces jointes

  • Kaka133- Tableau de recherche- v1.xlsm
    20.4 KB · Affichages: 2

chris

XLDnaute Barbatruc
Bonjour

Sans VBA :
VB:
=RECHERCHEV(STXT([@ID];CHERCHE(" ";[@ID])+1;CHERCHE("@";SUBSTITUE([@ID];" ";"@";2))-1-CHERCHE(" ";[@ID]))*1;Tableau1;2;0)

En VBA
Code:
Function Ch_Pays(ID As Range, Tabl As Range)
    Parties = Split(ID, " ")
    With Tabl.ListObject
        Set ID2 = .ListColumns(1).DataBodyRange.Find(Parties(1))
        y = .Range.Row
        Ch_Pays = .ListColumns(2).DataBodyRange.Rows(ID2.Row - y)
    End With
End Function
 

mapomme

XLDnaute Barbatruc
Supporter XLD
Re,

Avec une fonction personnalisée Vilkle()( dans module1 : Ville(xID, xRecherche) où xID est l'ID à rechercher et Xrecherche la plage de recherche.

Le code :
VB:
Function Ville(xID, xRecherche)
Dim t, ref, i&
   t = xRecherche.Value: ref = Val(Mid(xID, 5, 3))
   For i = 1 To UBound(t)
      If ref = t(i, 1) Then Ville = t(i, 2): Exit Function
   Next i
End Function
 

Pièces jointes

  • Kaka133- Tableau de recherche- v2.xlsm
    21.8 KB · Affichages: 2

Kaka133

XLDnaute Nouveau
Re,

Avec une fonction personnalisée Vilkle()( dans module1 : Ville(xID, xRecherche) où xID est l'ID à rechercher et Xrecherche la plage de recherche.

Le code :
VB:
Function Ville(xID, xRecherche)
Dim t, ref, i&
   t = xRecherche.Value: ref = Val(Mid(xID, 5, 3))
   For i = 1 To UBound(t)
      If ref = t(i, 1) Then Ville = t(i, 2): Exit Function
   Next i
End Function
je tiens à vous remercier pour votre aide et votre soutien
 

Kaka133

XLDnaute Nouveau
Bonjour

Sans VBA :
VB:
=RECHERCHEV(STXT([@ID];CHERCHE(" ";[@ID])+1;CHERCHE("@";SUBSTITUE([@ID];" ";"@";2))-1-CHERCHE(" ";[@ID]))*1;Tableau1;2;0)

En VBA
Code:
Function Ch_Pays(ID As Range, Tabl As Range)
    Parties = Split(ID, " ")
    With Tabl.ListObject
        Set ID2 = .ListColumns(1).DataBodyRange.Find(Parties(1))
        y = .Range.Row
        Ch_Pays = .ListColumns(2).DataBodyRange.Rows(ID2.Row - y)
    End With
End Function
je vous remercie pour votre aide
 

Kaka133

XLDnaute Nouveau
Re,

Avec une fonction personnalisée Vilkle()( dans module1 : Ville(xID, xRecherche) où xID est l'ID à rechercher et Xrecherche la plage de recherche.

Le code :
VB:
Function Ville(xID, xRecherche)
Dim t, ref, i&
   t = xRecherche.Value: ref = Val(Mid(xID, 5, 3))
   For i = 1 To UBound(t)
      If ref = t(i, 1) Then Ville = t(i, 2): Exit Function
   Next i
End Function
Bonjour

Sans VBA :
VB:
=RECHERCHEV(STXT([@ID];CHERCHE(" ";[@ID])+1;CHERCHE("@";SUBSTITUE([@ID];" ";"@";2))-1-CHERCHE(" ";[@ID]))*1;Tableau1;2;0)

En VBA
Code:
Function Ch_Pays(ID As Range, Tabl As Range)
    Parties = Split(ID, " ")
    With Tabl.ListObject
        Set ID2 = .ListColumns(1).DataBodyRange.Find(Parties(1))
        y = .Range.Row
        Ch_Pays = .ListColumns(2).DataBodyRange.Rows(ID2.Row - y)
    End With
End Function
Salam

la fonction recherche ne fonctionne pas les caractère sans espace
 

Pièces jointes

  • Tableau de recherche Example.xlsx
    12.4 KB · Affichages: 2

mapomme

XLDnaute Barbatruc
Supporter XLD
Re,

Le code qui suit devrait le faire (avec ou sans espace) :
VB:
Function Ville(ByVal xID, xRecherche)
Dim t, ref, i&
   t = xRecherche.Value: xID = Mid(Replace(xID, " ", ""), 4)
   ref = Val(Left(xID, Len(xID) - 3))
   For i = 1 To UBound(t)
      If ref = t(i, 1) Then Ville = t(i, 2): Exit Function
   Next i
End Function
 

Discussions similaires

Membres actuellement en ligne

Statistiques des forums

Discussions
312 084
Messages
2 085 194
Membres
102 813
dernier inscrit
kaiyi