+ Reply to Thread
Results 1 to 18 of 18

Looping through a code to get the value of the counter

  1. #1
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Looping through a code to get the value of the counter

    Hello, I need help with my code, it is supposed to sum column G if column B are values contained in list "aircrafts", and if the date in column A is equal to today-90 days.
    I put a counter "i", So the code is supposed to loop through the calculation to "myvar" and stops when myvar< 3:00. When it stops, it should return a msgbox with the value of i.
    This code is returning a type mismatch error.
    Any help?


    Please Login or Register  to view this content.
    Attached Files Attached Files

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    This code doesn't error, and it produces a result - not sure if it's the right one though.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  3. #3
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    Thanks Norie, it does not return an error, but it is always returning 1. In this sample, the answer should be 79.
    Last edited by chiidzzz; 01-29-2013 at 08:56 AM.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    Why should it return 79?

    There's only 4 records on the sheet 'pilot1'.

    The values in column G are all less than 3:00.

  5. #5
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    If you try this formula in a random cell,
    Please Login or Register  to view this content.
    the answer will be 2:00 which is less than 3:00 (date2)
    so "i" in this case is 79...
    I hope I am clear enough.

  6. #6
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    That isn't the formula in the code.

  7. #7
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    I showed you that the code result in the sample must be 79.
    The code you wrote always return 1, disregarding the records on the sheet 'pilot1' and the values in column G.

  8. #8
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    I'm confused.

    If the formula you posted gives the result you want, why not use it?

    Also, why are you looping?

    On the first iteration of the loop this is the formula that's evaluated.
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  9. #9
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    The purpose of this code is to calculate after how many days "i", the sum of the hours flown in the last 90 days will be less than 3:00.
    This is why I want in the reply in the msgbox to be the last value of "i", where the criteria is met (myvar < date2).

  10. #10
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    Am I making myself clear enough? or should I explain more?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    Have I been clear in explaining what's happening in the code?

    If you wanted to evaluate this formula in the code,
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    you would need this.
    Please Login or Register  to view this content.

  12. #12
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    sorry Norie, this is not what I mean.
    The number 79 applies only in this example.
    When I will make more entries , this number will change.
    You said On the first iteration of the loop this is the formula that's evaluated
    Please Login or Register  to view this content.
    OK. But it should not stop because the criteria is still not met. In this case myvar = 3:00 which is not less than date2.
    So the code should continue and the second iteration should be
    Please Login or Register  to view this content.
    Also in this case, the criteria is still not met...
    So it will continue until "i" will become 79 in this case
    Please Login or Register  to view this content.
    where the condition will be true: myvar(2:00) < date2 (3:00)

  13. #13
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    The value of "i" represents after how many days myvar will be less than 3:00
    This is the logic I am trying to achieve, knowing that the syntax is all wrong.
    Please Login or Register  to view this content.

  14. #14
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    The value of "i" represents after how many days myvar will be less than 3:00
    This is the logic I am trying to achieve, knowing that the syntax is all wrong.
    Please Login or Register  to view this content.

  15. #15
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644
    I think there might be a problem comparing the result of the formula to date2.

    They appear to be the same but VBA isn't treating then as such.

    I don't know why that is but I'll check it out.

  16. #16
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    Thank you !!

  17. #17
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Looping through a code to get the value of the counter

    I've incorporated the comparison in the formula being evaluated.

    The formula now returns a boolean value which we use to determine when to end the loop.

    When I run it I get 79, but you should check it out with other data.
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    07-08-2012
    Location
    beirut
    MS-Off Ver
    Excel 2010
    Posts
    102

    Re: Looping through a code to get the value of the counter

    Thank you Norie, it works and I tried it with different data. Much appreciated.

+ 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