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.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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:
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,557
Members
449,088
Latest member
davidcom

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