Remove trailing spaces in a cell
To remove only the trailing spaces from text in a cell we need to use the VBA RTrim function
Example: Remove trailing spaces in a cell
Sub Remove_trailing_spaces_in_a_cell()
'declare a variable
Dim ws As Worksheet
Dim ws As Worksheet
Set ws = Worksheets("Analysis")
'Remove trailing spaces in cell (B5)
ws.Range("C5") = RTrim(ws.Range("B5"))
ws.Range("C5") = RTrim(ws.Range("B5"))
End Sub
OBJECTS
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
Worksheets: The Worksheets object represents all of the worksheets in a workbook, excluding chart sheets.
Range: The Range object is a representation of a single cell or a range of cells in a worksheet.
PREREQUISITES
Worksheet Name: Have a worksheet named Analysis.
Text: Have the text from which you only want to remove the trailing spaces, populated in cell ("B5").
Worksheet Name: Have a worksheet named Analysis.
Text: Have the text from which you only want to remove the trailing spaces, populated in cell ("B5").
ADJUSTABLE PARAMETERS
Output Range: Select the output range by changing the cell reference ("C5") in the VBA code to any cell in the worksheet, that doesn't conflict with formula.
Text: Select the text from which you only want to remove the trailing spaces by changing the cell reference ("B5") to any range in the worksheet, that doesn't conflict with the formula.
EXPLANATION
To remove only the trailing spaces from text in a cell we need to use the VBA RTrim function. The VBA RTrim function only removes the extra space at the end of the text in a cell. Therefore, any extra spaces at the start of the text or between words in a cell will remain.