Search
Close this search box.

VBA – RTrim String


The RTrim function in VBA is used to remove any trailing spaces (spaces at the end) from a string.

Syntax:

RTrim(string)

Parameter:

 

  • string: The string from which you want to remove trailing spaces.

Return Value:

  • String: Returns the same string without any trailing spaces.

 

Example

				
					Dim str1 As String
Dim trimmedStr As String

str1 = "Hello, World!   " 
trimmedStr = RTrim(str1) 

' trimmedStr will be "Hello, World!"
				
			

Use Cases:

  • Data Cleaning:
    • Removing extra spaces from user input.
    • Preparing data for database entry or processing.
  • String Comparisons:
    • Ensuring accurate string comparisons by removing any trailing spaces that might affect the result.
  • Data Formatting:
    • Removing unnecessary spaces from the end of text fields in reports or forms.

Key Considerations:

  • RTrim only removes trailing spaces. It does not affect leading spaces or spaces within the string.
  • For removing both leading and trailing spaces, use the Trim function.