I was just wondering if anybody had any advice on what this piece of VBA code is doing. A colleague of mine left recently and hadn't commented any of their work so I am a bit confused.
There is some code to sort some cells but I'm not sure how it knows what to sort, as a range is specified which isn't the data I want sorting. I think I am missing something but nothing online seems to explain Excel VBA in a very good manner.
The code looks like this:
Range(Selection, Selection.End(xlDown)).Select
ActiveWorkbook.ActiveSheet.SORT.SortFields.Clear
ActiveWorkbook.ActiveSheet.SORT.SortFields.Add Key:=ActiveCell, _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
With ActiveWorkbook.ActiveSheet.SORT
.SetRange ActiveCell.Range("A1:A550")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
ActiveCell.Offset(0, 1).Range("A1").Select
I know that the top stuff sets the criteria to be used to sort the data, but I am unsure why range("A1:A550") is there, as the data to be sorted usually begins somewhere around cell C15. Is the code just saying "Use the current cell as A1, select the next 550 rows down from this and sort the data accordingly"?
Thank you all in advance.
A1:A550is relative to the selected cell. IfA1is selected then it will refer toA1:A550. IfB2is selected then that cell is considered to be theA1of the selection so it will refer to the rangeB2:B551. The same withActiveCell.Offset(0, 1).Range("A1")- it's telling it to select the first cell one column across from theActiveCell- you could just sayActiveCell.Offset(0, 1).