excel - VBA Sort not working -
i made vba code macro used excel file calculation. requires sorting excel sheet 2 columns 1 one. sorting first column not able sort other although code both same except column number. here code snippet sheet's calculation:
sheets("restock clusters").select range("b1").select selection.pastespecial paste:=xlpastevaluesandnumberformats, operation:= _ xlnone, skipblanks:=false, transpose:=false lastrow = range("b:c").find("*", searchorder:=xlbyrows, searchdirection:=xlprevious).row range("a3").select range(selection, selection.end(xldown)).select selection.clearcontents range("a2").select selection.autofill destination:=range("a2:a" & lastrow) range("d3:f3").select range(selection, selection.end(xldown)).select selection.clearcontents range("d2:f2").select selection.autofill destination:=range("d2:f" & lastrow) range("c1").select application.cutcopymode = false activeworkbook.worksheets("restock clusters").autofilter.sort.sortfields.clear activeworkbook.worksheets("restock clusters").autofilter.sort.sortfields.add _ key:=range("c:c"), sorton:=xlsortonvalues, order:=xldescending, _ dataoption:=xlsortnormal activeworkbook.worksheets("restock clusters").autofilter.sort .header = xlyes .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end range("a1").select application.cutcopymode = false selection.autofilter selection.autofilter activeworkbook.worksheets("restock clusters").autofilter.sort.sortfields.clear activeworkbook.worksheets("restock clusters").autofilter.sort.sortfields.add _ key:=range("a:a"), sorton:=xlsortonvalues, order:=xlascending, _ dataoption:=xlsortnormal activeworkbook.worksheets("restock clusters").autofilter.sort .header = xlyes .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end activesheet.calculate
please avoid use of .select
slows down code besides causing runtime errors.
is trying (untested)?
sub sample() dim ws worksheet dim lastrow long set ws = thisworkbook.sheets("restock clusters") ws '~~> copy code??? .range("b1").pastespecial paste:=xlpastevaluesandnumberformats, _ operation:=xlnone, skipblanks:=false, transpose:=false lastrow = .range("b:c").find("*", searchorder:=xlbyrows, _ searchdirection:=xlprevious).row .range("a3:a" & lastrow).clearcontents .range("a2:a" & lastrow).formula = .range("a2").formula .range("d3:f" & lastrow).clearcontents .range("d2:f" & lastrow).formula = .range("d2:f2").formula '~~> sort col c .columns(3).sort key1:=.range("c2"), order1:=xlascending, header:=xlguess, _ ordercustom:=1, matchcase:=false, orientation:=xltoptobottom, _ dataoption1:=xlsortnormal '~~> sort col .columns(1).sort key1:=.range("a2"), order1:=xlascending, header:=xlguess, _ ordercustom:=1, matchcase:=false, orientation:=xltoptobottom, _ dataoption1:=xlsortnormal end end sub
Comments
Post a Comment