Détecter une cellule

Résolu
nonossov Messages postés 638 Statut Membre -  
nonossov Messages postés 638 Statut Membre -
Bonjour mes amis;
Grâce à votre aide mes chers amis, je recherche une macro qui peut détecter le numéro de cellule dans un fichier excel, par deux critères, le premier dans une colonne et le deuxième dans une ligne, par exemple:
feuil 1:
colonne B j'ai: B5: Paris
Colonne A j'ai: A5: France

je dois chercher ces deux critères dans le fichier excel et aller à cette cellule.

3 réponses

  1. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 588
     
    bonsoir, peux-tu décrire ton exemple jusqu'au bout?
    2
    1. nonossov Messages postés 638 Statut Membre
       
      Voilà j'ai joint un fichier qui presente un exemple:
      dans le feuil 1: j'ai ces données:

      Colonne A/B
      Paris Rue 1
      Lyon Rue 2
      London Rue 3
      Tanger Rue 4
      Berlin Rue 5
      cette macro doit détecter la cellule qui correspond à chacun aux deux données, comme il est coloré dans le feuil 2:

      https://www.cjoint.com/c/GCBolw3IELV

      Merci
      0
    2. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > nonossov Messages postés 638 Statut Membre
       
      et que va faire la macro avec cette cellule?
      0
    3. nonossov Messages postés 638 Statut Membre
       
      la macro va juste detecter la macro s'il est possible de la coloreren bleu.
      0
    4. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > nonossov Messages postés 638 Statut Membre
       
      as-tu de l'expérience en VBA?
      0
    5. nonossov Messages postés 638 Statut Membre
       
      Non pas de tout, je suis loin de VBA, est je l'utilise bcp.
      0
  2. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 588
     
    avec quelques commentaires:
    Option Explicit
    
    Sub nonossov_color()
    
    Dim critere As Range, matrice As Range
    Set critere = [Feuil1!A2]   ' position de la première cellule où se trouvent les critères
    Set matrice = [Feuil2!A1]   ' position de la cellule en haut à gauche de la table à colorier
    Dim ncol As Long, icol As Long
    Dim nlig As Long, fcol As Long
    Dim ws As Worksheet
    Dim col As Scripting.Dictionary
    Set col = New Scripting.Dictionary
    Dim lig As Scripting.Dictionary
    Set lig = New Scripting.Dictionary
    Dim horval() As Long
    Dim numcol As Long
    Dim verval As Long
    
    ' parcourons et mémorisons les critères de recherche
    ncol = critere.Column
    nlig = critere.Row
    Set ws = critere.Worksheet
    Do While ws.Cells(nlig, ncol) <> ""
        col.Add ws.Cells(nlig, ncol).Value, nlig
        lig.Add ws.Cells(nlig, ncol + 1).Value, nlig
        nlig = nlig + 1
    Loop
    
    ' parcourons les titres de colonnes, et mémorisons leur positions dans les critères de recherche
    ncol = matrice.Column
    nlig = matrice.Row
    numcol = 0
    Set ws = matrice.Worksheet
    Do While ws.Cells(nlig, ncol + 1) <> ""
        ncol = ncol + 1
        numcol = numcol + 1
        ReDim Preserve horval(1 To numcol)
        If lig.Exists(ws.Cells(nlig, ncol).Value) Then
            horval(numcol) = lig(ws.Cells(nlig, ncol).Value)
        Else
            horval(numcol) = 0
        End If
    Loop
    
    ' parcourons la table à colorier
    nlig = matrice.Row + 1
    fcol = matrice.Column
    ' parcourons chaque ligne
    Do While ws.Cells(nlig, fcol) <> ""
        If col.Exists(ws.Cells(nlig, fcol).Value) Then
            ' le titre de ligne est à rechercher
            verval = col(ws.Cells(nlig, fcol).Value)
            ' parcourons chaque colonne de cette ligne
            For icol = 1 To numcol
                If verval = horval(icol) Then
                    ' le titre de colonne et le titre de ligne forment un critère de recherche
                    ' colorions la cellule
                    ws.Cells(nlig, fcol + icol).Interior.ColorIndex = 37
                End If
            Next icol
        End If
        nlig = nlig + 1
    Loop
        
    End Sub
    
    2
    1. nonossov Messages postés 638 Statut Membre
       
      Merci infiniment,
      0
  3. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   Ambassadeur 1 588
     
    je propose ceci:
    Option Explicit
    
    Sub nonossov_color()
    
    Dim critere As Range, matrice As Range
    Set critere = [Feuil1!A3]
    Set matrice = [Feuil2!A1]
    Dim ncol As Long, icol As Long
    Dim nlig As Long, fcol As Long
    Dim ws As Worksheet
    Dim col As Scripting.Dictionary
    Set col = New Scripting.Dictionary
    Dim lig As Scripting.Dictionary
    Set lig = New Scripting.Dictionary
    Dim horval() As Long
    Dim numcol As Long
    Dim verval As Long
    
    ncol = critere.Column
    nlig = critere.Row
    Set ws = critere.Worksheet
    Do While ws.Cells(nlig, ncol) <> ""
        col.Add ws.Cells(nlig, ncol).Value, nlig
        lig.Add ws.Cells(nlig, ncol + 1).Value, nlig
        nlig = nlig + 1
    Loop
    ncol = matrice.Column
    nlig = matrice.Row
    numcol = 0
    Set ws = matrice.Worksheet
    Do While ws.Cells(nlig, ncol + 1) <> ""
        ncol = ncol + 1
        numcol = numcol + 1
        ReDim Preserve horval(1 To numcol)
        If lig.Exists(ws.Cells(nlig, ncol).Value) Then
            horval(numcol) = lig(ws.Cells(nlig, ncol).Value)
        Else
            horval(numcol) = 0
        End If
    Loop
    nlig = matrice.Row + 1
    fcol = matrice.Column
    Do While ws.Cells(nlig, fcol) <> ""
        If col.Exists(ws.Cells(nlig, fcol).Value) Then
            verval = col(ws.Cells(nlig, fcol).Value)
            For icol = 1 To numcol
                If verval = horval(icol) Then
                    ws.Cells(nlig, fcol + icol).Interior.ColorIndex = 37
                End If
            Next icol
            nlig = nlig + 1
        End If
    Loop
        
    End Sub
    

    tiens-moi au courant
    1
    1. nonossov Messages postés 638 Statut Membre
       
      Merci infiniment, mais je recois ce msg: Erreur de compilation:
      Type défini par l'utilisateur non défini
      col As Scripting.Dictionary
      Sub nonossov_color() "coloré en jaune."
      0
    2. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > nonossov Messages postés 638 Statut Membre
       
      ah oui, j'ai oublié de te prévenir : tu dois ajouter "microsoft scripting runtime" dans les références (menu outils/references).
      0
    3. nonossov Messages postés 638 Statut Membre
       
      Meric, j'ai une question: Comment je peux l'exécuter? je dois choisir les cellules ou comment?
      0
    4. nonossov Messages postés 638 Statut Membre
       
      j'ai exécuté la macro est le fichier excel est bloqué? comment puis-je l'exécuté?
      0
    5. yg_be Messages postés 23437 Date d'inscription   Statut Contributeur Dernière intervention   1 588 > nonossov Messages postés 638 Statut Membre
       
      pour tester, tu peux changer les lignes suivantes:
      Set critere = [Feuil1!A3]
      Set matrice = [Feuil2!A1]

      Elles contrôlent la première cellule contenant un critère, et la cellule en haut à gauche de la zone à examiner.
      0