PDA

View Full Version : Solved: Why does res value stay at 1?



jrm
09-21-2010, 05:24 PM
Hi,

I'm a C programmer, very new to VBA.

Would you help me with this very simple line of code, what's wrong ?

res = 1

For count2 = 5 To (count1 - 2)
res = res * (Cells(count2, 9).Value + 1)
Next count2


Why on earth does res stay at value 1 ? :(

Bob Phillips
09-22-2010, 04:20 AM
Have you initialised Count1? If you don't initialise it to a value greater than 6 the loop will never execute.

It will also stay at1 if the cell is empty.

jrm
09-22-2010, 05:14 AM
Yep, count1 is worth 25.

Bob Phillips
09-22-2010, 06:06 AM
When I tested it with count1 = 6 and Cells(5,9) = 3 I got res1 changing fine.

jrm
09-22-2010, 06:23 AM
got it. in my column I are the results of a previous calculation (MoM return) and for some reason, VBA consider them as 0 ....? any idea how to change that or where it comes from ?

here is the code that generates the number in column I :

For count2 = 4 To count1 - 2
Cells(count2 + 1, 9).Value = -1 + Cells(count2 + 1, 8) / Cells(count2, 8).Value
Next count2

jrm
09-22-2010, 07:55 AM
what i mean is that for some reason, excel consider the cells are text and not numbers, any idea on how to change that ?

jrm
09-22-2010, 08:01 AM
or maybe because some numbers have a negative value ...

jrm
09-22-2010, 08:24 AM
solved. i declared "res" as long instead of single.
thanks for your help.
have a good day/night.
xoxo