XL 2016 Sélectionner les cellules contenant deux suites de lettres

PETIT YANNICK

XLDnaute Occasionnel
Bonjour,

Je cherche a sélectionner dans mon exemple joint toutes les cellules contenants l'abréviation DET ou MON.
C'est un tableau qui est copier coller par plusieurs utilisateurs et coller dans un autre fichier.
jamais coller au meme endroit en colonne B A D...suivant l'humeur des utilisateurs


Je ne vois pas comment procéder pour toutes les cellules contenants l'abréviation DET ou MON sur un tableau qui change de position.

Quelqu'un aurait une idée?

Merci d'avance
 

Pièces jointes

  • Quote-CNC-BDM2020011405.xls
    41.5 KB · Affichages: 19
Solution
Voici la macro pour sélectionner comme indiqué au post #15 :
VB:
Sub Selectionner()
Dim crit1$, crit2, ncol%, c As Range, sel As Range
crit1 = "DET" 'à adapter
crit2 = "MON" 'à adapter
ncol = 9 'à adapter
Set c = Cells.Find(crit1, , xlValues, xlPart)
If c Is Nothing Then Set c = Cells.Find(crit2)
If c Is Nothing Then Exit Sub
For Each c In Intersect(c.EntireColumn, ActiveSheet.UsedRange)
    If InStr(CStr(c), crit1) + InStr(CStr(c), crit2) Then _
        Set sel = Union(IIf(sel Is Nothing, c.Resize(, ncol), sel), c.Resize(, ncol))
Next
If Not sel Is Nothing Then sel.Select
End Sub
Nota : la casse est respectée.

Et SVP répondez : pourquoi sélectionner ???

job75

XLDnaute Barbatruc
Voici la macro pour sélectionner comme indiqué au post #15 :
VB:
Sub Selectionner()
Dim crit1$, crit2, ncol%, c As Range, sel As Range
crit1 = "DET" 'à adapter
crit2 = "MON" 'à adapter
ncol = 9 'à adapter
Set c = Cells.Find(crit1, , xlValues, xlPart)
If c Is Nothing Then Set c = Cells.Find(crit2)
If c Is Nothing Then Exit Sub
For Each c In Intersect(c.EntireColumn, ActiveSheet.UsedRange)
    If InStr(CStr(c), crit1) + InStr(CStr(c), crit2) Then _
        Set sel = Union(IIf(sel Is Nothing, c.Resize(, ncol), sel), c.Resize(, ncol))
Next
If Not sel Is Nothing Then sel.Select
End Sub
Nota : la casse est respectée.

Et SVP répondez : pourquoi sélectionner ???
 

Dudu2

XLDnaute Barbatruc
Bonjour,
Je suppose que c'est pour copier puis coller.
Tu aurais pu le dire plus tôt que tu voulais sélectionner les 7 colonnes suivantes.
Ça veut dire aussi que la recherche doit être en 1ère colonne uniquement.

Ci-dessous ma version du code (assez semblable à celle de Job) adaptée avec 1 constante à ajuster qui définit nombre de colonnes à sélectionner.

VB:
Sub SelectMONDET()
    Dim Selection As Range
    Dim Cell As Range
    Const NbColonnesSelection = 9   'A adapter selon le nombre de colonnes à sélectionner
 
    For Each Cell In Intersect(ActiveSheet.UsedRange, ActiveSheet.Columns(ActiveSheet.UsedRange.Column))
        If VarType(Cell) = vbString Then
            If Left(Cell.Value, 3) = "MON" _
            Or Left(Cell.Value, 3) = "DET" _
            Then
                If Selection Is Nothing _
                Then Set Selection = Cell.Resize(1, NbColonnesSelection) _
                Else Set Selection = Union(Selection, Cell.Resize(1, NbColonnesSelection))
            End If
        End If
    Next Cell
 
    If Not Selection Is Nothing Then Selection.Select
End Sub
 
Dernière édition:

job75

XLDnaute Barbatruc
Si le but est de définir une plage entre la 1ère valeur trouvée et la dernière de la même colonne :
VB:
Sub DefinirPlage()
Dim crit1$, crit2, ncol%, c As Range, P As Range, tablo, i&, x$, deb As Range, fin As Range, Plage As Range
crit1 = "DET" 'à adapter
crit2 = "MON" 'à adapter
ncol = 9 'à adapter
Set c = Cells.Find(crit1, , xlValues, xlPart)
If c Is Nothing Then Set c = Cells.Find(crit2)
If c Is Nothing Then Exit Sub
Set P = Intersect(c.EntireColumn, ActiveSheet.UsedRange)
tablo = P.Resize(P.Rows.Count + 1) 'matrice, plus rapide, au moins 2 éléments
For i = 1 To UBound(tablo) - 1
    x = CStr(tablo(i, 1))
    If InStr(x, crit1) + InStr(x, crit2) Then Set deb = P(i): Exit For
Next
If deb Is Nothing Then Exit Sub
For i = UBound(tablo) - 1 To 1 Step -1
    x = CStr(tablo(i, 1))
    If InStr(x, crit1) + InStr(x, crit2) Then Set fin = P(i): Exit For
Next
Set Plage = Range(deb, fin).Resize(, ncol)
'---suite du code en utilisant Plage---
Plage.Select 'mais en VBA il est recommandé de ne pas sélectionner...
End Sub
Comme tout à l'heure la casse est respectée.
 

Discussions similaires

Réponses
4
Affichages
361

Statistiques des forums

Discussions
312 305
Messages
2 087 088
Membres
103 461
dernier inscrit
dams94