+ Reply to Thread
Results 1 to 13 of 13

Trim all Cells in a sheet

  1. #1
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Smile Trim all Cells in a sheet

    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 1 To Column
                Columns
    (i).Select
                Selection
    .Insert Shift:=xlToRight
                ActiveSheet
    .Range(Cells(1i), Cells(Numberi)) = "=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

  2. #2
    Forum Contributor
    Join Date
    02-19-2005
    Location
    Gurgaon,India
    MS-Off Ver
    2007,2010,2013
    Posts
    180

    Re: Trim all Cells in a sheet

    Hi,

    Try this one

    Please Login or Register  to view this content.
    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.

    HTH
    Kris

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Trim all Cells in a sheet

    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:
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Trim all Cells in a sheet

    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 RangeRng 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 

  5. #5
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Trim all Cells in a sheet

    Quote Originally Posted by shg View Post
    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:
    Please Login or Register  to view this content.
    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... :)

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Trim all Cells in a sheet

    The VBA Trim and Worksheet TRIM functions are different; TRIM replaces sequential interior spaces with one, Trim does not.

  7. #7
    Forum Contributor
    Join Date
    02-19-2005
    Location
    Gurgaon,India
    MS-Off Ver
    2007,2010,2013
    Posts
    180

    Re: Trim all Cells in a sheet

    Hi,

    No idea why it causes the error. Is it(Sheet) protected?

  8. #8
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Trim all Cells in a sheet

    Quote Originally Posted by Krishnakumar View Post
    Hi,

    No idea why it causes the error. Is it(Sheet) protected?

    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

  9. #9
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Trim all Cells in a sheet

    I am actually not closing it as I don't see where I should do this...
    well... anyways. Thanks

  10. #10
    Forum Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898

    Re: Trim all Cells in a sheet

    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
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

  11. #11
    Registered User
    Join Date
    08-24-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Trim all Cells in a sheet

    Done. Cheers

  12. #12
    Registered User
    Join Date
    08-01-2012
    Location
    Hyderabad, India
    MS-Off Ver
    Excel 2010
    Posts
    1

    Re: Trim all Cells in a sheet

    This code is not working in my Excel 2010 showing me run tiem error

  13. #13
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Trim all Cells in a sheet

    Pavan,

    Welcome to the Forum, unfortunately:

    Your post does not comply with Rule 2 of our Forum RULES. Don't post a question in the thread of another member -- start your own thread. If you feel it's particularly relevant, provide a link to the other thread. It makes sense to have a new thread for your question because a thread with numerous replies can be off putting & difficult to pick out relevant replies.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1