# Filter list per Character From Smallest to Highest using code

#### bordetaning

##### New Member
Hi Guys,

Is there a way we can filter list based on the number of character in a cell from lowest to highest using code alone?

Sample Below
Sa1
Sample 23
Samepl
Samples

Output
Sa1
Samepl
Samples
Sample 23

Thanks,

#### Bill R

##### New Member
1) Say your list starts at A1. Enter in B1 =len(A1) and copy down.
2) Use col C1 to C4 to number each item (1-4)
3) Enter in D1 =small(B:B,C1) and copy down
4) Enter in E1 =INDEX(A:A,MATCH(D1,B:B,0)) and copy down

#### Rick Rothstein

##### MrExcel MVP
1) Say your list starts at A1. Enter in B1 =len(A1) and copy down.
2) Use col C1 to C4 to number each item (1-4)
3) Enter in D1 =small(B:B,C1) and copy down
4) Enter in E1 =INDEX(A:A,MATCH(D1,B:B,0)) and copy down
I would use your first step, but then I would select Columns A and B and sort on Column B, then clear Column B.

#### kalak

##### Active Member
... (VBA?) code alone?
Code:
``````Sub sortz()
Dim a, i
Set a = Range("A1:A5")
For i = 2 To 5
a(i, 1) = Len(a(i, 1)) & Chr(2) & a(i, 1)
Next i
a.Replace "*" & Chr(2), ""
End Sub``````

#### bordetaning

##### New Member
Hi Guys,

I appreciate you taking time in viewing and answering my post. I've tried the the formula and it works but the problem I noticed now is that it won't filter all entries that has the same number of charcter.

Example
Sample1
Sample2
Sample3

the output will only filter the first entry
Sample1
Sample1
Sample1

Is there any way we can filter all entries that has the same number of characters using formula and some tricks alone?

Thanks so much guys

#### Rick Rothstein

##### MrExcel MVP
I appreciate you taking time in viewing and answering my post. I've tried the the formula and it works but the problem I noticed now is that it won't filter all entries that has the same number of charcter.
Did you try the variation on Bill's method that I proposed in Message #3?

#### Bill R

##### New Member
Oops... add change in red text:

1) Say your list starts at A1. Enter in B1 =len(A1)
+row()/10000 and copy down. (This adds a fraction of the row # so as to differentiate items. Make it 100000 if 10000 isn't enough)
2) Use col C1 to C4 to number each item (1-4)
3) Enter in D1 =small(B:B,C1) and copy down
4) Enter in E1 =INDEX(A:A,MATCH(D1,B:B,0)) and copy down

Now... I'm guessing your next question will be... how to alphabetize the list after it is sorted by length?

Stick these in row 1 and copy down as needed

F1 =LEN(E1)/10000&E1
G1 =COUNTIF(F:F,"<="&F1)+ROW()/10000
H1 =SMALL(G:G,I1)
I1 = row()
J1 =INDEX(E:E,MATCH(H1,G:G,0))

Or you could use Rick's method and sort 1st on length of item and 2nd on the list of items.

Last edited:

#### bordetaning

##### New Member
Thanks Guys, It worked. Your a life Saver.

1,081,526
Messages
5,359,287
Members
400,524
Latest member
Excelbat

### This Week's Hot Topics

• VBA (Userform)
Hi All, I just would like to know why my code isn't working. Here is my VBA code: [CODE=vba]Private Sub OKButton_Click() Dim i As Integer...
• List box that changes fill color
Hello, I have gone through so many pages trying to figure this out. I have a 2020 calendar that depending on the day needs to have a certain...
• Remove duplicates and retain one. Cross-linked cases
Hi all I ran out of google keywords to use and still couldn't find a reference how to achieve the results of a single count. It would be great if...
• VBA Copy and Paste With Duplicates
Hello All, I'm in need of some input. My VBA skills are sub-par at best. I've assembled this code from basic research and it works but is...
• Macro
is it possible for a macro to run if the active cell value is different to the value above it
• IF DATE and TIME
I currently use this to check if date has passed but i also need to set a time on it too. Is it possible? [CODE=vba]=IF(B:B>TODAY(),"Not...