I have an excel sheet that I need to sort by an alphanumeric column. I can currently do this, but it's an ugly method and I want to have a vba macro to do this instead. It is numbers 3-22 and letters A-Z, currently I can only get vba to sort it 3-22 and it ignores the alphabetic portion. I need 3(A-Z) then 4(A-Z) etc and it needs to sort the whole sheet.
Currently I can accomplish this as follows: Make two new columns, first one
=IF(LEN(A4)=2,"0"&LEFT(A4,1),LEFT(A4,2))
the "0" is necessary to allow excel to see the new split number as a number and allows me to process both single and double digit numbers.
Second column:
=RIGHT(A3,1)
this works as I always have the letter as the second digit. I then have to sort by column B then sort by C. So I am using the GUI to "sort by then".
I would like a macro that will automate the GUI clicks and formula input I mentioned above
I recorded a macro to do it and this is what I got:
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C3").Select
ActiveCell.FormulaR1C1 = _
"=IF(LEN(RC[-1])=2,""0""&LEFT(RC[-1],1),LEFT(RC[-1],2))"
Range("B3").Select
Selection.End(xlDown).Select
Range("C162").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("D3").Select
ActiveCell.FormulaR1C1 = "=RIGHT(RC[-2],1)"
Range("C3").Select
Selection.End(xlDown).Select
Range("D162").Select
Range(Selection, Selection.End(xlUp)).Select
Selection.FillDown
Range("A1:E162").Select
Range("D162").Activate
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("C1:C162" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
xlSortTextAsNumbers
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add2 Key:=Range("D1:D162" _
), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Sheet1").Sort
.SetRange Range("A1:E162")
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
I would really like this to be cleaner so I can sort the data sheet by the alphanumeric column so I get 1(A-Z) then 2 (A-Z). Ideal output: 1A 2B 3C 2A 2B 2C etc