Consulting

Results 1 to 8 of 8

Thread: Solved: Why does res value stay at 1?

  1. #1
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location

    Solved: Why does res value stay at 1?

    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 ?

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    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.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location
    Yep, count1 is worth 25.

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    When I tested it with count1 = 6 and Cells(5,9) = 3 I got res1 changing fine.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location
    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

  6. #6
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location
    what i mean is that for some reason, excel consider the cells are text and not numbers, any idea on how to change that ?

  7. #7
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location
    or maybe because some numbers have a negative value ...

  8. #8
    VBAX Regular
    Joined
    Sep 2010
    Posts
    10
    Location
    solved. i declared "res" as long instead of single.
    thanks for your help.
    have a good day/night.
    xoxo

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •