VBA Code - Pick proper range, skip blanks and trim

Monty_Python

New Member
Joined
Feb 9, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
Hi All,

I'm trying to speed up a bit process of data validation, but I got stuck with too many ideas and no clear solution to my VBA code.
I've just started my macro/VBA fun, so getting a piece of information is great, but combing this into 1 logical way, for now, doesn't work for me.

What I have:
Excel file where starting from column R row 13 I have loads of cells. Depending on the file sometimes those data are only in column R, sometimes I have more columns this is why I need to include the Last Column. When it comes to the number of rows - they also differ, so I assume relative could help here.
Every column has blanks, which can't be deleted, but definitely, for a further purpose, the formula TRIM has to skip those blanks.
And the last point - when I have my range defined & blanks cells are instructed to be omitted I need to trim the data including both sides (before and after the wording, not between).

The last version of my formula which has an error looks like that :

Sub Trim_Data()

Dim DataRange As Range

Set DataRange = Range("R13:W" & LastRow)

Set R = Selection

If rng <> "" Then

For Each cell In rng

cell.Value = WorksheetFunction.Trim(cell)
Next
End If

End Sub

I'd much appreciate if anyone could help me with correcting ( or totally changing) the script.

Thank you!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Welcome to MrExcel Message Board.
TRIM Function Only remove additional spaces within cell Not Remove Blanks.
You don't Set Range for rng, Don't define LastRow, Don't Dim Cell & rng.
IF rng same as DataRange Use this:
VBA Code:
Sub Trim_Data()
Dim DataRange As Range, Lr as Long, Cell as Range
Lr = Range("R" & ROws.Count).End(xlup).Row
Set DataRange = Range("R13:W" & Lr)
For Each cell In DataRange
cell.Value = WorksheetFunction.Trim(cell.value)
Next
End Sub
 
Upvote 0
Solution
Based on this
I need to trim the data including both sides (before and after the wording, not between).
You need to use the VBA function not the worksheet function, so with maabadi's code it would be
VBA Code:
cell.Value = Trim(cell.value)
 
Upvote 0
Thank you both - it's working perfectly!
Now I need to understand what has been written here in order to instruct the proper steps, but many thanks for your support.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,650
Messages
6,120,736
Members
448,988
Latest member
BB_Unlv

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