![]() |
loosing decimal upon sorting?
Hi all, Hope someone can help with this problem I use a dynamic array of Long type to keep som values in so that I ca sort them later. The problem is that when I list the values afte sorting them, the decimal value is lost; e.g 204,5 becomes 204,0 (al values have a zero after the decimal point) I do Dim myArr() As Long ReDim Preserve myArr(0 To counter) myArr(counter) = _ Worksheets("Sheet1").Range("A" & counter).Value ' Call sort(myArr) Then I use these algorithms to sort the values Sub sort(A() As Long) Dim i As Long, j As Long For i = LBound(A) To UBound(A) - 1 For j = i + 1 To UBound(A) If A(j) A(i) Then swapI A(j), A(i) End If Next j Next i End Sub Sub swapI(x As Long, y As Long) Dim tmp As Long tmp = x x = y y = tmp End Sub Can anyone help with this please -- hk ----------------------------------------------------------------------- hke's Profile: http://www.excelforum.com/member.php...nfo&userid=155 View this thread: http://www.excelforum.com/showthread.php?threadid=31931 |
loosing decimal upon sorting?
Long type variables hold only whole numbers. The decimal portion
of a number stored to a Long will be lost. Change your data types from Long to Double. -- Cordially, Chip Pearson Microsoft MVP - Excel Pearson Software Consulting, LLC www.cpearson.com "hke" wrote in message ... Hi all, Hope someone can help with this problem I use a dynamic array of Long type to keep som values in so that I can sort them later. The problem is that when I list the values after sorting them, the decimal value is lost; e.g 204,5 becomes 204,0 (all values have a zero after the decimal point) I do Dim myArr() As Long ReDim Preserve myArr(0 To counter) myArr(counter) = _ Worksheets("Sheet1").Range("A" & counter).Value ' Call sort(myArr) Then I use these algorithms to sort the values Sub sort(A() As Long) Dim i As Long, j As Long For i = LBound(A) To UBound(A) - 1 For j = i + 1 To UBound(A) If A(j) A(i) Then swapI A(j), A(i) End If Next j Next i End Sub Sub swapI(x As Long, y As Long) Dim tmp As Long tmp = x x = y y = tmp End Sub Can anyone help with this please? -- hke ------------------------------------------------------------------------ hke's Profile: http://www.excelforum.com/member.php...fo&userid=1550 View this thread: http://www.excelforum.com/showthread...hreadid=319311 |
All times are GMT +1. The time now is 10:04 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com