Contains dans VBA

Bonjour!

J'ai une petite probleme, je suis en train d'expérimenter les macros et je voudrais utiliser la function "contient" .

En gros pour mon test j'ai fais ce tableau. Je voudrais que si une case de la colonne D contient "Test", la ligne soit surlignée en couleur.

A_________ B__________ C_________D

Pipe_______ 46_________ 23_________ Att

Elbow_____ 22_________ 22_________ Ok

Tee_________5_________ 3_________ Attente

bl_________ 27_________ 27_________ aaaaaatestaaaaaa

it_________ 1_________ 55_________ bbbbtest2bbbb

ccctest3ccc

J'ai fais ca :

Sub Update_Row_Colors()

Dim LRow As Integer

Dim LCell As String

Dim LColorCells As String

'Start at row 1

LRow = 1

'Update row colors for the first 20 rows

While LRow < 20

LCell = "D" & LRow

'Color will changed in columns A to K

LColorCells = "A" & LRow & ":" & "K" & LRow

Select Case Left(Range(LCell).Value, 7)

'Set row color to light blue

Case "Ok"

Range(LColorCells).Interior.ColorIndex = 34

Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to light blue

Case "Att"

Range(LColorCells).Interior.ColorIndex = 39

Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to light green

Case "Attente"

Rows(LRow & ":" & LRow).Select

Range(LColorCells).Interior.ColorIndex = 35

Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to light yellow

Case InStr("test", vbtext)

Rows(LRow & ":" & LRow).Select

Range(LColorCells).Interior.ColorIndex = 19

Range(LColorCells).Interior.Pattern = xlSolid

'Set row color to light yellow

Dim CharNum As Integer

CharNum = WorksheetFunction.Find("test2", Range("d"))

MsgBox CharNum

Range(LColorCells).Interior.ColorIndex = 19

Range(LColorCells).Interior.Pattern = xlSolid

'Default all other rows to no color

Case Else

Rows(LRow & ":" & LRow).Select

Range(LColorCells).Interior.ColorIndex = xlNone

End Select

LRow = LRow + 1

Wend

Range("A1").Select

End Sub

Ce qui est en rouge ne fonctionne pas. C'est des tests, je ne maitrise pas vraiment .. Voir pas du tout

Merci d'avance !

Ildan

Salut,

Ta macro semble extrêmement compliquée pour le résultat attendu.

A défaut d’avoir ton fichier, j’en ai créé un, avec une démonstration d’une macro toute simple réalisant ton souhait.

Si ça ne te convient pas, fournis-nous ton travail.

Je ne connais pas la ‘’function contient’’.

Cordialement.

75classeur1.zip (10.09 Ko)

Bonjour

Salut Yvouille

Avec toutes les conditions, ça donne :

Sub Update_Row_Colors()
Dim i As Integer, Plage As Range

Columns("A:D").Interior.Pattern = xlNone

For i = 1 To 20
    Set Plage = Range("A" & i & ":D" & i)
    If Range("D" & i) = "OK" Then Plage.Interior.ColorIndex = 34
    If Range("D" & i) = "Att" Then Plage.Interior.ColorIndex = 39
    If Range("D" & i) = "Attente" Then Plage.Interior.ColorIndex = 35
    If Range("D" & i) Like "*test*" Then Plage.Interior.ColorIndex = 19
Next i

Range("A1").Select
End Sub

Edit : Avec Select Case

Sub Update_Row_Colors()
Dim i As Integer
Dim Result As String
Dim Plage As Range

Columns("A:D").Interior.Pattern = xlNone

For i = 1 To 20
Result = Range("D" & i)
Set Plage = Range("A" & i & ":D" & i)
    Select Case True
    Case Result = "OK": Plage.Interior.ColorIndex = 34
    Case Result = "Att": Plage.Interior.ColorIndex = 39
    Case Result = "Attente": Plage.Interior.ColorIndex = 35
    Case Result Like "*test*": Plage.Interior.ColorIndex = 35
    End Select
Next i

End Sub

Amicalement

Nad

Merci à vous pour vos réponses!

Pour la petite histoire, je suis vraiment debutant en VBA.. Je fais des tests avant de voir si je peux m'en servir dans mon travail.. mais en general je google ce que je veux faire, je copie la macro et je l'édite à ma sauce ^^...

Merci encore, le "contain" fonctionne je vais essayer de comprendre maintenant ^^

Rechercher des sujets similaires à "contains vba"