Loop through each row in range to sort row values alphabetically

ilsley_excel

Board Regular
Joined
Mar 5, 2015
Messages
54
Office Version
  1. 2010
Platform
  1. Windows
Hi. I have the following routine that I am trying to manipulate in order to loop through each row in a dynamic range and sort the values alphabetically (i.e. a horizontal sort).

VBA Code:
Sub SortRowRange(Target As Range, Optional Desc As Boolean)
    Dim r As Range
    Dim list As Object
    Set list = CreateObject("System.Collections.ArrayList")
    For Each r In Target
        list.Add r.Value
    Next

    list.Sort

    If Desc Then list.Reverse

    For Each r In Target
        r.Value = list(0)
        list.Remove list(0)
    Next

End Sub

'Call SortRowRange to go through each row in a range

Sub AlphabetiseRows()

 Dim lastRow As Long
 Dim DataRange As Range
 
 lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row
 Set DataRange = ActiveSheet.Range("I" & lastRow & ":W" & lastRow)
 
 For Each Row In DataRange
  
   'Start at row 2
    Rows.Value = 2
    SortRowRange Range(DataRange)
 
 Next

End Sub


... but I keep getting a 'method Range of object_ Global failed' error.

Any ideas?

Thanks.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Couldn't you just use Excel's built-in sort facility.? Something like this?

BTW, I suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

VBA Code:
Sub AlphabetiseRows_v2()
  Dim Rw As Range
  Dim lastRow As Long
 
  lastRow = Range("A" & Rows.Count).End(xlUp).Row
  Application.ScreenUpdating = False
  For Each Rw In Range("I2:W" & lastRow).Rows
    Rw.Sort Key1:=Rw, Order1:=xlAscending, Header:=xlNo, Orientation:=xlLeftToRight
  Next Rw
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Sub AlphabetiseRows_v2() Dim Rw As Range Dim lastRow As Long lastRow = Range("A" & Rows.Count).End(xlUp).Row Application.ScreenUpdating = False For Each Rw In Range("I2:W" & lastRow).Rows Rw.Sort Key1:=Rw, Order1:=xlAscending, Header:=xlNo, Orientation:=xlLeftToRight Next Rw Application.ScreenUpdating = True End Sub

Thank you so much! That worked!

Also - my profile shows the correct version. I'm still using Excel 2010!
 
Upvote 0
Thank you so much! That worked!
You're welcome. Thanks for the confirmation. :)

Also - my profile shows the correct version. I'm still using Excel 2010!
Your profile does not show any version(s)
1685013645678.png

If your version(s) were in your profile, it would show like this

1685013693659.png
 
Upvote 0

Forum statistics

Threads
1,215,079
Messages
6,123,009
Members
449,093
Latest member
ikke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top