+ Reply to Thread
Results 1 to 11 of 11

Dim integer in For loop does not work, why?

  1. #1
    Registered User
    Join Date
    11-28-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    75

    Dim integer in For loop does not work, why?

    Hello,

    I dimensioned Jan as Integer and made it = 31

    In a For loop I said: For j=1 to Jan <--Does not work! The macro does not even run!
    When I say instead: For j=1 to 31 <--Does work! The macro runs and everything works!

    Why is that?

  2. #2
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: Dim integer in For loop does not work, why?

    hi 123wc, can you post your code?

    Basing on your explanation the following code runs without any troubles :

    Please Login or Register  to view this content.
    The only way for the code not to enter the loop is to comment out this line of the code:
    Please Login or Register  to view this content.
    thus making variable Jan being equal to zero.

  3. #3
    Registered User
    Join Date
    11-28-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Dim integer in For loop does not work, why?

    Thanks man, I found the problem: I was declaring many variable in one line as this

    Dim Jan, Feb, Mar....as Integer <--wrong

    This is the correct way it seems: Dim Jan as Integer, Feb as Integer, Mar as Integer.... <--correct

  4. #4
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Dim integer in For loop does not work, why?

    You have identified the correct way to declare the variables (otherwise they end up being declared as Variants). however, I would be surprised if this alone was responsible for causing your loop not to run. What was your entire code before you fixed it?

  5. #5
    Registered User
    Join Date
    11-28-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Dim integer in For loop does not work, why?

    The other mistake I did is that I initialized the variables the incorrect way too. eg:

    Jan, Mar, May...=31 <--wrong
    Jan=31: Mar=31: May=31 <--correct

  6. #6
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Dim integer in For loop does not work, why?

    Ah I see - thank you for the clarification

  7. #7
    Registered User
    Join Date
    11-28-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Dim integer in For loop does not work, why?

    I don't want to start a new thread, so can you help me with summation?

    Here is what I want to do:

    X5 = SUM(G58:N58)
    X5_offset1 = SUM(G58 +i: N58 +i) 'So this becomes G59:N59
    X5_offset2 = SUM(G58 +i: N58 +i) 'So this becomes G60:N60

    I know how to do the offset, and I know how to declare i and increment it. I just don't know how to do the summation in VBA! I also want the cells (X5, or whatever) to display the whole summation formula, ie:
    = SUM(G58: N58)
    = SUM(G59: N59)
    etc.

    I don't want the cells to display the summation value only.

    Any ideas?

    Thanks a lot

  8. #8
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Dim integer in For loop does not work, why?

    Sounds to me like you just need:

    Please Login or Register  to view this content.
    Adjust the X5:X10 to be the whole range you want to enter the formula into. Excel will automatically adjust the SUM() formula ranges to reflect the increasing rows

  9. #9
    Registered User
    Join Date
    11-28-2011
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2007
    Posts
    75

    Re: Dim integer in For loop does not work, why?

    That works too. Thanks a lot

    But sometimes I can't tell easily how far the loop is going so it's easier to just put the summation in the loop and it adds as the loop goes on.

  10. #10
    Valued Forum Contributor
    Join Date
    07-21-2008
    Location
    London, UK
    Posts
    326

    Re: Dim integer in For loop does not work, why?

    This might be clearer, VB coders use that method and generally Excel causes issues

    Try:
    Please Login or Register  to view this content.
    Last edited by Jack in the UK; 12-30-2011 at 12:37 PM. Reason: Spellings :-)

  11. #11
    Forum Contributor
    Join Date
    12-28-2011
    Location
    England
    MS-Off Ver
    Excel 2007
    Posts
    280

    Re: Dim integer in For loop does not work, why?

    Quote Originally Posted by 123wc View Post
    That works too. Thanks a lot

    But sometimes I can't tell easily how far the loop is going so it's easier to just put the summation in the loop and it adds as the loop goes on.
    You can use the FormulaR1C1 property instead:

    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1