Need to remove spaces from cells

Holley

Board Regular
Joined
Dec 11, 2019
Messages
155
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am having difficulty using the TRIM formula and think there may be a better way to accomplish what I am trying to do. I have 4 columns (C,D,E,F) that contain data that is imported from a text file. Each field is will accommodate up to 35 characters. As this imports into Excel, it converts these to spaces. Since there is no real data, the cells appear empty. Some cells may only contain 5 characters, but there are 30 spaces trailing. Some cells contain multiple words, so I do need to keep the space between them. I thought about find and replace, but since the number of trailing spaces will vary, I wasn't sure how to accomplish this.

Any suggestions and thanks in advance!
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Pick a cell with at least one leading space then in the immediate window of the vb editor, type
?asc(left(sheets("Sheet23").range("D9"),1)) and hit return. Change your sheet and range references to suit. If your result is not 32, then your problem might be that the space character is from another character set (e.g. Unicode) in which case your number is probably 160. Trim only works on character 32.

EDIT - if you find that the spaces are 160 then I think the approach would be to substitute 32 for 160 inside of Trim.
 
Upvote 0
i occasionally have troublesome columns so I
run this to trim the data.
usage: place cursor in column, then run macro.

Code:
'place cursor in the column to trim, then run:TrimMyCol
Public Sub TrimMyCol()
Dim vVal
Dim iRows As Long, iStartCol As Long

iStartCol = ActiveCell.Column
'Range("A1").Select
Range("A1").CurrentRegion.Select
iRows = Range("A1").CurrentRegion.Rows.Count

Cells(1, iStartCol).Select

While ActiveCell.Row <= iRows
   ActiveCell.Value = Trim(ActiveCell.Value)
   ActiveCell.Offset(1, 0).Select  'next row
Wend
'msgbox "Done"
End Sub
 
Upvote 0
If they are normal spaces try
VBA Code:
Sub Holley()
   With Range("C1:F" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("Trim(" & .Address & ")")
   End With
End Sub
 
Upvote 0
Solution
If they are normal spaces try
VBA Code:
Sub Holley()
   With Range("C1:F" & Range("A" & Rows.Count).End(xlUp).Row)
      .Value = Evaluate("Trim(" & .Address & ")")
   End With
End Sub
This worked perfectly! Much appreciated!!

Thanks as always!!!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,964
Messages
6,175,659
Members
452,666
Latest member
AllexDee

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