Hi,
I'm new here ans have only basic understanding of VBA. However, once it works, I do love it.Here is my problem.
I'm trying to trim all the data in my worksheet in order to prepare it for several steps of further analysis. Running the below code will cause an error that I don't understand. It says:
Runtime error '1004': Application-defined or object-defined error
the code is (error causing line indicated in comments):
PHP Code:Sub trimAll()
Application.ScreenUpdating = False
Dim Rows As Long
Dim Column As Long
Dim i As Integer
Rows = ActiveSheet.UsedRange.Rows.Count
Column = ActiveSheet.UsedRange.Columns.Count
For i = 1 To Column
Columns(i).Select
Selection.Insert Shift:=xlToRight
ActiveSheet.Range(Cells(1, i), Cells(Number, i)) = "=TRIM(RC[1])" ** ERROR **
Columns(i).Select
Selection.Copy
Columns(i + 1).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Columns(i).Select
Selection.Delete Shift:=xlToLeft
Next i
Application.ScreenUpdating = True
End Sub
Last edited by iPad; 08-24-2009 at 11:13 AM. Reason: in compliance with rule3 now, I hope. sorry for this
Hi,
Try this one
In your code you didn't dim the variable 'Number'. Also try to avoid dim the variables like Rows, Column as these are reserved in VBA.Sub kTest() Dim Cel As Range, Rng As Range Set Rng = ActiveSheet.UsedRange For Each Cel In Rng If Len(Cel) > 0 Then Cel = Trim(Cel) Next End Sub
HTH
Kris
You code is entering an R1C1 formula without telling Excel to set the FormulaR1C1 property.
It's also assuming that the used range always starts in row 1, which may not be true.
Here's an option, assuming there are no formulas on the sheet:
Sub x() With ActiveSheet.UsedRange .Value = Evaluate("if(row(" & .Address & "), trim(" & .Address & "))") End With End Sub
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Kris,
thanks for this. I feel stupid looking on the code for ages...not seeing that I forgot to Dim this variable.
Now, your code looks much smaller and I think I like that.
However, it doesn't work and gives me a run-time error '13': Type mismatch.
PHP Code:Sub kTest()
Dim Cel As Range, Rng As Range
Set Rng = ActiveSheet.UsedRange
For Each Cel In Rng '' Causes Run-Time Error
If Len(Cel) > 0 Then Cel = Trim(Cel)
Next
End Sub
Hi
The second code runs fine. The result is different to my first code thouhg.
If I use the formula =trim(whateverCell) , everything will then be in text format and all empty spaces are gone.
Is it possible that the VBA code "trim()" is different to that?
thanks for your time! Much appreciated!!![]()
Last edited by iPad; 08-24-2009 at 10:42 AM. Reason: got confused... :)
The VBA Trim and Worksheet TRIM functions are different; TRIM replaces sequential interior spaces with one, Trim does not.
Microsoft MVP - Excel
Entia non sunt multiplicanda sine necessitate
Hi,
No idea why it causes the error. Is it(Sheet) protected?
Kris
Hi Kris,
no, no protection at all.
It just won't run.
I think I will just follow your first advice and Dim ALL my variables for a start ...
![]()
Well, thanks to you both for helping. I think I am going to close this thread now as my initial problem is solved.
Cheers, iPad
I am actually not closing it as I don't see where I should do this...
well... anyways. Thanks
How to mark a thread Solved
Go to the first post
Click edit
Click Go Advanced
Just below the word Title you will see a dropdown with the word No prefix.
Change to Solved
Click Save
Microsoft MVP - Excel
Where there is a will there are many ways. Pick One!
Please read the Forum Rules
If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below
Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.
Preferred Charities: Lupus Canada and Sick Kids Foundation.
Feel Free to Donate if you want to, for the assistance you received today.
Done. Cheers
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks