Sub ModifierModuleAvecBarres()
Dim comp As Object, modName As String, code As String
Dim lignes() As String, ligne As String, newLigne As String
Dim i As Long
modName = InputBox("Nom du module à modifier (ex: Module1)", "Encadrement de chaînes")
If modName = "" Then Exit Sub
On Error Resume Next
Set comp = ThisWorkbook.VBProject.VBComponents(modName)
If comp Is Nothing Then
MsgBox "Module introuvable.", vbCritical
Exit Sub
End If
On Error GoTo 0
code = comp.CodeModule.Lines(1, comp.CodeModule.CountOfLines)
lignes = Split(code, vbNewLine)
comp.CodeModule.DeleteLines 1, comp.CodeModule.CountOfLines
For i = 0 To UBound(lignes)
newLigne = EncadrerGuillemets(lignes(i))
comp.CodeModule.InsertLines comp.CodeModule.CountOfLines + 1, newLigne
Next i
MsgBox "Module '" & modName & "' modifié avec succès.", vbInformation
End Sub
Function EncadrerGuillemets(texte As String) As String
Dim i As Long
Dim c As String
Dim dansChaine As Boolean
Dim buffer As String
Dim resultat As String
i = 1
Do While i <= Len(texte)
c = Mid(texte, i, 1)
If c = """" Then
buffer = buffer & c
If i < Len(texte) And Mid(texte, i + 1, 1) = """" Then
buffer = buffer & """"
i = i + 1
Else
If dansChaine Then
resultat = resultat & "|" & buffer & "|"
buffer = ""
dansChaine = False
Else
dansChaine = True
buffer = """"
End If
End If
Else
If dansChaine Then
buffer = buffer & c
Else
resultat = resultat & c
End If
End If
i = i + 1
Loop
EncadrerGuillemets = resultat
End Function