Sub ReplaceAndBold()
Dim RepQuestionToutesFeuilles As String
Dim QuestionToutesFeuilles As String
QuestionToutesFeuilles = "Voulez-vous excuter la macro sur toutes les feuilles ?"
RepQuestionToutesFeuilles = MsgBox(QuestionToutesFeuilles, vbQuestion + vbYesNo, "Toutes les feuilles ?")
If RepQuestionToutesFeuilles = vbYes Then
Dim Ws As Worksheet
motaremplacer = Trim(InputBox("Entrer le mot à remplacer"))
mot = Trim(InputBox("Entrer le mot de remplacement"))
Dim RepQuestionGras As String
Dim QuestionGras As String
QuestionGras = "Voulez-vous mettre en gras ?"
RepQuestionGras = MsgBox(QuestionGras, vbQuestion + vbYesNo, "Bold")
For Each Ws In Worksheets
Ws.Select
Set oRegExp = CreateObject("vbscript.regexp")
With oRegExp
.Global = True
.ignorecase = False
For Each c In Range("A1:Z" & Range("A" & Rows.Count).End(xlUp).Row)
.Pattern = "(\s|^)(" & motaremplacer & ")(\s|$)"
c.Font.Bold = False
chaine = c.Value
If .test(chaine) = True Then
Set matches = .Execute(chaine)
c.Value = .Replace(chaine, "$1" & mot & "$3")
chaine = c.Value
.Pattern = "(\s|^)" & mot & "(\s|$)"
If RepQuestionGras = vbYes Then
Set matches = .Execute(chaine)
For i = 0 To matches.Count - 1
c.Characters(Start:=matches.Item(i).firstIndex + 1, _
Length:=matches.Item(i).Length).Font.Bold = True
Next i
End If
End If
Next c
End With
Next Ws
If RepQuestionGras = vbNo Then
MsgBox (Chr(34) & motaremplacer & Chr(34) & " a été remplacé par " & Chr(34) & mot & Chr(34) & " sur toutes les feuilles"), vbInformation
Else
MsgBox (Chr(34) & motaremplacer & Chr(34) & " a été remplacé par " & Chr(34) & mot & Chr(34) & " (en gras) sur toutes les feuilles"), vbInformation
End If
Else
Dim xl As Excel.Application
Dim wbk As Excel.Workbook
Do
For k = 0 To 9
Dim QuestionNomFeuille As String
QuestionNomFeuille = Trim(InputBox("Saisir le nom de la feuille sur laquelle vous voulez exécuter la macro ?"))
If FeuilleExiste(QuestionNomFeuille) Then
Sheets(QuestionNomFeuille).Select
Dim RepQuestion4 As String
Dim Question4 As String
Question4 = "Valider ?"
RepQuestion4 = MsgBox(Question4, vbQuestion + vbYesNo, "Validation")
If RepQuestion4 = vbYes Then
motaremplacer = Trim(InputBox("Entrer le mot à remplacer"))
mot = Trim(InputBox("Entrer le mot de remplacement"))
QuestionGras = "Voulez-vous mettre en gras ?"
RepQuestionGras = MsgBox(QuestionGras, vbQuestion + vbYesNo, "Bold")
Sheets(QuestionNomFeuille).Activate
Set oRegExp = CreateObject("vbscript.regexp")
With oRegExp
.Global = True
.ignorecase = False
For Each c In Range("A1:Z" & Range("A" & Rows.Count).End(xlUp).Row)
.Pattern = "(\s|^)(" & motaremplacer & ")(\s|$)"
c.Font.Bold = False
chaine = c.Value
If .test(chaine) = True Then
Set matches = .Execute(chaine)
c.Value = .Replace(chaine, "$1" & mot & "$3")
chaine = c.Value
.Pattern = "(\s|^)" & mot & "(\s|$)"
If RepQuestionGras = vbYes Then
Set matches = .Execute(chaine)
For i = 0 To matches.Count - 1
c.Characters(Start:=matches.Item(i).firstIndex + 1, _
Length:=matches.Item(i).Length).Font.Bold = True
Next i
End If
End If
Next c
End With
k = 9
If RepQuestionGras = vbNo Then
MsgBox (Chr(34) & motaremplacer & Chr(34) & " a été remplacé par " & Chr(34) & mot & Chr(34) & " sur la feuille " & Chr(34) & QuestionNomFeuille & Chr(34)), vbInformation
Else
MsgBox (Chr(34) & motaremplacer & Chr(34) & " a été remplacé par " & Chr(34) & mot & Chr(34) & " (en gras)" & " sur la feuille " & Chr(34) & QuestionNomFeuille & Chr(34)), vbInformation
End If
Else
k = 0
End If
Else
MsgBox (Chr(34) & QuestionNomFeuille & Chr(34) & " n'existe pas"), vbExclamation
If k = 9 Then
MsgBox ("Feuille non-trouvée après 10 tentatives. Macro annulée"), vbCritical
End If
End If
Next
Loop While k = 9
End If
End Sub