VBA Sort in Excel leaves table contents selected -
i have excel spreadsheet hold leaderboard table on 1 sheet based on data in separate sheet. monitor change on data sheet , automatically sort leaderboard table. seems work fine contents of leaderboard table left selected after sort. how clear selection seamlessly without affecting selection on data sheet.
i've been boy , done sort using with instead of select command.
here have:
sheet2(code) - data sheet
sub worksheet_change(byval target range) ' check if item changed in group area if not intersect(target, target.worksheet.range("f7:h93")) nothing activeworkbook.worksheets("leaderboard").sortleaderboard end if end sub
sheet1(code) - leaderboard sheet
sub sortleaderboard() dim lo excel.listobject set lo = activeworkbook.worksheets("leaderboard").listobjects("table3") lo .sort.sortfields.clear .sort.sortfields.add key:=range("table3[total]"), sorton:=xlsortonvalues, order:= _ xldescending, dataoption:=xlsortnormal .sort.sortfields.add key:=range("table3[name]"), sorton:=xlsortonvalues, order:= _ xlascending, dataoption:=xlsortnormal .sort .header = xlyes .matchcase = false .orientation = xltoptobottom .sortmethod = xlpinyin .apply end end end sub
it seems quirk of table if sheet isn't active. can avoid using old sort method instead:
sub sortleaderboard() dim lo excel.listobject set lo = me.listobjects("table3") lo .sort.sortfields.clear .range.sort key1:=.listcolumns("total").databodyrange.cells(1), _ order1:=xldescending, dataoption1:=xlsortnormal, _ key2:=.listcolumns("name").databodyrange.cells(1), _ order2:=xlascending, dataoption2:=xlsortnormal, _ sortmethod:=xlpinyin, matchcase:=false end end sub
for example.
Comments
Post a Comment