ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Problem with 'For' loop execution [VBA,Excel XP] (https://www.excelbanter.com/excel-programming/304720-problem-loop-execution-%5Bvba-excel-xp%5D.html)

Angelos Markos

Problem with 'For' loop execution [VBA,Excel XP]
 
I'm a VBA begginer and i've recently faced the following problem:

Suppose we have the following sub:

Sub test()
Dim i
For i = 0.6 To 0.8 Step 0.01
MsgBox (i)
Next i
End Sub

Surprisingly, in the above loop the last value (0.8) does not appear.

On the contrary, in the following sub (step is 0.1 instead of 0.01)

Sub test2()
Dim i
For i = 0.6 To 0.8 Step 0.1
MsgBox (i)
Next i
End Sub

everything is ok.
How do you explain that?

--
With respect,
A.M.

Don Guillett[_4_]

Problem with 'For' loop execution [VBA,Excel XP]
 
try to .81

--
Don Guillett
SalesAid Software

"Angelos Markos" wrote in message
m...
I'm a VBA begginer and i've recently faced the following problem:

Suppose we have the following sub:

Sub test()
Dim i
For i = 0.6 To 0.8 Step 0.01
MsgBox (i)
Next i
End Sub

Surprisingly, in the above loop the last value (0.8) does not appear.

On the contrary, in the following sub (step is 0.1 instead of 0.01)

Sub test2()
Dim i
For i = 0.6 To 0.8 Step 0.1
MsgBox (i)
Next i
End Sub

everything is ok.
How do you explain that?

--
With respect,
A.M.




Tom Ogilvy

Problem with 'For' loop execution [VBA,Excel XP]
 
http://www.cpearson.com/excel/rounding.htm

I expect you are getting accumulated rounding error.
--
Regards,
Tom Ogilvy

"Angelos Markos" wrote in message
m...
I'm a VBA begginer and i've recently faced the following problem:

Suppose we have the following sub:

Sub test()
Dim i
For i = 0.6 To 0.8 Step 0.01
MsgBox (i)
Next i
End Sub

Surprisingly, in the above loop the last value (0.8) does not appear.

On the contrary, in the following sub (step is 0.1 instead of 0.01)

Sub test2()
Dim i
For i = 0.6 To 0.8 Step 0.1
MsgBox (i)
Next i
End Sub

everything is ok.
How do you explain that?

--
With respect,
A.M.





All times are GMT +1. The time now is 01:35 PM.

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