I don't understand why my simple loop in vba not working as I want.
I would like to make a diagram and loop through the x axis values with a step = 0,01. I cut out part of the code, probably not necessary for this question.
My problem is that the generated values are not exact. It should be something like: 0; 0,01; 0,02; ... and so on but around 0,82 the cell value is: 0,820000000000001 and after that the error goes on.
This little bit of difference between 0,82 and 0,820000000000001 is not much but makes a lot of problem for me. For example:
in the loop I can not use this:
If x = 0.82 ...
because it is not going to be equal. I can cure this problem but there are others and maybe there is a simpler solution.
Here is the code:
Sub Generate_Punch()
Dim NoL As Integer
NoL = Worksheets("Punch").Range("D5").Value 'Number of loops
Dim d As Double
d = Worksheets("Punch").Range("D4").Value 'value of increment/step
'which line start
Dim i As Integer
i = 15
'Coordinate values:
Dim x, y As Double
x = 0
y = 0
Worksheets("Punch").Range("A" & i & ":F32767").Clear
For j = 1 To NoL
'Counter
Worksheets("Punch").Cells(i, 1).Value = j
'X coordinate values:
Worksheets("Punch").Cells(i, 2).Value = x
'.... .... ....
i = i + 1
x = x + d
Next j
End Sub
Thank you for your answers. Best regards,
Robert