ashish mehra
Active Member
Hi all,
The below code is sorting on column B in ascending order.
I want help in
1) inserting header too & then
2) would be able to sort on any column in case sort column number changes.
The below code is sorting on column B in ascending order.
I want help in
1) inserting header too & then
2) would be able to sort on any column in case sort column number changes.
Code:
Sub SortingMultiDimensionArray()
'Writing Range to Dynamic Array
Dim MyArray() As Variant
Dim myRange As Range
Dim LR As Long, wsName As String
Sheet1.Activate
MyArray = Range("A2", Range("A1").End(xlDown).End(xlToRight))
'Writing Dynamic Array to Range
Worksheets.Add
Set myRange = Range(ActiveCell, ActiveCell.Offset(UBound(MyArray, 1) - 1, UBound(MyArray, 2) - 1))
myRange = MyArray
LR = Cells(Rows.Count, 1).End(xlUp).Row
'set the sort key, in this case sort by column B
ActiveSheet.Sort.SortFields.Add Key:=Range("B" & LR), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange myRange
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("B2").Select
Erase MyArray
End Sub