ilsley_excel
Board Regular
- Joined
- Mar 5, 2015
- Messages
- 54
- Office Version
- 2010
- Platform
- 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).
... but I keep getting a 'method Range of object_ Global failed' error.
Any ideas?
Thanks.
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.