JavaScript:
//https://www.mrexcel.com/board/threads/macro-to-trim-all-cells.242545/
Dim cell As Range
For Each cell In ActiveSheet.UsedRange.SpecialCells(xlCellTypeConstants)
cell = WorksheetFunction.Trim(cell)
Next cell
WorksheetFunction.Trim = will remove sequential spaces within the text, whereas the VBA function Trim will not. Both the worksheetfunction and the VBA function trim leading and trailing spaces.