Trailing Zero

LongToast

Board Regular
Joined
Dec 1, 2021
Messages
55
Office Version
  1. 2019
Platform
  1. Windows
Hi everyone.

I want to sort data in a range, the key is numbers, my data key look like this :

bukti21.jpg


The problem is, in my real world, 1112 is actually less than 516, so to sort properly, i need to make 516 become 5160, 119 become 1190 and so on.

I use this code to sort :

VBA Code:
With oWs.Sort
            .SortFields.Clear
            .SortFields.Add Key:=oWs.Range("A1"), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
            .SetRange oWs.Range("A1:K" & iLastRow)
            .Header = xlNo
            .Apply
        End With

Solution in my head is "format that range before sort action", so i add this code :

Code:
oWs.Range("A:A").NumberFormat = "#000"
Code:
oWs.Range("A:A").NumberFormat = "0000"

That codes doesn't work

I can't use looping because i have hundred thousand data in my range, looping on com object just make it slower, i can't use array because bubble sort on array is very slow, i can't use quick sort because it only support 1 key (i have to sort on multiple keys).


So, any suggestion?
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Number format just changes the display, not the underlying value. And all Excel functions and functionality (like sorting) run on the actual values, not their displays.

You can either do what awoohaw suggests, of for any numbers less than 1000, multiply them by 10 to make them for digit numbers (assuming that all numbers are at least 3 digits).
 
Upvote 0
are you asking for alphabetical sorting? If so, just change your numeric values to text?
Example :
119 > 1112

119 is bigger than 1112, as you can see, this is not common, but i need to make this happen, so what i thought is how to add 0 to 119 and become 1190 so :

1190 > 1112 ----> this is the goal so i can sort it properly.
 
Upvote 0
Number format just changes the display, not the underlying value. And all Excel functions and functionality (like sorting) run on the actual values, not their displays.

You can either do what awoohaw suggests, of for any numbers less than 1000, multiply them by 10 to make them for digit numbers (assuming that all numbers are at least 3 digits).
To do this i need loop right?
 
Upvote 0
To do this i need loop right?
That would be one way.
You could also use a helper column/formula like:
Excel Formula:
=IF(A2<1000,A2*10,A2)
then copy/paste special values over original values, then delete helper column. No loops involved that way.
 
Upvote 0
what are you asking for loops for? Unless you're insisting on a VBA solution (which probably is not necessary):

Cell Formulas
RangeFormula
F4F4="119">"1112"
G4:G5G4=FORMULATEXT(F4)
F5F5=119*10>1112

I sort it on temporary sheet, not on original sheet, the process is :

VBA Code:
arrData = OriginalSheet.Range("A1:K" & LastRow).Value

TemporarySheet.Range("A1").Resize(UBound(arrData, 1), UBound(arrData, 2)).Value = arrData

'sort process on TemporarySheet here'
'after sorting i take that data back to array and use it to display in another sheet which has some format like color, space between data, etc'
 
Upvote 0
I sort it on temporary sheet, not on original sheet, the process is :

VBA Code:
arrData = OriginalSheet.Range("A1:K" & LastRow).Value

TemporarySheet.Range("A1").Resize(UBound(arrData, 1), UBound(arrData, 2)).Value = arrData

'sort process on TemporarySheet here'
'after sorting i take that data back to array and use it to display in another sheet which has some format like color, space between data, etc'
I'm confused. I don't see how that will do what you want to have done. Convert the values to text and then compare, is all I can say.
 
Upvote 0
I don't see how in that code how you are comparing two values against each other.
I comparing two values against each other on array when sort process is done, my problem is in sorting part.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top