excel - Macro to sort multiple tables on two criteria -
i have tried in vain below macro run correctly. end goal macro sort multiple tables (on single worksheet) based on 2 criteria, , work on whatever worksheet active. can create macro exact table references, seek more agile avoid having macro each worksheet.
the mass of code draws doug glancy's recommendation in this post, doesn't run (as original poster noted before thread died). macro runs without errors doesn't sort of data.
i think broken portion key:=lo.listcolumns("name of table column").range
part. not familiar plenty syntax troubleshoot it.
i appreciate suggestions or alternatives! also, allow me know if requirements unclear @ all.
sub customsort() dim lo excel.listobject dim ws excel.worksheet set ws = activesheet each lo in ws.listobjects lo.sort .sortfields.add key:=lo.listcolumns("status").range, sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .sortfields.add key:=lo.listcolumns("inventory number").range, sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .header = xlyes .apply end next lo end sub
thanks tomdillinger prompting me take closer @ macro's behavior. realized macro sorting of time, although correctly. added .sortfields.clear
before .sortfields.add
lines, clears existing sorting fresh start speak sort commands in macro. here functioning macro:
sub customsort() dim lo excel.listobject dim ws excel.worksheet set ws = activesheet each lo in ws.listobjects lo.sort .sortfields.clear .sortfields.add key:=lo.listcolumns("status").range, sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .sortfields.add key:=lo.listcolumns("inventory number").range, sorton:=xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal .header = xlyes .apply end next lo end sub
excel vba sorting excel-vba
No comments:
Post a Comment