ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Longs losing accuracy (https://www.excelbanter.com/excel-programming/286061-longs-losing-accuracy.html)

pikus

Longs losing accuracy
 
I just wrote the following code to convert hours and minutes to decimal
numbers. This is a test run to prepare for the real deal so it's kind
of ugly. The problem is that the variable "min" which is declaired as
a LONG is only giving me back whole numbers. What do I do to stop
that??? Thanks in advance - Pikus

Private Sub Test_Click()
Dim wd As String
Dim time As Long
Dim hr As Long
Dim min As Long

wd = "Saturday"

Do
y = y + 1
Loop Until Worksheets(wd).Cells(y + 1, 1).Value = ""

For x = 1 To y
Worksheets(wd).Cells(x, 4).Value = "=HOUR(C" & x & ")"
hr = Worksheets(wd).Cells(x, 4).Value
Worksheets(wd).Cells(x, 5).Value = "=MINUTE(C" & x & ")"
min = Worksheets(wd).Cells(x, 5).Value
min = min / 60
Worksheets(wd).Cells(x, 6).Value = min
time = hr + min
Worksheets(wd).Cells(x, 7).Value = time
Next x

End Sub


---
Message posted from http://www.ExcelForum.com/


Vasant Nanavati

Longs losing accuracy
 
Long is an expanded form of Integer. For greater precision you need to use
Single or Double.

--

Vasant


"pikus" wrote in message
...
I just wrote the following code to convert hours and minutes to decimal
numbers. This is a test run to prepare for the real deal so it's kind
of ugly. The problem is that the variable "min" which is declaired as
a LONG is only giving me back whole numbers. What do I do to stop
that??? Thanks in advance - Pikus

Private Sub Test_Click()
Dim wd As String
Dim time As Long
Dim hr As Long
Dim min As Long

wd = "Saturday"

Do
y = y + 1
Loop Until Worksheets(wd).Cells(y + 1, 1).Value = ""

For x = 1 To y
Worksheets(wd).Cells(x, 4).Value = "=HOUR(C" & x & ")"
hr = Worksheets(wd).Cells(x, 4).Value
Worksheets(wd).Cells(x, 5).Value = "=MINUTE(C" & x & ")"
min = Worksheets(wd).Cells(x, 5).Value
min = min / 60
Worksheets(wd).Cells(x, 6).Value = min
time = hr + min
Worksheets(wd).Cells(x, 7).Value = time
Next x

End Sub


---
Message posted from http://www.ExcelForum.com/




pikus

Longs losing accuracy
 
DOH!

Damn. You're right.

I feel like an idiot.

Thanks very much. - Pikus:confused:


---
Message posted from http://www.ExcelForum.com/



All times are GMT +1. The time now is 07:13 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com