+ Reply to Thread
Results 1 to 4 of 4

Sorting Rows in VBA

Hybrid View

  1. #1
    Kevin
    Guest

    Sorting Rows in VBA

    OK. I've got a feeling that I'm going to feel like an idiot when someone
    tells me what I'm doing wrong, but I am having trouble sorting rows in VBA.
    Consider the following code snippet:

    Dim intFirstRow, intSecondRow as Integer
    intFirstRow = 9
    intSecondRow = 15
    With ActiveSheet
    .Rows(intFirstRow, intSecondRow).Sort
    End With

    I keep getting run-time error 1004 which says "Application-defined or
    object-defined error", which seems to mean: Something is wrong. VBA's not
    sure what it is, but something is definitely wrong.

    Alternatively, I tried the line:
    .Rows(.Rows(intFirstRow),.Rows(intSecondRow)).Sort

    I got the same error.

    Can anyone point me in the right direction?

    Thanks in advance!!!

    --
    Kevin

  2. #2
    Tom Ogilvy
    Guest

    Re: Sorting Rows in VBA

    Sub AA()
    Dim intFirstRow as Long, intSecondRow as Long
    intFirstRow = 5
    intSecondrow = 9
    With ActiveSheet
    .Range(intFirstRow & ":" & intSecondrow).Sort _
    key1:=.Cells(intFirstRow, 1)
    End With
    End Sub

    Worked for me
    --
    Regards,
    Tom Ogilvy

    "Kevin" <Kevin@discussions.microsoft.com> wrote in message
    news:4F83D338-EF8E-452D-B2D2-A5BE9EC58E9A@microsoft.com...
    > OK. I've got a feeling that I'm going to feel like an idiot when someone
    > tells me what I'm doing wrong, but I am having trouble sorting rows in

    VBA.
    > Consider the following code snippet:
    >
    > Dim intFirstRow, intSecondRow as Integer
    > intFirstRow = 9
    > intSecondRow = 15
    > With ActiveSheet
    > .Rows(intFirstRow, intSecondRow).Sort
    > End With
    >
    > I keep getting run-time error 1004 which says "Application-defined or
    > object-defined error", which seems to mean: Something is wrong. VBA's not
    > sure what it is, but something is definitely wrong.
    >
    > Alternatively, I tried the line:
    > .Rows(.Rows(intFirstRow),.Rows(intSecondRow)).Sort
    >
    > I got the same error.
    >
    > Can anyone point me in the right direction?
    >
    > Thanks in advance!!!
    >
    > --
    > Kevin




  3. #3
    Bob Phillips
    Guest

    Re: Sorting Rows in VBA

    You are only defining a single cell to sort (a bit pointless no?).

    You might mean all rows between the first and last
    .Rows(intFirstRow & ":" & intSecondRow).Sort

    or perhaps you mean only some columns, say H:M, in which case use
    .Range(.Cells(intFirstRow,"H"),.Cells(intSecondRow,"M")).Sort

    And then you haven't specified the key to sort on, which should be the first
    data cell in a specific column.

    BTW, Dim intFirstRow, intSecondRow as Integer doesn't declare both variables
    as Integer, the first is a variant. You have to be specific

    Dim intFirstRow as Integer, intSecondRow as Integer

    --

    HTH



    RP
    (remove nothere from the email address if mailing direct)


    "Kevin" <Kevin@discussions.microsoft.com> wrote in message
    news:4F83D338-EF8E-452D-B2D2-A5BE9EC58E9A@microsoft.com...
    > OK. I've got a feeling that I'm going to feel like an idiot when someone
    > tells me what I'm doing wrong, but I am having trouble sorting rows in

    VBA.
    > Consider the following code snippet:
    >
    > Dim intFirstRow, intSecondRow as Integer
    > intFirstRow = 9
    > intSecondRow = 15
    > With ActiveSheet
    > .Rows(intFirstRow, intSecondRow).Sort
    > End With
    >
    > I keep getting run-time error 1004 which says "Application-defined or
    > object-defined error", which seems to mean: Something is wrong. VBA's not
    > sure what it is, but something is definitely wrong.
    >
    > Alternatively, I tried the line:
    > .Rows(.Rows(intFirstRow),.Rows(intSecondRow)).Sort
    >
    > I got the same error.
    >
    > Can anyone point me in the right direction?
    >
    > Thanks in advance!!!
    >
    > --
    > Kevin




  4. #4
    Bob Phillips
    Guest

    Re: Sorting Rows in VBA

    Actually, it is not even a single cell it is nothing real. I read Rows as
    Range. Rest still stands though.

    --

    HTH

    RP
    (remove nothere from the email address if mailing direct)


    "Bob Phillips" <bob.phillips@notheretiscali.co.uk> wrote in message
    news:ekbSt9WNFHA.4092@tk2msftngp13.phx.gbl...
    > You are only defining a single cell to sort (a bit pointless no?).
    >
    > You might mean all rows between the first and last
    > .Rows(intFirstRow & ":" & intSecondRow).Sort
    >
    > or perhaps you mean only some columns, say H:M, in which case use
    > .Range(.Cells(intFirstRow,"H"),.Cells(intSecondRow,"M")).Sort
    >
    > And then you haven't specified the key to sort on, which should be the

    first
    > data cell in a specific column.
    >
    > BTW, Dim intFirstRow, intSecondRow as Integer doesn't declare both

    variables
    > as Integer, the first is a variant. You have to be specific
    >
    > Dim intFirstRow as Integer, intSecondRow as Integer
    >
    > --
    >
    > HTH
    >
    >
    >
    > RP
    > (remove nothere from the email address if mailing direct)
    >
    >
    > "Kevin" <Kevin@discussions.microsoft.com> wrote in message
    > news:4F83D338-EF8E-452D-B2D2-A5BE9EC58E9A@microsoft.com...
    > > OK. I've got a feeling that I'm going to feel like an idiot when someone
    > > tells me what I'm doing wrong, but I am having trouble sorting rows in

    > VBA.
    > > Consider the following code snippet:
    > >
    > > Dim intFirstRow, intSecondRow as Integer
    > > intFirstRow = 9
    > > intSecondRow = 15
    > > With ActiveSheet
    > > .Rows(intFirstRow, intSecondRow).Sort
    > > End With
    > >
    > > I keep getting run-time error 1004 which says "Application-defined or
    > > object-defined error", which seems to mean: Something is wrong. VBA's

    not
    > > sure what it is, but something is definitely wrong.
    > >
    > > Alternatively, I tried the line:
    > > .Rows(.Rows(intFirstRow),.Rows(intSecondRow)).Sort
    > >
    > > I got the same error.
    > >
    > > Can anyone point me in the right direction?
    > >
    > > Thanks in advance!!!
    > >
    > > --
    > > Kevin

    >
    >




+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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