PDA

View Full Version : Solved: Sort Left to Right



YellowLabPro
03-04-2007, 03:03 PM
My code is not working, w/ either of the following subs; cell references specified in the code or w/ the use of an input box. The sort feature does work when used from within Excel. Can someone see what the problem might be? Both codes are included below.

Thank you,

YLP


Sub Macro1()
Dim i As Long

With Range("am38:aw171")
For i = 1 To .Rows.Count
With .Rows(i)
.Rows(i).Sort Key1:=.Rows(i).Range("a1"), Order1:=xlAscending, _
Orientation:=xlLeftToRight
End With
Next
End With
End Sub




Sub Macro1()

Dim rng As Range
Dim i As Long
Set rng = Application.InputBox("Select range with the mouse", Type:=8)
If Not rng Is Nothing Then
With rng
For i = 1 To .Rows.Count
With .Rows(i)
.Rows(i).Sort Key1:=.Rows(i).Range("A1"), Order1:=xlAscending, _
Orientation:=xlLeftToRight
End With
Next
End With
End If
End Sub

Bob Phillips
03-04-2007, 04:35 PM
Second one works fine for me.

mdmackillop
03-04-2007, 04:52 PM
Hi Yelp,
You have an extra With .Rows(i) in your with structures

Sub Macro1()
Dim i As Long
With Range("am38:aw171")
For i = 1 To .Rows.Count
.Rows(i).Sort Key1:=.Rows(i).Range("a1"), Order1:=xlAscending, _
Orientation:=xlLeftToRight
Next
End With
End Sub

Simon Lloyd
03-04-2007, 04:53 PM
Would merged cells cause a problem with sorting this way?

Regards,
Simon

YellowLabPro
03-04-2007, 06:18 PM
Malcolm,
That was it, thank you!

YellowLabPro
03-04-2007, 06:19 PM
Simon,
I do not know the answer to that one, but I am only using it on non-merged cells currently.

Thanks,

YLP