trim then add space

snowman1976

Board Regular
Joined
Nov 4, 2008
Messages
191
hello
I have some external data that I import daily into a spreadsheet using VBA. The data I am given is not consistent - there are often spaces in the string in data in column 'I', and I would like to clean it up before importing. The spacing is also never consistent so I would like to make it uniform before importing.

is there a way in VBA to change any spacing found in column I to 4 spaces? I have figured out a way already, but its quite slow. Basically I am trimming out the column first, then spreading it out to 4 spaces....but its very slow.

any help is appreciated


current code after opening the csv file:


[I:I] = [IFERROR(TRIM(I:I),"""")]
Columns("I:I").Select
Selection.Replace What:=" ", Replacement:=" ", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Try
Code:
With Intersect(ActiveSheet.UsedRange, Range("I:I"))
   .Value = Evaluate("if({1},trim(" & .Address & "),"""")")
   .Replace " ", "     ", xlPart, , , , False, False
End With
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top