'-------------------------------------------
'Retire les caractères de contrôle (sauf LF)
'de la feuille active
'-------------------------------------------
Sub SupprimeCaracteresDeContrôle()
Dim Rng As Range
Dim Area As Range
Dim TabCells As Variant
Dim i As Integer
Dim j As Integer
Dim Change As Boolean
Set Rng = ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants, xlTextValues)
If Rng Is Nothing Then Exit Sub
'MsgBox Rng.Address
Application.ScreenUpdating = False
For Each Area In Rng.Areas
If Area.Cells.Count = 1 Then
ReDim TabCells(1 To 1, 1 To 1)
TabCells(1, 1) = Area.Value
Else
TabCells = Area.Value
End If
Change = False
For i = 1 To UBound(TabCells, 1)
For j = 1 To UBound(TabCells, 2)
Change = Change Or RemoveControlChars(TabCells(i, j))
Next j
Next i
'Si modification dans l'Area, valoriser ses cellules
If Change Then Area.Value = TabCells
Next Area
Application.ScreenUpdating = True
End Sub
'---------------------------------------------
'Retire les caractères de contrôle (sauf LF)
'de la chaine de caractère passée en paramètre
'---------------------------------------------
Private Function RemoveControlChars(Valeur As Variant) As Boolean
Dim AscChar As Integer
Dim i As Integer
Dim Nb As Integer
Dim Change As Boolean
'Init Return Value
RemoveControlChars = False
If VarType(Valeur) <> vbString Then Exit Function
i = 1
Nb = Len(Valeur)
Change = False
Do While i <= Nb
AscChar = Asc(Mid(Valeur, i, 1))
If AscChar < 32 And AscChar <> 10 Then
Valeur = Replace(Valeur, Chr(AscChar), "")
Nb = Len(Valeur)
Change = True
Else
i = i + 1
End If
Loop
'Return Value
RemoveControlChars = Change
End Function