sorting - sort, remove duplicates and blanks, return numbers only in an excel vba array -


in userform, have textbox multiline enabled . want user enter numbers in textbox each number being in new line , click commandbutton. commandbutton should store text array, sort in ascending order, remove duplicates , blanks , non numbers , return data excel sheet starting @ range i3.

i tried coding failed sort, remove blanks , non numbers. moreover, output in excel sheet not recognized numbers :(

in simple code, when following text entered textbox

1 2 3 4  6 5 

the output on excel sheet is

5  1 2 3 4 6 

here trial .. appreciated

private sub commandbutton1_click() dim strtext() string  dim long, k long  k = 3     strtext = split(textbox3.text, chr(10))       = 0 ubound(strtext)         sheet3.cells(k, 9).value = strtext(i)         k = k + 1     next  sheet3 .range("i3:i" & k).removeduplicates columns:=1, header:=xlno .range("i3:i" & k).sort key1:=.range("i3"), order1:=xlascending end end sub 

try following code:

private sub commandbutton1_click()     dim strtext() string     dim rng range     dim c range       strtext = split(textbox3.text, chr(10))     sheet1         'clear previous content         .range(.cells(3, 9), .cells(.rows.count, 9).end(xlup)).clearcontents         set rng = .cells(3, 9).resize(ubound(strtext)+1)     end      rng = application.transpose(strtext)     rng.replace chr(13), ""     each c in rng         if not isnumeric(c) c = ""     next      rng         .numberformat = "0"         .value = .value         .removeduplicates columns:=1, header:=xlno         .sort key1:=.cells(1, 1), order1:=xlascending     end  end sub 

notes:

  1. line .range(.cells(3, 9), .cells(.rows.count, 9).end(xlup)).clearcontents removes previous data column i
  2. line rng = application.transpose(strtext) writes data in column i without loop
  3. if value not numeric replace empty string: if not isnumeric(c) c = "" , removes using removeduplicates
  4. line .numberformat = "0" sets number format in range. line .value = .value converts "numbers stored text" "numbers stored numbers"

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -