Index.Match #N/A

Calvus

XLDnaute Barbatruc
Bonjour le Forum,

Après une nuit à chercher et devenir fou, quelqu'un peut il m'expliquer pourquoi mon code ne fonctionne pas ?

J'ai mis dans l'exemple la ligne qui pose problème.
Code:
Range("V20").Value = Application.Index(Sheets("Clients").Range("B2:G5"), Application.Match(Sheets("Pilote").Range("y4"), Sheets("Clients").Range("B2:B5"), 0), 1)

Bouton en feuille Pilote

Merci.
 

Pièces jointes

  • Index Match.xlsm
    20 KB · Affichages: 43

Modeste geedee

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonsour®
proc modifiée :
VB:
Sub Test()

ville = Sheets("Clients").Range("g2:g50")
nom = Sheets("Clients").Range("b2:b50")
Table = Sheets("Clients").Range("A2:K50")
' ----- ces plages gagneraient à etre nommées dynamiquement

With Sheets("Pilote")
        ligne = Application.Match(.Range("y4"), ville, 0)
        Range("U20").Value = Application.Index(Table, ligne, 1)
        Range("V20").Value = Application.Index(Table, ligne, 2)
        Range("W20").Value = Application.Index(Table, ligne, 3)
        Range("X20").Value = Application.Index(Table, ligne, 4)
        Range("Y20").Value = Application.Index(Table, ligne, 5)
'----- etc ...
End With
End Sub
 

BOISGONTIER

XLDnaute Barbatruc
Repose en paix
Re : Index.Match #N/A

Bonjour,

Code:
Sub Test()
  Set ville = Sheets("Clients").Range("g2:g5")
  Set nom = Sheets("Clients").Range("b2:b5")
  Set Table = Sheets("Clients").Range("b2:g5")
  p = Application.Match(Sheets("Pilote").Range("y4"), ville, 0)
  If Not IsError(p) Then Sheets("pilote").Range("V20").Value = Application.Index(Table, p, 1)
End Sub

JB
 
Dernière édition:

Calvus

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonjour,

@Dranreb, Modeste Geedee a parfaitement répondu à ma demande. J'avais dû mal m'exprimer.

@ Modeste Geedee, merci, génial ! Et ça paraît simple comme ça...pfff, du moins évident.

Je vais adapter, et tenter de poursuivre, car comme tu dois t'en douter, j'aimerais remplir le tableau. Je vais donc tenter de faire une boucle.
Je reviendrai certainement... :confused:

Bonne journée.

EDIT : Bonjour JB, merci.
 

Calvus

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonsoir,

J'ai fait ma boucle, mais évidemment ça ne fonctionne pas. Du moins partiellement.
J'ai réussi à faire une boucle (OUAOU !!:cool:), mais je n'arrive pas à exprimer clairement ma condition.

Celle-ci est que l'on a trouvé la valeur correspondant au nom "Tende" dans le tableau "table" de la feuille clients, on affiche toutes les valeurs correspondantes.
Code:
Sub Test()

ville = Sheets("Clients").Range("g2:g50")
nom = Sheets("Clients").Range("b2:b50")
Table = Sheets("Clients").Range("A2:g50")



Dim cel As Range
With Sheets("Pilote")
For Each cel In Sheets("Clients").Range("g2:g50")
        ligne = Application.Match(Range("y4"), ville, 0)
        
        result1 = Application.Index(Table, ligne, 1)
        result2 = Application.Index(Table, ligne, 2)
        result3 = Application.Index(Table, ligne, 3)
    'MsgBox (result1)
    For i = 7 To 40

            If cel = Sheets("Pilote").Range("y4") Then
            'MsgBox ("Trouvé!")
            
    Range("u" & i) = result1
    Range("v" & i) = result2
    Range("w" & i) = result3
    End If
    
   Next i

Next cel
End With
Range("U:AE").Cells.Columns.AutoFit
End Sub

Merci de votre aide.
 

Pièces jointes

  • Index Match 2.xlsm
    22.4 KB · Affichages: 29
Dernière édition:

Modeste geedee

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonsour®
l'instruction :
ligne = Application.Match(Range("y4"), ville, 0)
renvoie et reverra toujours le N° de la 1ere ligne trouvée ligne
la boucle n'y fera rien ...
il faut utiliser une fonction Find :
exemple :
VB:
lignepilote =N '------------------(n° de ligne ou l'on doit ecrire
With Worksheets("Clients").Range("G2:G50")
    Set c = .Find(Range("Y4"), lookin:=xlValues)
    If Not c Is Nothing Then
        firstAddress = c.Address
        ligneclient=C.row '----------- N° de ligne client trouvée
                Do
           ' ici les instruction de réécriture dans feuille Pilote
           '......
           '......
         lignepilote=lignepilote+1  ' on incrémente la destination
            Set c = .FindNext(c)
        Loop While Not c Is Nothing And c.Address <> firstAddress
    End If
End With
 

Calvus

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonjour Modeste Geedee :), le Forum,

Merci de ta réponse Modeste.
Je crois avoir compris à peu près, et je dis bien à peu près la logique du code, néanmoins, rien ne se passe dans mon tableau.
' ici les instruction de réécriture dans feuille Pilote
'......
'......
Il doit me manquer des instructions j'imagine.

Par ailleurs, j'ai ajouté des Msgbox pour vérifier le code et j'obtiens :
Msgbox(c) me donne Tende
Msgbox(c.Row) me donne 5 alors que je devrais avoir 3

Code:
lignepilote = N '------------------(n° de ligne ou l'on doit ecrire
With Worksheets("Clients").Range("G2:G50")
    Set C = .Find(Range("Y4"), LookIn:=xlValues)
    MsgBox (C)
    If Not C Is Nothing Then
        firstAddress = C.Address
        ligneclient = C.Row '----------- N° de ligne client trouvée
    MsgBox (C.Row)
               Do
           ' ici les instruction de réécriture dans feuille Pilote
          '......
          '......
        lignepilote = lignepilote + 1 ' on incrémente la destination
           Set C = .FindNext(C)
        Loop While Not C Is Nothing And C.Address <> firstAddress
    End If
End With

Je vais bien finir par trouver, surtout grâce à vous en fait ! :):)

Merci
 

job75

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonjour à tous,

Surtout ne pas utiliser Find, c'est beaucoup trop lent.

Méthode très classique par tableaux VBA, nombreux exemples sur le forum :

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, [Y4]) Is Nothing Then Exit Sub
Dim cible$, col%, ncol%, t, a(), i&, n&, j%
cible = CStr([Y4])
col = 7 'colonne où se fait la recherche de cible
ncol = 11 'nombre de colonnes à copier >= col
t = Sheets("Clients").[A1].CurrentRegion.Resize(, ncol + 1)
ReDim a(1 To UBound(t), 1 To ncol)
For i = 2 To UBound(t)
  If t(i, col) = cible Then
    n = n + 1
    For j = 1 To ncol
      a(n, j) = t(i, j)
    Next j
  End If
Next i
'---restitution et mise en forme---
Application.ScreenUpdating = False
With [U7] '1ère cellule, à adapter
  .Resize(Rows.Count - .Row + 1, ncol).Delete xlUp 'RAZ
End With
If n Then
  With [U7].Resize(n, ncol)
    .Value = a
    .Interior.ColorIndex = 37 'bleu
    .Borders(xlEdgeLeft).Weight = xlMedium
    .Borders(xlEdgeRight).Weight = xlMedium
    .Borders(xlEdgeBottom).Weight = xlMedium
    .Borders(xlInsideVertical).Weight = xlThin
  End With
End If
Columns.AutoFit 'ajustement largeur
End Sub
Fichier joint.

Edit : j'ai mis la colonne Z au format "Code postal", il serait bien de formater aussi les n° de téléphone...

A+
 

Pièces jointes

  • Index Match(1).xlsm
    31 KB · Affichages: 33
Dernière édition:

Calvus

XLDnaute Barbatruc
Re : Index.Match #N/A

Bonsoir Job75, le Forum,

@Job, merci beaucoup, vraiment. Ainsi que pour les explications sur le code, que je comprends maintenant partiellement. Faudra se familiariser avec les cible$, col%, ncol%, t, a(), i&, n&, j% hein ?! :)

Comment puis-je remplacer le [Y4] figurant dans la ligne If Intersect(Target, [Y4]) ?

En effet, dans mon fichier final, je cherche les valeurs d'après des Shapes d'une carte géographique, dont voici la macro, située en feuille 1 de mon classeur :
Code:
Sub Click()
Dim NomShape As String
Application.ScreenUpdating = False
Shapes("Roya").Fill.ForeColor.RGB = RGB(192, 255, 159)
Shapes("Vallee de la Vesubie").Fill.ForeColor.RGB = RGB(162, 236, 130)
Shapes("Vallee de la Tinee").Fill.ForeColor.RGB = RGB(135, 222, 135)
Shapes("Vallee du Var").Fill.ForeColor.RGB = RGB(170, 255, 204)
Shapes("Vallee de l'Esteron").Fill.ForeColor.RGB = RGB(188, 226, 162)
Shapes("Prealpes").Fill.ForeColor.RGB = RGB(195, 232, 146)
Shapes("Pays Grassois").Fill.ForeColor.RGB = RGB(230, 185, 184)
Shapes("Pays Niçois").Fill.ForeColor.RGB = RGB(242, 220, 219)
Shapes("Pays des Paillons").Fill.ForeColor.RGB = RGB(195, 214, 155)
Shapes("Pays Vençois").Fill.ForeColor.RGB = RGB(217, 150, 148)
Shapes("Grand Antibes").Fill.ForeColor.RGB = RGB(179, 162, 199)
Shapes("Grand Menton").Fill.ForeColor.RGB = RGB(243, 255, 159)
Shapes("Grand Beausoleil").Fill.ForeColor.RGB = RGB(255, 217, 159)
Shapes("Nice").Fill.ForeColor.RGB = RGB(204, 193, 218)
Shapes("Villeneuve Loubet").Fill.ForeColor.RGB = RGB(204, 193, 218)
Shapes("Cagnes").Fill.ForeColor.RGB = RGB(179, 162, 199)
Shapes("Grand Cannes").Fill.ForeColor.RGB = RGB(96, 74, 123)
Shapes("Mandelieu La Napoule").Fill.ForeColor.RGB = RGB(144, 224, 188)
Shapes("Valbonne").Fill.ForeColor.RGB = RGB(195, 214, 155)

NomShape = Application.Caller
'For Each Shape In ActiveSheet.Shapes
'Shape.Fill.ForeColor.RGB = RGB(255, 0, 255)
'Next Shape


Shapes(NomShape).Fill.ForeColor.RGB = RGB(255, 0, 0)

Range("U7:AE40").ClearContents
Range("AA44:AA48").ClearContents
Range("Y7") = NomShape
Range("AA7") = Application.Index(Sheets("BD Villes").Range("J2:L178"), Application.Match(NomShape, Sheets("BD Villes").Range("J2:J178"), 0), 3)

If Range("U5") = "Prospect" Then

MEF_Prospect
ElseIf Range("U5") = "Clients" Then
MEF_Clients
Else: MEF_Vide
End If
Range("AA44") = NomShape
TabInf
Application.ScreenUpdating = True
End Sub


MEF_Prospect, MEF_Clients; TabInf sont d'autres macros dans un module permettant la mise en forme des tableaux affichés ainsi que leurs valeurs.
Je pourrai mettre les codes si vous le souhaitez.

Vous l'aurez compris, mon souhait est d'afficher un tableau des références Clients ou Prospects après avoir cliqué sur une ville en particulier de ma carte.

Espérant avoir été assez clair.
Merci
 

job75

XLDnaute Barbatruc
Re : Index.Match #N/A

Re,

J'étudierai plus tard votre post #10.

Avec cette version la recherche se fait dans toutes les colonnes du tableau source :

Code:
Option Compare Text 'pour ignorer la casse

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, [Y4]) Is Nothing Then Exit Sub
Dim cible$, ncol%, t, a(), i&, j%, n&, k%
cible = CStr([Y4])
ncol = 11 'nombre de colonnes à copier
t = Sheets("Clients").[A1].CurrentRegion.Resize(, ncol + 1)
ReDim a(1 To UBound(t), 1 To ncol)
For i = 2 To UBound(t)
  For j = 1 To ncol
    If IsDate(t(i, j)) Then t(i, j) = Format(t(i, j), "dd/mm/yyyy")
    If t(i, j) Like "*" & cible & "*" Then
      n = n + 1
      For k = 1 To ncol
        a(n, k) = t(i, k)
      Next k
      Exit For
    End If
Next j, i
'---restitution et mise en forme---
Application.ScreenUpdating = False
With [U7] '1ère cellule, à adapter
  .Resize(Rows.Count - .Row + 1, ncol).Delete xlUp 'RAZ
End With
If n Then
  With [U7].Resize(n, ncol)
    .Value = a
    .Interior.ColorIndex = 37 'bleu
    .Borders(xlEdgeLeft).Weight = xlMedium
    .Borders(xlEdgeRight).Weight = xlMedium
    .Borders(xlEdgeBottom).Weight = xlMedium
    .Borders(xlInsideVertical).Weight = xlThin
  End With
End If
Columns.AutoFit 'ajustement largeur
End Sub
Nota 1 : ici Option Compare Text permet d'ignorer la casse.

Nota 2 : j'ai fait en sorte que la recherche puisse se faire sur des dates.

Fichier (2).

A+
 

Pièces jointes

  • Index Match(2).xlsm
    31.5 KB · Affichages: 28

job75

XLDnaute Barbatruc
Re : Index.Match #N/A

Re,

J'ai étudié rapidement votre post #10.

1) Toutes vos Shapes ont des noms de villes donc c'est ma version (1) qu'il faut utiliser.

2) Je vois que vous utilisez Range("Y7") = NomShape, avec Range("Y4") = NomShape cela lancera ma macro.

3) Vous ne devez pas utiliser les cellules placées sous le tableau car je les supprime pour la RAZ.

A+
 

Calvus

XLDnaute Barbatruc
Re : Index.Match #N/A

Re,

Merci Job. Quel travail ! :)
Cela ne fonctionne pas chez moi, mais je n'ai pas dû mettre le code là où il faut.
Il ne faut pas m'en vouloir, je tente d'apprendre et de comprendre :)

Voici l'intégralité du code modifié avec votre macro.
La réponse de bug est : objet requis
Code:
Sub Click()
Dim NomShape As String
Application.ScreenUpdating = False
Shapes("Roya").Fill.ForeColor.RGB = RGB(192, 255, 159)
Shapes("Vallee de la Vesubie").Fill.ForeColor.RGB = RGB(162, 236, 130)
Shapes("Vallee de la Tinee").Fill.ForeColor.RGB = RGB(135, 222, 135)
Shapes("Vallee du Var").Fill.ForeColor.RGB = RGB(170, 255, 204)
Shapes("Vallee de l'Esteron").Fill.ForeColor.RGB = RGB(188, 226, 162)
Shapes("Prealpes").Fill.ForeColor.RGB = RGB(195, 232, 146)
Shapes("Pays Grassois").Fill.ForeColor.RGB = RGB(230, 185, 184)
Shapes("Pays Niçois").Fill.ForeColor.RGB = RGB(242, 220, 219)
Shapes("Pays des Paillons").Fill.ForeColor.RGB = RGB(195, 214, 155)
Shapes("Pays Vençois").Fill.ForeColor.RGB = RGB(217, 150, 148)
Shapes("Grand Antibes").Fill.ForeColor.RGB = RGB(179, 162, 199)
Shapes("Grand Menton").Fill.ForeColor.RGB = RGB(243, 255, 159)
Shapes("Grand Beausoleil").Fill.ForeColor.RGB = RGB(255, 217, 159)
Shapes("Nice").Fill.ForeColor.RGB = RGB(204, 193, 218)
Shapes("Villeneuve Loubet").Fill.ForeColor.RGB = RGB(204, 193, 218)
Shapes("Cagnes").Fill.ForeColor.RGB = RGB(179, 162, 199)
Shapes("Grand Cannes").Fill.ForeColor.RGB = RGB(96, 74, 123)
Shapes("Mandelieu La Napoule").Fill.ForeColor.RGB = RGB(144, 224, 188)
Shapes("Valbonne").Fill.ForeColor.RGB = RGB(195, 214, 155)

NomShape = Application.Caller
'For Each Shape In ActiveSheet.Shapes
'Shape.Fill.ForeColor.RGB = RGB(255, 0, 255)
'Next Shape


Shapes(NomShape).Fill.ForeColor.RGB = RGB(255, 0, 0)

Range("U7:AE40").ClearContents
Range("AA44:AA48").ClearContents
Range("Y4") = NomShape
Range("AA7") = Application.Index(Sheets("BD Villes").Range("J2:L178"), Application.Match(NomShape, Sheets("BD Villes").Range("J2:J178"), 0), 3)

If Range("U5") = "Prospect" Then

MEF_Prospect
ElseIf Range("U5") = "Clients" Then
MEF_Clients
Else: MEF_Vide
End If
Range("AA44") = NomShape
TabInf


If Intersect(Target, [Y4]) Is Nothing Then Exit Sub
Dim cible$, col%, ncol%, t, a(), i&, n&, j%
cible = CStr([Y4])
col = 7 'colonne où se fait la recherche de cible
ncol = 11 'nombre de colonnes à copier >= col
t = Sheets("Clients").[A1].CurrentRegion.Resize(, ncol + 1)
ReDim a(1 To UBound(t), 1 To ncol)
For i = 2 To UBound(t)
  If t(i, col) = cible Then
    n = n + 1
    For j = 1 To ncol
      a(n, j) = t(i, j)
    Next j
  End If
Next i
'---restitution et mise en forme---
Application.ScreenUpdating = False
With [U7] '1ère cellule, à adapter
  .Resize(Rows.Count - .Row + 1, ncol).Delete xlUp 'RAZ
End With
If n Then
  With [U7].Resize(n, ncol)
    .Value = a
    .Interior.ColorIndex = 37 'bleu
    .Borders(xlEdgeLeft).Weight = xlMedium
    .Borders(xlEdgeRight).Weight = xlMedium
    .Borders(xlEdgeBottom).Weight = xlMedium
    .Borders(xlInsideVertical).Weight = xlThin
  End With
End If
Columns.AutoFit 'ajustement largeur

Application.ScreenUpdating = True
End Sub

Merci
 

Discussions similaires

Réponses
1
Affichages
1 K

Statistiques des forums

Discussions
312 206
Messages
2 086 208
Membres
103 158
dernier inscrit
laufin