Function TriMAllCellsInRange(ByRef RnG As Range)'trim le left et right
'supprime les espace en debut et fin de chaine de caracteres dans une plage equivalent de "Ltrim" in one shoot
With RnG.Parent.Range(RnG.Address)
.Value = Evaluate("IF(ISTEXT(" & .Address & "),MID(" & .Address & ",FIND(MID(TRIM(" & .Address & "),1,2)," & .Address & ",1),LEN(" & .Address & ")),REPT(" & .Address & ",1))")
TriMAllCellsInRange = Evaluate("IF(ISTEXT(" & .Address & "),MID(" & .Address & ",1,FIND(TRIM(RIGHT(SUBSTITUTE(TRIM(" & .Address & "), "" "", REPT("" "", 100)), 100))," & .Address & ",1)+LEN(TRIM(RIGHT(SUBSTITUTE(TRIM(" & .Address & "), "" "", REPT("" "", 100)), 100)))-1),REPT(" & .Address & ",1))")
End With
End Function
'
'
'equivalent à application.trim de VBA(régularSpace)
Function SupprfirstAndNexAndDoubleSpaceInRange(ByRef RnG As Range)
'supprime tout les espaces avant et apres la chaine et tout les doubles espaces dans la chaine in one shoot
With RnG
SupprfirstAndNexAndDoubleSpaceInRange = Evaluate("IF(ISTEXT(" & .Address & "),TRIM(" & .Address & "),REPT(" & .Address & ",1))")
End With
End Function '
Sub test() 'trim les valeurs dans la plage
Dim DL, RnG As Range
DL = Cells(Rows.Count, 3).End(xlUp).Row
Set RnG = Sheets(1).Range("C2:C" & DL)
RnG.Value = TriMAllCellsInRange(RnG)
End Sub
Sub test2() 'trim et supprime les espaces consécutif des valeurs
Dim DL, RnG As Range
DL = Cells(Rows.Count, 3).End(xlUp).Row
Set RnG = Sheets(1).Range("C2:C" & DL)
RnG.Value = SupprfirstAndNexAndDoubleSpaceInRange(RnG)
End Sub