David Montoya
New Member
- Joined
- Apr 25, 2018
- Messages
- 49
Gentlemen,
I need to apply the following code to a selected range; however, also need to limit the loop function to the last active cell of the range (otherwise it takes long time to go though the range), or to apply the code only to active cells of the range, instead of hard coding first column rows 4 to 1004.
Your assistance is appreciated:
I need to apply the following code to a selected range; however, also need to limit the loop function to the last active cell of the range (otherwise it takes long time to go though the range), or to apply the code only to active cells of the range, instead of hard coding first column rows 4 to 1004.
Your assistance is appreciated:
Code:
Sub RemoveLeadingTrailing()
For i = 4 To 1004
Do While InStr(ActiveSheet.Cells(i, 1), " ") > 0
ActiveSheet.Cells(i, 1).Value = Replace(ActiveSheet.Cells(i, 1), " ", " ")
Loop
ActiveSheet.Cells(i, 1).Value = Trim(ActiveSheet.Cells(i, 1).Value)
Next i
End Sub