Trim column in table databodyrange?

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
So as discovered earlier I have an issue with extra spaces when I bring in data from another souce file.

What I want is to be able to, using VBA script, trim all extra spaces, specifically after the text.

My data is in a table titled "PasteTable" I have tried every solution I can think of and I can find on the web but cant seem to get it right.

The data length is dynamic, every time I use it it could be one row or 80 not telling.

Help, Suggestions, or guidance would be greatly appreciated.
 

Some videos you may like

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
How about this...

Code:
Sub CleanTable()


    Dim r As Long, c As Long
    
    For r = 1 To ActiveSheet.ListObjects("PasteTable").ListRows.Count
        For c = 1 To ActiveSheet.ListObjects("PasteTable").ListColumns.Count
            ActiveSheet.ListObjects("PasteTable").DataBodyRange(r, c) = Trim(ActiveSheet.ListObjects("PasteTable").DataBodyRange(r, c))
        Next
    Next
    
End Sub
 
Last edited:

cizzett

Board Regular
Joined
Jan 10, 2019
Messages
121
I actually just got it to work, dont know why it kicked my but so bad I guess I was overthinking it.

Code:
Private Sub TrimDest()

Dim Cell As Range
  For Each Cell In ActiveSheet.UsedRange.Columns("D").Cells
    Cell.Value = WorksheetFunction.Trim(Cell.Value)
  Next
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,123,385
Messages
5,601,331
Members
414,446
Latest member
CRAVIN

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
Top