SmartFAQ is developed by The SmartFactory (http://www.smartfactory.ca), a division of InBox Solutions (http://www.inboxsolutions.net)

[VBA] Comment mettre en couleur un mot dans une phrase, sur une cellule ou une plage de cellules ?

Q&R publiée par MyDearFriend! le 16-03-2008 (8808 Lectures)

Voici une fonction permettant d'assurer rapidement ce traitement :

DANS UN MODULE DE CODE STANDARD
Option Explicit
 
Sub MotEnCouleur(LeMot As String, Plage As Range)
'myDearFriend! - www.mdf-xlpages.com
Dim Cel As Range
Dim AdrDeb As String, T As String
Dim Pos As Integer
With Plage
Set Cel = .Find(LeMot, LookAt:=xlPart)
If Not Cel Is Nothing Then
AdrDeb = Cel.Address
Do
T = Cel.Text
Do
Pos = InStr(Pos + 1, T, LeMot)
If Pos > 0 Then
With Cel.Characters(Start:=Pos, Length:=Len(LeMot)).Font
.FontStyle = "Gras"
.ColorIndex = 3 'rouge
End With
End If
Loop Until Pos = 0
Set Cel = .FindNext(Cel)
Loop While Not Cel Is Nothing And AdrDeb <> Cel.Address
End If
End With
End Sub


Et voici comment l'utiliser :

Sub TEST()
'Mettre en rouge et gras le mot "pays" sur la plage de cellules B4:K32
MotEnCouleur "pays", Range("B4:K32")
End Sub

 


  Imprimer la Q&R Envoyer la Q&R