CptGoodar
CptGoodar

Reputation: 303

Why does my for loop not step up?

This is likely to be a simple issue that I have overlooked. I have a for loop that is refusing to step up with the given interval. The code below is an extract from a more complex code. My program was running for 1h, with no change and I manged to narrow it down to this segment that was causing the large run time.

I have tried running this code in its own module and the same issue happens. I have other for loops within the main bulk of code that have no issue with step it's just this one. Any help in finding where I've gone wrong would be greatly appreciated.

Sub Test()


Dim h As Long
Dim BiezerP0 As Double
Dim BiezerP1 As Double
Dim BiezerP2 As Double
Dim BiezerP3 As Double
Dim BiezerValue As Double
Dim BiezerStepInterval As Double
Dim BiezerLocation As Double
Dim P0Time As Double

BiezerStepInterval = 0.167


            For h = 0 To 1 Step BiezerStepInterval

                BiezerP0 = ((1 - h) ^ 3) * P0
                BiezerP1 = 3 * ((1 - h) ^ 2) * h * P1
                BiezerP2 = 3 * (1 - h) * h * h * P2
                BiezerP3 = h * h * h * P3

                BiezerValue = BiezerP0 + BiezerP1 + BiezerP2 + BiezerP3

                BiezerLocation = h * BiezerSteps
                Cells(BiezerLocation + 4, 15) = BiezerValue
                Cells(BiezerLocation + 4, 16) = P0Time + (0.02 * BiezerLocation)


            Next h
End Sub

The only issue I can think of causing this is the fact 0.167 doesn't go directly into 1. Would that affect it?

Upvotes: 1

Views: 56

Answers (2)

ThunderFrame
ThunderFrame

Reputation: 9471

As Magnetron said, h should be a Double, the reason being that assigning 0.167 to a Long will result in a Long value of 0 - as can be seen in the Immediate window:

?Clng(0.167)
 0 

Adding 0.167 to the 0, and assigning the result to a Long, will again result in a Long value of 0....

Rinse... Repeat...

That's why the variable won't increment. But change the variable type to a Double and you won't lose the precision.

Upvotes: 2

Magnetron
Magnetron

Reputation: 8573

h should be a Double, not long

Dim h As Double

Upvotes: 4

Related Questions