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!
 

Some videos you may like

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

maabadi

Well-known Member
Joined
Oct 22, 2012
Messages
1,641
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
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
 
Solution

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
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)
 

Monty_Python

New Member
Joined
Feb 9, 2021
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
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.
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,574
Office Version
  1. 365
Platform
  1. Windows
Glad we could help & thanks for the feedback.
 

Watch MrExcel Video

Forum statistics

Threads
1,127,653
Messages
5,626,101
Members
416,161
Latest member
David1966Lewis

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