Excel VBA How to sort a single-row Range -


i have range defined as

set myrange=range("$e$10,$g$10,$i$10") 

the values in these cells -1, -1.2, 1 when iterate on range values printed in order of -1, -1.2, 1

i sort range when iterate on range see: -1.2, -1, 1

i not want reorganize these cells in actual worksheet.

i trying mimic sort function in normal programming language, excel range, expect 'cells' re-arranged within range data structure

i have tried naive

myrange.sort key1:=myrange.item(1, 1), order1:=xlascending, header:=xlno 

but not anything

excel not sort non-contiguous range.

but using arraylist sort values easy ranges values in order. using sortanyrange it's desc parameter set true sort range in descending order.

enter image description here

windows only

sub testsortanyrange()     sortanyrange range("$e$10,$g$10,$i$10") end sub  sub sortanyrange(target range, optional desc boolean)     dim r range     dim list object     set list = createobject("system.collections.arraylist")     each r in target         list.add r.value     next      list.sort      if desc list.reverse      each r in target         r.value = list(0)         list.remove list(0)     next  end sub 

mac or windows

the op requested subroutine work on either mac or windows platform. reason, refactored code replacing arraylist (not available on mac) array , bubblesort routine.

sub sortanyrange(target range, optional desc boolean)     dim r range     dim list     dim long, j long      redim list(0 target.cells.count - 1)      each r in target       list(i) = r.value       = + 1     next      = lbound(list) ubound(list)         j = + 1 ubound(list)             if list(i) > list(j)                 srttemp = list(j)                 list(j) = list(i)                 list(i) = srttemp             end if         next j     next      = iif(desc, ubound(list), 0)      each r in target         r.value = list(i)         = + iif(desc, -1, 1)     next  end sub 

Comments

Popular posts from this blog

php - How to display all orders for a single product showing the most recent first? Woocommerce -

asp.net - How to correctly use QUERY_STRING in ISAPI rewrite? -

angularjs - How restrict admin panel using in backend laravel and admin panel on angular? -