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 :

VBA - Fonction recherche sans les cellules Vides


Sujet :

Macros et VBA Excel

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Février 2020
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Distribution

    Informations forums :
    Inscription : Février 2020
    Messages : 25
    Par défaut VBA - Fonction recherche sans les cellules Vides
    Bonjour à tous,

    Je viens vers vous car je bute sur une formule en VBA.

    Je m'explique:
    J'ai un onglet avec une base données qui s'appelle "BasedeDonnées".
    Sur celle ci, divers informations , exemple Row 1 Nom, Row 2 Prenom, ect….

    J'ai un autre onglet, qui s'appelle "Casiersafermer", qui va chercher via un bouton et du code VBA derrière, les informations dans la base données.
    Pour être clair, voici la composition des cellules de l'onglet "Casierafermer"
    Cellule F15, une réf posée manuellement (exp, FM1, FM2, FM3, ect….)
    Cellule G15, le nom inséré automatiquement via mon bouton
    Cellule H15, le prénom inséré automatiquement via mon bouton
    Cellule I15, le Service de la personne inséré automatiquement via mon bouton
    Cellule J15, Le poste occupé de la personne inséré automatiquement via mon bouton

    En cliquant sur mon bouton, mon code regarde la cellule F15 et insère les informations sur la même ligne via ma base de donnée.

    Jusque là mon code fonctionne correctement pas de souci.

    Mais, sinon se ne serait pas drôle…. , le code vérifie également en plus de la cellule F15, la cellule F16, F17, F18, ect… jusqu'à F34

    Lorsque je n'ai qu'une ou deux ref d'inscrit en F15 et F16, par exemple, il met un temps monstrueux car les autres cellules F17, F18, ect sont vide….

    Je voudrait lui ajouter une fonction du style, si la cellule F15, F16, F17, ect jusqu'à F34 est vide, tu passes à l'étape suivante.
    Ce qui lui permettrais de ne pas passer une plombe.

    Voici mon code VBA:

    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
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    Sub miseajourcasiers()
    Dim F1 As Worksheet
    Dim F2 As Worksheet
    Set F1 = Sheets("Casiersafermer")
    Set F2 = Sheets("BasedeDonnées")
    Dim plage As Range
    Set plage = F2.Range("Q3:Q800")
    codrecherché = F1.Range("F15").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G15") = F2.Cells(cell.Row, 1)
        F1.Range("H15") = F2.Cells(cell.Row, 2)
        F1.Range("I15") = F2.Cells(cell.Row, 9)
        F1.Range("J15") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F16").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G16") = F2.Cells(cell.Row, 1)
        F1.Range("H16") = F2.Cells(cell.Row, 2)
        F1.Range("I16") = F2.Cells(cell.Row, 9)
        F1.Range("J16") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F17").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G17") = F2.Cells(cell.Row, 1)
        F1.Range("H17") = F2.Cells(cell.Row, 2)
        F1.Range("I17") = F2.Cells(cell.Row, 9)
        F1.Range("J17") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F18").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G18") = F2.Cells(cell.Row, 1)
        F1.Range("H18") = F2.Cells(cell.Row, 2)
        F1.Range("I18") = F2.Cells(cell.Row, 9)
        F1.Range("J18") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F19").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G19") = F2.Cells(cell.Row, 1)
        F1.Range("H19") = F2.Cells(cell.Row, 2)
        F1.Range("I19") = F2.Cells(cell.Row, 9)
        F1.Range("J19") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F20").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G20") = F2.Cells(cell.Row, 1)
        F1.Range("H20") = F2.Cells(cell.Row, 2)
        F1.Range("I20") = F2.Cells(cell.Row, 9)
        F1.Range("J20") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F21").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G21") = F2.Cells(cell.Row, 1)
        F1.Range("H21") = F2.Cells(cell.Row, 2)
        F1.Range("I21") = F2.Cells(cell.Row, 9)
        F1.Range("J21") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F22").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G22") = F2.Cells(cell.Row, 1)
        F1.Range("H22") = F2.Cells(cell.Row, 2)
        F1.Range("I22") = F2.Cells(cell.Row, 9)
        F1.Range("J22") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F23").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G23") = F2.Cells(cell.Row, 1)
        F1.Range("H23") = F2.Cells(cell.Row, 2)
        F1.Range("I23") = F2.Cells(cell.Row, 9)
        F1.Range("J23") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F24").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G24") = F2.Cells(cell.Row, 1)
        F1.Range("H24") = F2.Cells(cell.Row, 2)
        F1.Range("I24") = F2.Cells(cell.Row, 9)
        F1.Range("J24") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F25").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G25") = F2.Cells(cell.Row, 1)
        F1.Range("H25") = F2.Cells(cell.Row, 2)
        F1.Range("I25") = F2.Cells(cell.Row, 9)
        F1.Range("J25") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F26").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G26") = F2.Cells(cell.Row, 1)
        F1.Range("H26") = F2.Cells(cell.Row, 2)
        F1.Range("I26") = F2.Cells(cell.Row, 9)
        F1.Range("J26") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F27").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G27") = F2.Cells(cell.Row, 1)
        F1.Range("H27") = F2.Cells(cell.Row, 2)
        F1.Range("I27") = F2.Cells(cell.Row, 9)
        F1.Range("J27") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F28").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G28") = F2.Cells(cell.Row, 1)
        F1.Range("H28") = F2.Cells(cell.Row, 2)
        F1.Range("I28") = F2.Cells(cell.Row, 9)
        F1.Range("J28") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F29").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G29") = F2.Cells(cell.Row, 1)
        F1.Range("H29") = F2.Cells(cell.Row, 2)
        F1.Range("I29") = F2.Cells(cell.Row, 9)
        F1.Range("J29") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F30").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G30") = F2.Cells(cell.Row, 1)
        F1.Range("H30") = F2.Cells(cell.Row, 2)
        F1.Range("I30") = F2.Cells(cell.Row, 9)
        F1.Range("J30") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F31").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G31") = F2.Cells(cell.Row, 1)
        F1.Range("H31") = F2.Cells(cell.Row, 2)
        F1.Range("I31") = F2.Cells(cell.Row, 9)
        F1.Range("J31") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F32").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G32") = F2.Cells(cell.Row, 1)
        F1.Range("H32") = F2.Cells(cell.Row, 2)
        F1.Range("I32") = F2.Cells(cell.Row, 9)
        F1.Range("J32") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F33").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G33") = F2.Cells(cell.Row, 1)
        F1.Range("H33") = F2.Cells(cell.Row, 2)
        F1.Range("I33") = F2.Cells(cell.Row, 9)
        F1.Range("J33") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    codrecherché = F1.Range("F34").Value
    Application.ScreenUpdating = False
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G34") = F2.Cells(cell.Row, 1)
        F1.Range("H34") = F2.Cells(cell.Row, 2)
        F1.Range("I34") = F2.Cells(cell.Row, 9)
        F1.Range("J34") = F2.Cells(cell.Row, 10)
     End If
     Next cell
        Application.ScreenUpdating = True
    End Sub

    Voilà, j'espère avoir été assez clair dans mon explication.

    En vous remerciant d'avance.

    Cordialement.

  2. #2
    Expert confirmé
    Homme Profil pro
    Responsable des études
    Inscrit en
    Juillet 2014
    Messages
    2 681
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France, Aude (Languedoc Roussillon)

    Informations professionnelles :
    Activité : Responsable des études
    Secteur : Santé

    Informations forums :
    Inscription : Juillet 2014
    Messages : 2 681
    Par défaut
    Bonjour,

    N'as-tu pas l'impression que tu répète x fois le même code ? pourquoi ne pas faire une boucle de plus ?

    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
    Sub miseajourcasiers()
    Dim F1 As Worksheet
    Dim F2 As Worksheet
    Set F1 = Sheets("Casiersafermer")
    Set F2 = Sheets("BasedeDonnées")
    Dim plage As Range
    Set plage = F2.Range("Q3:Q800")
    for i = 15 to 34
    codrecherché = F1.Range("F" & i).Value
    Application.ScreenUpdating = False
    if codrecherché <>"" then  'ici on vérifie que la valeur n'est pas vide
    For Each cell In plage
     If cell.Value = codrecherché Then
        F1.Range("G" &i) = F2.Cells(cell.Row, 1)
        F1.Range("H" &i) = F2.Cells(cell.Row, 2)
        F1.Range("I" &i) = F2.Cells(cell.Row, 9)
        F1.Range("J" &i) = F2.Cells(cell.Row, 10)
     End If
     Next cell
    end if
    next i
    ps: penses a utiliser les balises codes, ça rend ton message bien plus lisible

  3. #3
    Membre averti
    Homme Profil pro
    Responsable de service informatique
    Inscrit en
    Février 2020
    Messages
    25
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 47
    Localisation : France, Landes (Aquitaine)

    Informations professionnelles :
    Activité : Responsable de service informatique
    Secteur : Distribution

    Informations forums :
    Inscription : Février 2020
    Messages : 25
    Par défaut Royal !!! ;)
    Merci énormément !

    Merci pour la rectification de mon code, c'est excellent !!!
    Plus aucune attente du à plusieurs boucles inutiles de ma part ……

    Je te souhaite une bonne après midi

  4. #4
    Rédacteur/Modérateur


    Homme Profil pro
    Formateur et développeur chez EXCELLEZ.net
    Inscrit en
    Novembre 2003
    Messages
    19 125
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 58
    Localisation : Belgique

    Informations professionnelles :
    Activité : Formateur et développeur chez EXCELLEZ.net
    Secteur : Enseignement

    Informations forums :
    Inscription : Novembre 2003
    Messages : 19 125
    Billets dans le blog
    131
    Par défaut
    Salut.

    Je pose ça comme ça... Pourquoi ne pas récupérer les données par fomules (INDEX-EQUIV ou autres). Vu tes explications, cela me semblerait possible.
    "Plus les hommes seront éclairés, plus ils seront libres" (Voltaire)
    ---------------
    Mes billets de blog sur DVP
    Mes remarques et critiques sont purement techniques. Ne les prenez jamais pour des attaques personnelles...
    Pensez à utiliser les tableaux structurés. Ils vous simplifieront la vie, tant en Excel qu'en VBA ==> mon tuto
    Le VBA ne palliera jamais une mauvaise conception de classeur ou un manque de connaissances des outils natifs d'Excel...
    Ce ne sont pas des bonnes pratiques parce que ce sont les miennes, ce sont les miennes parce que ce sont des bonnes pratiques
    VBA pour Excel? Pensez D'ABORD en EXCEL avant de penser en VBA...
    ---------------

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

Discussions similaires

  1. [XL-2013] VBA : ne pas compter les cellules vides
    Par charlieda dans le forum Macros et VBA Excel
    Réponses: 14
    Dernier message: 22/12/2019, 14h29
  2. [XL-2007] Importer une plage sans les cellules vides
    Par JClmnop dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 12/02/2019, 18h13
  3. [XL-2010] Double de colonne sans les cellules vides
    Par jean780000 dans le forum Excel
    Réponses: 5
    Dernier message: 15/04/2015, 09h26
  4. [XL-2007] Copier/coller du texte sans les cellules vides et avec condition
    Par Titia974 dans le forum Macros et VBA Excel
    Réponses: 0
    Dernier message: 12/03/2014, 12h03
  5. Réponses: 1
    Dernier message: 22/12/2008, 17h15

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