VBA trim and clean quick

YounesB3

Board Regular
Joined
Mar 28, 2012
Messages
148
Hello, I'm trying to clean and trim a specific range in VBA and I'm having errors. My problem is probably while setting my range, but I'm not sure why.

All my variables are defined earlier in the code:

VBA Code:
Private LastrowMaster As Long, LastrowCT As Long
Private WS1 As String
Private MainWB As Workbook
Private rng As Range, Area As Range

Set rng = MainWB.Worksheets(WS1).Range("A" & LastrowCT & ":BP" & LastrowMaster)

'If I use this, I get an Object required error
For Each Area In rng.Areas
    Area.Value = Evaluate("IF(ROW(" & Area.Address & "),CLEAN(TRIM(" & Area.Address & ")))")
Next Area

'If I use this, I get a Type Mismatch error
For Each cell In rng.Cells
    cell.Value = Application.WorksheetFunction.Clean(Trim(cell.Value))
Next cell

What am I doing wrong?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
I removed the Option Explicit. It says object required. As if the variable wasn't defined. I don't get it.

Do range variable need to use the "set" always?
Normally yes but "For Each <object> in <object_collection>
would be the exception

Can you replace your for each loop with this one and tell us what you see in the immediate window.
VBA Code:
Debug.Print rng.Address
For Each Area In rng.Areas
    Debug.Print Area.Address
    Area.Value = Evaluate("IF(ROW(" & Area.Address(1, 1, xlA1, 1) & "),CLEAN(TRIM(" & Area.Address(1, 1, xlA1, 1) & ")))")
Next Area

Also where do you have your code ? In a Sheet module or a normal module ?
Any reason you are delclaring your variables at the module level and not inside the actual procedure (ie Sub) ?
 
Upvote 0
Solution
Any reason you are delclaring your variables at the module level and not inside the actual procedure (ie Sub) ?
Because they are Private declarations not Dim

@Alex Blakenburg , try running the first code below (the code in post 5) and then try running the 2nd code below.
You just need a sheet called ABC, you don't need anything in the cells to show the difference in how the 2 codes act.

VBA Code:
Option Explicit

    Private WS1 As String
    Private MainWB As Workbook
    Private rng As Range, cell As Range
Sub TEST()
    Set MainWB = ThisWorkbook
    WS1 = "ABC"
    Set rng = MainWB.Worksheets(WS1).Range("A3000:BP3020")

 
    For Each cell In rng.Cells
        cell.Value = Application.WorksheetFunction.Clean(Trim(cell.Value))
    Next cell
End Sub

VBA Code:
Option Explicit


Sub TEST2()
    Private WS1 As String
    Private MainWB As Workbook
    Private rng As Range, cell As Range

    Set MainWB = ThisWorkbook
    WS1 = "ABC"
    Set rng = MainWB.Worksheets(WS1).Range("A3000:BP3020")

 
    For Each cell In rng.Cells
        cell.Value = Application.WorksheetFunction.Clean(Trim(cell.Value))
    Next cell
End Sub
 
Upvote 0
Hi, I replaced this

VBA Code:
For Each Area In rng.Areas
   Area.Value = Evaluate("IF(ROW(" & Area.Address(1, 1, xlA1, 1) & "),CLEAN(TRIM(" & Area.Address(1, 1, xlA1, 1) & ")))")
Next Area


With this

VBA Code:
For Each Area In rng.Areas
    Area.Value = Evaluate("IF(ROW(" & Area.Address(1, 1, xlA1, 1) & "),CLEAN(TRIM(" & Area.Address(1, 1, xlA1, 1) & ")))")
Next Area


And it works. Now you might say it's the same, but apparently it's not. There's probably a special space character in the first code snippets that VBA doesn't like.

Just a quick question about the Evaluate thing which I'm not familiar. Can you explain how it works? I tried to read about it and it's not clear, but really more efficient then looping through cells which I would have normally done. Also, could you explain what you changed?

From this

VBA Code:
Evaluate("IF(ROW(" & Area.Address & "),CLEAN(TRIM(" & Area.Address & ")))")


To this

VBA Code:
Evaluate("IF(ROW(" & Area.Address(1, 1, xlA1, 1) & "),CLEAN(TRIM(" & Area.Address(1, 1, xlA1, 1) & ")))")


Thanks for your help both of you!
 
Upvote 0
Also, could you explain what you changed?
The important thing Alex changed is the red 1 in the below which changes it to External = True which means it references the book and sheet of the range being used, it defaults to False which references the ActiveSheet

Rich (BB code):
Evaluate("IF(ROW(" & Area.Address(1, 1, xlA1, 1) & "),CLEAN(TRIM(" & Area.Address(1, 1, xlA1, 1) & ")))")

the below will probably also work for you (although the above is better IMO as it specifies the parameters rather than relying on the defaults)

VBA Code:
Evaluate("IF(ROW(" & Area.Address(External:=True) & "),CLEAN(TRIM(" & Area.Address(External:=True) & ")))")

If you put the Address in the Immediate window you get the results below which shows the difference

VBA Code:
?Sheets("ABC").Range("a3000").address
$A$3000

?Sheets("ABC").Range("a3000").address(external:=true)
[Book1.xlsb]ABC!$A$3000
 
Last edited:
Upvote 0
Great, that's what I actually needed and the code would have crashed since I didn't test for that (and it's running from another tab).

What about the evaluate function, what does it do exactly?
 
Upvote 0
You're welcome (although I think that you should mark post 8 by Alex as the solution as that is what finally got the end result that you were looking for, I just helped explain it)
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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