Consulting

Results 1 to 2 of 2

Thread: Trim not working and moving through a range

  1. #1

    Trim not working and moving through a range

    Hey everybody, I have just started VBA programming and could use some help...

    I have a problem with trimming in Excel 2007:

    For some reason, the following does not work:
    [vba]
    Sub TrimEverything ()
    Dim Row NoR as As Long
    Dim Column, NoC As Integer

    NoR = Application.WorksheetFunction.CountA(ActiveWindow.ActiveSheet.Range("A1:A50 000"))
    NoC = Application.WorksheetFunction.CountA(ActiveWindow.ActiveSheet.Range("A1:Z1" ))

    For Row = 1 To NoR

    For Column = 1 To NoC
    Cells(Row, Column).Value = Trim(Cells(Row, Column).Value)
    Next Column

    Next Row

    End Sub[/vba]
    When I execute it says "Wrong number of arguments or invalid property assignment"... this is giving me headaches since substituting "Trim(Cells(Row, Column).Value)" with "LTrim(RTrim(Cells(Row, Column).Value))" seems to work just fine so the whole deal does not make much sense to me.

    Also, I am wondering if this is the best way to move through a worksheet for this kind of substitutions. Is there a way I can do it all at once instead of moving cell by cell? Or perhaps a faster or more proper code or something? I need to move through large quantities of data and do various stuff to it (split data and insert it elsewhere, uppercase, search/replace and such) so speed is important...

    Cheers,
    the_norm

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Don't use variables called Row and Column

    [vba]

    Sub TrimEverything()
    Dim nRow As Long, NoR As Long
    Dim nColumn, NoC As Integer

    NoR = Application.CountA(ActiveSheet.Range("A1:A50000"))
    NoC = Application.CountA(ActiveSheet.Range("A1:Z1"))

    For nRow = 1 To NoR

    For nColumn = 1 To NoC
    Cells(nRow, nColumn).Value = Trim(Cells(nRow, nColumn).Value)
    Next nColumn

    Next nRow

    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Posting Permissions

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