View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Totte[_2_] Totte[_2_] is offline
external usenet poster
 
Posts: 1
Default Excel VBA Int, Cint?

Why differs the results from the function Int and CInt/CLng depending on the
value in this example?
E.g. see the values 6957 and 6960.

Private Sub CommandButton1_Click()
Dim tal As Double
Dim r As Integer

With ThisWorkbook.Sheets("Blad1")
tal = 69.5
r = 1
For i = 1 To 10
.Cells(r, 1) = "Value!"
.Cells(r + i, 1) = 100 * (tal + i / 100)
.Cells(1, 2) = "Int"
.Cells(r + i, 2) = Int(100 * (tal + i / 100))
.Cells(r, 3) = "Cint"
.Cells(r + i, 3) = CInt(100 * (tal + i / 100))
.Cells(r, 4) = "CLng"
.Cells(r + i, 4) = CInt(100 * (tal + i / 100))
Next i

tal = 169.5
r = 13
For i = 1 To 10
.Cells(r, 1) = "Value!"
.Cells(r + i, 1) = 100 * (tal + i / 100)
.Cells(r, 2) = "Int"
.Cells(r + i, 2) = Int(100 * (tal + i / 100))
.Cells(r, 3) = "Cint"
.Cells(r + i, 3) = CInt(100 * (tal + i / 100))
.Cells(r, 4) = "CLng"
.Cells(r + i, 4) = CInt(100 * (tal + i / 100))
Next i
End With
End Sub

Value! Int Cint CLng
6951 6951 6951 6951
6952 6952 6952 6952
6953 6953 6953 6953
6954 6954 6954 6954
6955 6955 6955 6955
6956 6956 6956 6956
6957 6956 6957 6957
6958 6958 6958 6958
6959 6959 6959 6959
6960 6959 6960 6960

Value! Int Cint CLng
16951 16951 16951 16951
16952 16952 16952 16952
16953 16953 16953 16953
16954 16954 16954 16954
16955 16955 16955 16955
16956 16956 16956 16956
16957 16957 16957 16957
16958 16958 16958 16958
16959 16959 16959 16959
16960 16960 16960 16960