excel - VBA Array Sort with Fieldname as Parameter -
here's deal...in trying past fear of class modules in excel vba, decided create class array, add functions (methods) adding elements, sorting instance, etc. things keep re-writing in normal modules functions/subs hope use of classes might step forward.
code module
public type thing name string somenumber double end type
class module
private psomething() thing
this followed usual public lets , gets, plus function inserting new values array. sorting function/method. there no problem sorting name or somenumber, far takes 2 function/methods. parameterize single function/mehod use optional parameter control field used. following works, seems bit clunky
function sortbyfield(optional fieldname string, optional sortorder vbasortorder) dim strtemp thing if sortorder = 0 sortorder = sobottomtotop if len(fieldname) = 0 fieldname = "name" dim long dim j long dim lngmin long dim lngmax long lngmin = lbound(psomething) lngmax = ubound(psomething) = lngmin lngmax - 1 j = + 1 lngmax if iif(sortorder = sobottomtotop, _ iif(fieldname = "name", psomething(i).name > psomething(j).name, _ psomething(i).somenumber > psomething(j).somenumber), _ iif(fieldname = "name", psomething(i).name < psomething(j).name, _ psomething(i).somenumber < psomething(j).somenumber)) _ strtemp = psomething(i) psomething(i) = psomething(j) psomething(j) = strtemp end if next j next end function
what replace following (and it's peer in second part of gawdawful if(iif...) nonsense
iif(fieldname = "name", psomething(i).name > psomething(j).name, psomething(i).somenumber > psomething(j).somenumber)
...with this
"psomething(i)." & fieldname > "psomething(j)." & fieldname
direct question: how string evaluate/convert code?
indirect question: there other technique pass in fieldname , have treated other string?
thanks in advance help, assistance, guidance, direction, references, advice fool's errand, or derisive comments :).
biggerdon, trying follow code , right nested iif gawdawful. can suggest rewrite code select case statements. might bit. further, big objective trying achieve? looks overkill single dimension array.
there might other excel vba built in methods can capitalize on.
i did quick internet search on sorting arrays , came across pearson's website http://www.cpearson.com/excel/sortingarrays.aspx
you might check out.
Comments
Post a Comment