Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For-Next not going to completion

Status
Not open for further replies.

Greedo

Technical User
Nov 17, 2002
19
JP
I have a for next loop that is supposed to go from 1 to 4 but stops at 1.5. If anyone can see anything wrong I would appreciate hearing about it.



Dim varOne(4) As Single
Static A(4, 8) As Double
Dim Total As Double
Static varTwo(4, 8) As Double
Dim Max As Double



For D = 1 To 4 Step 1

A(D, D) = varOne(D) * varTwo(D, D) * varThree
A(D, D + 1) =varOne(D+1)*varTwo(D,D+1)*varThree
A(D, D + 2) =varOne(D+2)*varTwo(D,D+2)*varThree
A(D, D + 3) =varOne(D+3)*varTwo(D,D+3)*varThree

Total = A(D, D)+A(D,D+1)+A(D,D+2)+A(D,D+3)

If Total >= Max Then
Max = Total
End If

Next D
 
I have a couple of questions,

is OPTION EXPLICIT defined?
if it isn't I suggest that you do (requires explicit variable declaration)

Is this the complete code?

how is D defined
I woudl suggest that you define it
dim D as integer

as the count variable should only be integer

try commenting out all the code in the loop and adding in
debug.print D or msgbox cstr(d)

Try those and post results back

Matt
 
It says that Option Explicit can't be used here. Only in procedures or something.

This is not the complete code but it very nearly is. The variables are scanned in from Excel at the beginning and the output is... outputted back into Excel at the end. It looks something like:

Dimming of Variables
Scanning Variables In
*What you saw*
Outputting Variables

As far as debug.print D or msgbox cstr(d) is concerned, I am afraid I can't find it. My sub par programming skils have been teamed up with a Japanese only computer. If you could give ridiculously explicit directions I am willing to give it a shot.

Also, this might be important: it stops right before the "Total" line on the second time through the loop. Maybe that's where the problem is.
 
You have to put Option Explicit right at the top in the declarations. I have Option Explicit inserted automatically in all code, by going to Tools, Options and checking the box Require Variable Declaration in Code Settings.

The problem I get with your code is a Subscript Out of Range. It occurs when D=2 and I think it's because you dim varOne as 4; when D=2, you're trying to access varOne(5).

(That's when I comment out the Option Explicit and don't bother to Dim D.)

When I do this:

Code:
For D = 1 To 4 Step 1
Debug.Print D
'A(D, D) = varOne(D) * varTwo(D, D) * varThree
'A(D, D + 1) = varOne(D + 1) * varTwo(D, D + 1) * varThree
'A(D, D + 2) = varOne(D + 2) * varTwo(D, D + 2) * varThree
'A(D, D + 3) = varOne(D + 3) * varTwo(D, D + 3) * varThree
'
'  Total = A(D, D) + A(D, D + 1) + A(D, D + 2) + A(D, D + 3)
'
'  If Total >= Max Then
'            Max = Total
'            End If

Next D

as Matt suggests, it prints out the values 1,2,3 & 4 so the For..Next itself is OK.

Jim Brown,
Johannesburg,
South Africa.
My time is GMT+2
 
JimBrown

I didn't spot that!

Greedo

perhaps you could give us soem generic description of what you are trying to do with this routine. We would then be able to help...

Matt
 
Actually, with that last little detail everything is running smoothly. Jim- I'm actually glad you missed it too, I felt like a jackass sitting here by myself. Anyway, if anyone is interested, this is what the program should do (although, like I said I don't need anything else, this is just if you're curious).

I am trying to find out the maximum heat load due to sunlight for a car.

varOne is the solar intensity which changes based on what direction you're facing (in the morning there is more light coming from the east for example).

varTwo is the window dimension. the first subscript is the direction the car is traveling while the second is what direction the window is facing. So varTwo(1,2) is the window facing east when the car is traveling north, or, the right hand side.

varThree is the window transmission factor which is assumed to be equal for all windows.

The reason I do the calculation 4 times is because there are 4 possible directions the car could be going. I don't want to assume that the car will always be going in the direction that results in the highest heat load; I need to calculate it and see.

So Total (and consequently Max) are then outputted along with the direction of travel that results in that Max. Does that make sense?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top