IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Macros et VBA Excel Discussion :

Boucles Do Loop successives


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut Boucles Do Loop successives
    Bonjour,

    Ma feuille de calculs contient de A1 à A200 une liste de noms et je cherche à ce que mon code sélectionne la dernière cellule dans laquelle le nom que je rentre dans une Input Box se trouve.
    Ces noms peuvent, ou non, se répéter plusieurs fois.

    Pour l'instant, j'utilise un code qui fonctionne très bien pour trouver la première cellule dans laquelle le nom se trouve et la dérive pour la deuxième occurrence mais il me semble fastidieux de poursuivre ainsi avec un code qui pourrait aller jusqu'à une douzaine d'occurrences.

    Le code:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
     
    If Target.Address = "$A$1" Then
     
     
      Dim Lig As Long, Nom As String, Line As Long, Col As Long, LignSel As Long
     On Error GoTo errorHandler2
    Nom = InputBox("Saisie de votre NOM : ", "NOM")
    If Nom = "" Then
    Exit Sub
    Else
    errorHandler2:
    Dim cel As Range
        If Application.WorksheetFunction.CountIf(Sheets("RECUP").Range("A2:A" & Sheets("RECUP").Range("A65536").End(xlUp).Row), Nom) = 1 Then
    MsgBox "OK"
     
    Do
        Set cel = Sheets("RECUP").Range("A2:A200").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
     
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
     
       Exit Sub
        Loop While Not cel Is Nothing
     
    End If
     
     
     
        End If
        If Application.WorksheetFunction.CountIf(Sheets("RECUP").Range("A2:A" & Sheets("RECUP").Range("A65536").End(xlUp).Row), Nom) = 2 Then
     
     
     
      Do
        Set cel = Sheets("RECUP").Range("A2:A200").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
     
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
     
     
     
     
     
        Set cel = Sheets("RECUP").Range("" & "A" & LignSel + 1 & ":A200" & "").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
     
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
     
        Exit Sub
        Loop While Not cel Is Nothing
     
      End If
     
     
    End If
     
     End Sub

    Pour synthétiser, la feuille de calculs:

    A1 : Michel
    A2: Patrick
    A3: Robert
    .
    .
    .

    A42: Robert
    .
    .
    .
    A105: Robert
    .
    .
    .

    Mon code, pour l'instant, fonction très bien pour sélectionner la cellule où se trouve Michel ou Patrick ainsi que Robert en A3 et A42 mais, pour le Robert en A105, il me faudrait encore répéter un:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    Set cel = Sheets("RECUP").Range("" & "A" & LignSel + 1 & ":A200" & "").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
        'MsgBox cel
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
    A l'intérieur de:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    Do
        Set cel = Sheets("RECUP").Range("A2:A200").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
     
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
     
     
     
     
        Set cel = Sheets("RECUP").Range("" & "A" & LignSel + 1 & ":A200" & "").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
     
        If Not cel Is Nothing Then cel.Activate
        LignSel = ActiveCell.Row
     
        Exit Sub
        Loop While Not cel Is Nothing
     
      End If
    Pour:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
        If Application.WorksheetFunction.CountIf(Sheets("RECUP").Range("A2:A" & Sheets("RECUP").Range("A65536").End(xlUp).Row), Nom) = 3 Then
    Et ainsi de suite jusqu'à une bonne douzaine de fois.

    Quelqu'un pourrait il m'apporter une aide précieuse?

    MERCI D'AVANCE

  2. #2
    Membre chevronné
    Homme Profil pro
    autodidacte
    Inscrit en
    Novembre 2013
    Messages
    517
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Rhône (Rhône Alpes)

    Informations professionnelles :
    Activité : autodidacte
    Secteur : Industrie

    Informations forums :
    Inscription : Novembre 2013
    Messages : 517
    Par défaut
    bonjour,

    ceci devrait pouvoir vous aider:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    Sub test()
    With Worksheets(1).Range("a1:a500")
        Set c = .Find("Pierre", LookIn:=xlValues)
        If Not c Is Nothing Then
            Do
            MsgBox c.Address
            Set c = .FindNext(c)
            Loop While Not c Is Nothing 
        End If
    End With
    End Sub

  3. #3
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut
    J'ai essayé d'adapter pour mon cas:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$1" Then
     
     
      Dim Lig As Long, Nom As String, Line As Long, Col As Long, LignSel As Long
     
    Nom = InputBox("Saisie de votre NOM : ", "NOM")
    If Nom = "" Then
    Exit Sub
    Else
    MsgBox "OK"
    With Sheets("RECUP").Range("a1:a500")
        Set C = .Find("Nom", LookIn:=xlValues)
        If Not C Is Nothing Then
            Do
            MsgBox C.Address
            Set C = .FindNext(C)
            Loop While Not C Is Nothing
        End If
    End With
    End If
    End If
    End Sub
    J'ai bien ma MsgBox "OK" qui apparaît, donc les conditions sont bien détectées mais ensuite, rien ne se passe....????

    Je viens de fermer rouvrir le classeur, en fait la MsgBox renvoie "$A$1" en boucle

    Avec:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$1" Then
     
     
      Dim Lig As Long, Nom As String, Line As Long, Col As Long, LignSel As Long
     
    Nom = InputBox("Saisie de votre NOM : ", "NOM")
    If Nom = "" Then
    Exit Sub
    Else
     
    With Sheets("RECUP").Range("a1:a500")
     
    'MsgBox Nom
        Set C = Sheets("RECUP").Range("A2:A200").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
        'MsgBox C
        If Not C Is Nothing Then
            Do
            MsgBox C.Address
            Set C = .FindNext(C)
            Loop While Not C Is Nothing
        End If
    End With
    End If
    End If
    End Sub
    La MsgBox me renvoie bien les adresses des cellules dans lesquelles sont le nom demandé, nous y sommes donc presque mais le code n'arrive pas à sortir de la boucle et n'active donc aucune cellule....

  4. #4
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut
    Très bon:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Target.Address = "$A$1" Then
     
     
      Dim Lig As Long, Nom As String, Line As Long, Col As Long, LignSel As Long
     
    Nom = InputBox("Saisie de votre NOM : ", "NOM")
    If Nom = "" Then
    Exit Sub
    Else
     
    With Sheets("RECUP").Range("a1:a500")
     
    'MsgBox Nom
        Set C = Sheets("RECUP").Range("A2:A200").Find(What:="" & Nom & "", LookAt:=xlWhole, SearchOrder:=xlByColumns)
        'MsgBox C
        If Not C Is Nothing Then
            Do
            'MsgBox C.Address
            Set C = .FindNext(C)
            C.Activate
            Exit Sub
     
            Loop While Not C Is Nothing
     
        End If
    End With
    End If
    End If
    End Sub
    mais pour l'instant, le cas de plus de 2 occurrences ne se présente pas....

    RESOLU en attedant

    MERCI!!!

  5. #5
    Membre Expert
    Avatar de Igloobel
    Homme Profil pro
    Développeur ERP - VBA et Formateur bureautique
    Inscrit en
    Septembre 2005
    Messages
    1 871
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Loiret (Centre)

    Informations professionnelles :
    Activité : Développeur ERP - VBA et Formateur bureautique
    Secteur : Industrie

    Informations forums :
    Inscription : Septembre 2005
    Messages : 1 871
    Billets dans le blog
    1
    Par défaut
    Bonjour,

    Bon je sais que c'est résolu mais je poste qd même une autre solution

    Bonjour,

    Ma feuille de calculs contient de A1 à A200 une liste de noms et je cherche à ce que mon code sélectionne la dernière cellule dans laquelle le nom que je rentre dans une Input Box se trouve.
    Donc on part de la fin !!!

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    Sub test()
        Dim NomF As String
        Dim Nom As String
        Dim Derlign As Long
        Dim Lign As Long
     
        NomF = "Feuil1"
        Nom = InputBox("Saisie de votre NOM : ", "NOM")
     
        Derlign = ThisWorkbook.Worksheets(NomF).UsedRange.Rows.Count
     
        For Lign = Derlign To 1 Step -1
            If Cells(Lign, 1).Value = Nom Then
                Exit For
            End If
        Next
     
        Rows(Lign).Select
     
    End Sub
    c'est a mon avis extrêmement simple

    A+

  6. #6
    Membre éprouvé
    Homme Profil pro
    Inscrit en
    Mai 2012
    Messages
    1 150
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 150
    Par défaut
    Trop de solutions vaut mieux qu'aucune!!!!!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Problème avec une boucle Do.. Loop Until
    Par gta126 dans le forum VB 6 et antérieur
    Réponses: 5
    Dernier message: 29/12/2007, 11h27
  2. terminer une boucle Do Loop
    Par svedberg dans le forum Macros et VBA Excel
    Réponses: 8
    Dernier message: 15/10/2007, 03h17
  3. Boucle Do Loop d'attente et Utilisation de l'UC
    Par ProgElecT dans le forum VB 6 et antérieur
    Réponses: 6
    Dernier message: 28/05/2007, 10h16
  4. boucle while loop trop lente et pennible
    Par jm_force dans le forum Access
    Réponses: 3
    Dernier message: 10/08/2006, 17h36
  5. [VBA-E] PRobleme avec une boucle DO..LOOP WHILE
    Par AliochaBada dans le forum Macros et VBA Excel
    Réponses: 11
    Dernier message: 31/07/2006, 01h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo