ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Can Step through but when run defines variable wrong (https://www.excelbanter.com/excel-programming/371802-can-step-through-but-when-run-defines-variable-wrong.html)

sportzrule

Can Step through but when run defines variable wrong
 
Hi, thanks for helping

When I step through the following code (lots of junk removed so it
should be easier to read) it works fine, however when I run it it works
fine until i = 527 then I get an error, and TimetoPM's value is wrong.
It comes out negative even when the formula makes it positive?

For i = 1 To LoopNumber Step 1
Ttotal = 0
Tevent = 0
WeightedAge = 0
y = 1
x = 0
Do
If WeightedAge = 0 Then
Tf = Alpha * ((WeightedAge / Alpha) ^ Beta - Log(1 - Rnd))
^ (1 / Beta) - WeightedAge
TimeToPM = (Period * y - Ttotal)
If Tf < TimeToPM Then 'If (Time to fail) < (Time of next
SM - Time of Last Event)
'stuff in here but no defining of TimetoPM
Else
'more stuff but TimetoPM is not changed
WeightedAge = WeightedAge + TimeToPM * (1 -
RestoreFactor)
End If
y = y + 1 'update SM multiple counter for System
Time option
End If
Ttotal = Tevent + Tm
Else
x = 1
End If
Loop While x = 0
Next


JLGWhiz

Can Step through but when run defines variable wrong
 
It looks to me that if yoour Ttotal is greater than y or if RestoreFactor is
greater than 1 then TimetoPM will be negative. If that is the case, then you
can put an error handling "If ... then" in there to accomodate it or use "on
error next" to bypass it, depending on what you expect as a result.

"sportzrule" wrote:

Hi, thanks for helping

When I step through the following code (lots of junk removed so it
should be easier to read) it works fine, however when I run it it works
fine until i = 527 then I get an error, and TimetoPM's value is wrong.
It comes out negative even when the formula makes it positive?

For i = 1 To LoopNumber Step 1
Ttotal = 0
Tevent = 0
WeightedAge = 0
y = 1
x = 0
Do
If WeightedAge = 0 Then
Tf = Alpha * ((WeightedAge / Alpha) ^ Beta - Log(1 - Rnd))
^ (1 / Beta) - WeightedAge
TimeToPM = (Period * y - Ttotal)
If Tf < TimeToPM Then 'If (Time to fail) < (Time of next
SM - Time of Last Event)
'stuff in here but no defining of TimetoPM
Else
'more stuff but TimetoPM is not changed
WeightedAge = WeightedAge + TimeToPM * (1 -
RestoreFactor)
End If
y = y + 1 'update SM multiple counter for System
Time option
End If
Ttotal = Tevent + Tm
Else
x = 1
End If
Loop While x = 0
Next




All times are GMT +1. The time now is 09:52 PM.

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