+ Reply to Thread
Results 1 to 15 of 15

simple loop to run until a date is hit?

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    simple loop to run until a date is hit?

    Hi all.

    How would I go about writing a loop that can run down rows until it hits, for ex 1/1/2010 (dates are in column A).


    Also, would I be able to make a loop run for 30 rows and perform some calculations with two different columns?

  2. #2
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    So I would like a loop to run (either for a predefined number of rows or until it hits a certain date - in column A) ... and do the following:

    There will be positive and negative values in Column E. So every time a loop sees a positive value, it should add it all up...and when it sees a negative value it should add all of those up as well, and then put those two numbers wherever on the sheet.

    So the input is a bunch of negative and positive numbers, and the output is two numbers: a sum of positive, and sum of negative numbers from that bunch.

    Is this possible?

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: simple loop to run until a date is hit?

    You don't need a macro for that. A formula should probably be able to do that.

    Click GO ADVANCED and use the paperclip icon to post up your workbook. Point out where you want the SUM of positive and SUM of negative numbers to appear.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    okay, i uploaded the file and instructions are on the same sheet.

    i am looking for a VBA code as it will be easier to play around with. you will see what i mean when u see the workbook.

    thank you in advance!!
    Attached Files Attached Files

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: simple loop to run until a date is hit?

    This should show you what I mean. This isn't hard at all. Not sure if I summed the right column, but you can adjust that.
    Attached Files Attached Files

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: simple loop to run until a date is hit?

    Or, in N3, =SUMIF(INDEX(H:H, N$2):INDEX(H:H, N$2+N$3-1), ">0")

    In N4: =SUMIF(INDEX(H:H, N$2):INDEX(H:H, N$2+N$3-1), "<0")
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    The first line works, for N3. The second for N4 doesn't.

    Is there any way we can shorten the code so we can just say "30", instead of telling it to sum up rows 1 to 31 ?

    Thank you for your help! This is exactly what I was looking for!

  8. #8
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    can this be done in VBA by any chance? I think it would be a lot easier as you can have a textbox to enter value (for however many rows you want the loop to run)

    any thoughts?

  9. #9
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    This is what I have for VBA code. It doesn't work though. Any help would be greatly appreciated guys.

    Please Login or Register  to view this content.

    Okay, I've done more testing and apparently I can't define Range as
    Please Login or Register  to view this content.
    but as
    Please Login or Register  to view this content.
    And I don't think i am setting the change.Value correctly. I am trying to tell it to be greater than 0, but I don't think it is correct.

    Also, how would I be able to tell the loop to stop after lets say 30 rows? 60? Instead of running through the whole sheet.
    Last edited by losmi8; 02-06-2010 at 09:23 PM.

  10. #10
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    And here is the file I am working with. Then you'll know what is what in my code.
    Attached Files Attached Files

  11. #11
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    Okay, I got the code to work but it is not what I want. So here is the working code:

    Please Login or Register  to view this content.
    "side" is there just so i can control for how many rows the code runs. Is that the correct way of doing it? It seems to work...



    Now, the code works ONLY if there is a "75" somewhere in Column H (or any other whole number i enter)

    However, to make this code perfect, I would want it to, instead of "75", look for values that are GREATER than 0.


    Any help would be appreciated guys. Thanks!
    Last edited by losmi8; 02-06-2010 at 09:22 PM.

  12. #12
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    okay, i solved the issue. there was some text in the first row that didn't allow me to do >=0

    thanks for all that helped!

  13. #13
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: simple loop to run until a date is hit?

    guys one more thing, how can i get the number of the positive days the code adds up as volume?

    i tell it to run for 30 rows....and in those 30 rows, lets say there were 12 positive values, which the code adds up....but how do i get that 12? i want that to be stored on the sheet somewhere. i dont want to manually count.

    i need that 12 because i want to calculate the average postive value.

  14. #14
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: simple loop to run until a date is hit?

    ...how can i get the number of the positive days the code adds up as volume?

    Please Login or Register  to view this content.
    Ben Van Johnson

  15. #15
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,629

    Re: simple loop to run until a date is hit?

    Here's the code to include the counts. Note that I don't know where you want them place so you will have to edit the code where indicated
    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