Friday 25 October 2013

Excel Macro to remove the spaces from the cell values


Sub removeSpaces()
'Excel macro to remove the leading and trailing spaces from the string or cell value.
'In Excel macro we can use trim functions to remove leading and trailing spaces from given string

MsgBox LTrim("  Kapil Sharma ") ' output "Kapil Sharma "
MsgBox RTrim("  Kapil Sharma ") ' output " Kapil Sharma"
MsgBox Trim("  Kapil Sharma ")  'output "Kapil Sharma"

' As displayed in above code we have 3 functions to remove the spaces from the string
'ltrim - removes the spaces from left side of the string
'rtrim - removes the spaces from right side of the string
'trim - removes the spaces from both left and right side of the string

'Please note that we can not remove the internal spaces from the string
'To remove the spaces from internal part from the string, you can use below macro code

MsgBox Replace(" Kapil Sharma ", " ", "")

'Output of above code will print "KapilSharma"
'Note that all spaces from the string are removed


End Sub

What do you think on above excel macro topic? Please provide your input, comments, questions on excel macro. I will try to reply as soon as possible. I value your opinion on Excel Macros.

No comments:

Post a Comment

Popular Posts

Search This Blog