ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Sorting Rows in VBA (https://www.excelbanter.com/excel-programming/326470-sorting-rows-vba.html)

Kevin

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

Tom Ogilvy

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" wrote in message
...
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




Bob Phillips[_6_]

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" wrote in message
...
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




Bob Phillips[_6_]

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" wrote in message
...
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" wrote in message
...
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







All times are GMT +1. The time now is 01:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com