Sort multiple columns of Excel in VBA given the top-left and lowest-right cell -


i trying sort these 3 columns (sort col-2) in excel using vba. top-left (row number , column number e.g. 1,1) , lowest-right cell (row number , column number e.g. 9,3) known. every cell contains values of string type.

input:

col-1      col-2    col-3  p1         i1       xyz 1 | xyz 2 | xyz 3 p8         i4       xyz 26 | xyz 27 | xyz 28 | xyz 29 p3         i2       xyz 10 | xyz 11 | xyz 12 p6         i5       xyz 19 | xyz 20 | xyz 21 p5         i2       xyz 16 | xyz17 | xyz 18 p9         i1       xyz 4 | xyz 5 | xyz 6 p7         i3       xyz 22 | xyz 23 | xyz 24 | xyz 25 p4         i2       xyz 13 | xyz14 | xyz 15 p2         i2       xyz 7 | xyz 8 | xyz 9 

output:

col-1      col-2    col-3  p1         i1       xyz 1 | xyz 2 | xyz 3 p9         i1       xyz 4 | xyz 5 | xyz 6 p3         i2       xyz 10 | xyz 11 | xyz 12 p5         i2       xyz 16 | xyz17 | xyz 18 p4         i2       xyz 13 | xyz14 | xyz 15 p2         i2       xyz 7 | xyz 8 | xyz 9 p7         i3       xyz 22 | xyz 23 | xyz 24 | xyz 25 p8         i4       xyz 26 | xyz 27 | xyz 28 | xyz 29 p6         i5       xyz 19 | xyz 20 | xyz 21 

macro recording gives me following code:

sub sortingrecord() ' ' sortingrecord macro '  ' activeworkbook.worksheets("sheet6").sort.sortfields.clear activeworkbook.worksheets("sheet6").sort.sortfields.add key:=range("b1:b9"), _     sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal activeworkbook.worksheets("sheet6").sort     .setrange range("a1:c9")     .header = xlguess     .matchcase = false     .orientation = xltoptobottom     .sortmethod = xlpinyin     .apply end end sub 

the problem new vba , not able understand meaning of many terms used in code. how decide statements necessary program solution.

the code doing doesn't need worksheet number running macro on particular sheet how use sort method?

please try me solving problem. not want range selecting cells want use cell numbers given in problem make program dynamic( there might change in number of rows , columns).

i got solution after going through many tutorials , hence posting here reference of 1 needs help.

sub testsort() dim currentsheet worksheet set currentsheet = activesheet  lastrows = currentsheet.cells(rows.count, 1).end(xlup).row lastcols = currentsheet.cells(1, columns.count).end(xltoleft).column  set sorta = currentsheet.range(cells(2, 1), cells(lastrows, lastcols))  currentsheet.sort.sortfields.clear currentsheet.sort.sortfields.add key:=range(cells(2, 2), cells(lastrows, 2)), _ sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal currentsheet.sort     .setrange sorta     .header = xlguess     .matchcase = false     .orientation = xltoptobottom     .sortmethod = xlpinyin     .apply end end sub 

note: range sorted start row=2 , column=1.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

php - Find a regex to take part of Email -

javascript - Function overwritting -