Excel VBA use defined name for table name -


in following code, sorting table named deplist on depname column.

    sub sortdepnameaz()     on error resume next          activeworkbook.worksheets("department list").listobjects("deplist").sort. _              sortfields.clear          activeworkbook.worksheets("department list").listobjects("deplist").sort. _              sortfields.add key:=range("deplist[[#all],[depname]]"), sorton:= _              xlsortonvalues, order:=xlascending, dataoption:=xlsortnormal          activeworkbook.worksheets("department list").listobjects("deplist").sort            .header = xlyes            .matchcase = false            .orientation = xltoptobottom            .sortmethod = xlpinyin            .apply         end      end sub 

i want make more general can use function, using defined ranges , names instead of specific names.

i have tried using following can't make syntax work:

 sub sortdepnamesaz(lo1 listobject, dn1 variant)  on error resume next      set lo1 = application.activeworkbook.activesheet.listobject(1)      dim tabstr string      tabstr = application.activeworkbook.activesheet.listobject(1).name      lo1.sort.sortfields.clear      lo1.sort.sortfields.add key:=range(tabstr[[#all],[dn1]]), sorton:= _          xlsortonvalues, order:=xlascending 'i think line wrong??      lo1.sort          .header = xlyes          .matchcase = false          .orientation = xltoptobottom          .sortmethod = xlpinyin          .apply      end  end sub 

any appreciated, if few pointers know code improved on many levels.

thanks

function:

sub sorttablebyheader(shtn string, tabn string, hed1 variant)  dim hed2 range, hed3 integer   set hed2 = range(tabn).rows(0)      hed3 = application.match(hed1, hed2, 0)   range(tabn).sort key1:=range(tabn).cells(1, hed3), header:=xlyes   end sub 

calling function:

sub sortdepartmentname()  dim shtn string, objname string, mycolhead variant   shtn = sheets("department list").name  objname = sheets("department list").listobjects(1).name  mycolhead = "depname"   sorttablebyheader shtn, objname, mycolhead   end sub 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -