LaraC
Honorary Master
- Joined
- Mar 11, 2014
- Messages
- 42,673
- Reaction score
- 1,847
I have a text field where people can type anything, however they frequently type a leading space before punctuation marks. To correct it I’ve compiled a macro to remove the leading space.
Is there another method to accomplish the task?
Can the sub be converted to a function?
Is there a more elegant way of coding instead of listing the replacement arguments on a separate line?
As an example the code will remove the spaces before the comma and the full stop in the following sentence:
"All bad dogs bark all night long , how annoying ."
Ps. I’m using Excel for Windows.
Is there another method to accomplish the task?
Can the sub be converted to a function?
Is there a more elegant way of coding instead of listing the replacement arguments on a separate line?
As an example the code will remove the spaces before the comma and the full stop in the following sentence:
"All bad dogs bark all night long , how annoying ."
Ps. I’m using Excel for Windows.
Code:
Sub Replace_spaces()
Columns("A:A").Select
Selection.Replace What:=" ,", Replacement:=",", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Selection.Replace What:=" .", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByColumns, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
End Sub