Sélectionner toutes les cellules de différentes valeurs

Bonjour,

Je viens de terminer le cours de VBA et j'essaie de faire une macro mais sans succès.

Tous les mois je dois faire un reporting pour la société dans laquelle je travaille. Je cherche à automatiser les tâches répétitives. Parmi celles-ci j'aimerais sélectionner toutes les cellules d'un tableau croisé dynamique (produits).

Les valeurs que je cherche à regrouper sont des produits vendus par la société. Je cherche à les regrouper en catégorie de produits.

Initialement j'avais fait :

Sub selection_produits()

With ActiveWorkbook.Sheets(1).Columns("a:a")
    Set a= .Find("Produit A", LookIn:=xlValues)
    Set b = .Find("Produit B", LookIn:=xlValues)

    Range(a,b).Select

End With
End Sub

mais je n'arrive pas à mettre de troisième produit :

Sub selection_produits()

With ActiveWorkbook.Sheets(1).Columns("a:a")
    Set a= .Find("Produit A", LookIn:=xlValues)
    Set b = .Find("Produit B", LookIn:=xlValues)
    Set c = .Find("Produit C", LookIn:=xlValues)

    Range(a,b,c).Select

End With
End Sub

J'ai donc essayé avec une boucle.

Sub Macro4()

With ActiveWorkbook.Sheets(1).Columns("a:a")
    Set a = Application.Union((.Find("Produit A ", LookIn:=xlValues)), (.Find("Produit B", LookIn:=xlValues)), (.Find("Produit C", LookIn:=xlValues))).Select

    a.Select

End With

End Sub

Mais ça bug.

Pourriez-vous m'aider ?

Merci,

Lionel

Bonjour,

Une piste à approfondir :

Sub selection_produits()

    Dim CelProdA As Range
    Dim CelProdB As Range
    Dim CelProdC As Range

    With Worksheets(1).Columns("A:A")

        Set CelProdA = .Find("Produit A", , xlValues, xlWhole)
        Set CelProdB = .Find("Produit B", , xlValues, xlWhole)
        Set CelProdC = .Find("Produit C", , xlValues, xlWhole)

    End With

    'les trois doivent avoir été trouvés !
    If Not CelProdA Is Nothing And Not CelProdB Is Nothing And Not CelProdC Is Nothing Then

        Range(CelProdA.Address & "," & CelProdB.Address & "," & CelProdC.Address).Select

    End If

End Sub

Hervé.

ça marche !!!

Pfiouu je suis encore loin de pouvoir écrire ça tout seul !

Merci,

Lionel

edit

Bonjour,

Concernant mon problème ci-dessous, il faut que je retravaille le code donné par Theze (merci à lui) car le code qu'il donne bloque si un produit de la liste n'est pas présent :

Sub selection_produits()

    Dim CelProdA As Range
    Dim CelProdB As Range
    Dim CelProdC As Range

    With Worksheets(1).Columns("A:A")

        Set CelProdA = .Find("Produit A", , xlValues, xlWhole)
        Set CelProdB = .Find("Produit B", , xlValues, xlWhole)
        Set CelProdC = .Find("Produit C", , xlValues, xlWhole)

    End With

    'les trois doivent avoir été trouvés !
   If Not CelProdA Is Nothing And Not CelProdB Is Nothing And Not CelProdC Is Nothing Then

        Range(CelProdA.Address & "," & CelProdB.Address & "," & CelProdC.Address).Select

    End If

End Sub

J'ai pensé à vérifier les produis présents et à le faire correspondre à une variable.

Sub Selection_test()

    Dim prodA As Range
    Dim prodB As Range
    Dim prodC As Range
    Dim prodD As Range
    Dim selection As Range

    With Worksheets(1).Columns("a:a")
        Set prodA = .Find("Prod1", , xlValues, xlWhole)
        Set prodB = .Find("Prod2", , xlValues, xlWhole)
        Set prodC = .Find("Prod3", , xlValues, xlWhole)
        Set prodD = .Find("Prod4", , xlValues, xlWhole)
    End With

    If Not prodA Is Nothing Then
        Set selection = Range(prodA)
        selection.Select
    End If

    If Not prodB Is Nothing Then
        Set selection = Union(selection, prodB)
        selection.Select
    End If

    If Not prodC Is Nothing Then
        Set selection = Union(selection, prodC)
        selection.Select
    End If

    If Not prodD Is Nothing Then
        Set selection = Union(selection, prodD)
        selection.Select
    End If

End Sub

Malheureusement ça ne fonctionne pas. Est-ce que ma logique est bonne ?

Où est mon erreur ?

Merci,

Lionel

Bonjour,

Comme ceci :

Sub Selection_test()

    Dim prodA As Range
    Dim prodB As Range
    Dim prodC As Range
    Dim prodD As Range
    Dim Cel As Range
    'Dim selection  as Range <-- éviter d'utiliser des noms VBA...

    With Worksheets(1).Columns("A:A")

        Set prodA = .Find("Prod1", , xlValues, xlWhole)
        Set prodB = .Find("Prod2", , xlValues, xlWhole)
        Set prodC = .Find("Prod3", , xlValues, xlWhole)
        Set prodD = .Find("Prod4", , xlValues, xlWhole)

    End With

    If Not prodA Is Nothing Then Set Cel = prodA

    If Not prodB Is Nothing Then Set Cel = Union(Cel, prodB)

    If Not prodC Is Nothing Then Set Cel = Union(Cel, prodC)

    If Not prodD Is Nothing Then Set Cel = Union(Cel, prodD)

    If Not Cel Is Nothing Then Cel.Select

End Sub

Hervé.

ça marche !

Merci,

Rechercher des sujets similaires à "selectionner toutes differentes valeurs"