+ Reply to Thread
Results 1 to 5 of 5

nested loops overwriting

  1. #1
    Registered User
    Join Date
    03-01-2019
    Location
    Mansfield
    MS-Off Ver
    2013
    Posts
    19

    nested loops overwriting

    Hi! I have written this piece of code below but the issue is that the values returned are only for the final year and the final day for each plant because they keep overwriting. I have tried to incorporate into a collection as string and then split but that returns the same values for year 1 and year 2 rather than performing the calculation again i.e value at plant 1, day 1 year 1 will be the same as plant 1 day 1 year 2. Even though i know where the problem seems to be, i'm not sure how to fix it. I'm really knew to VBA so any advice ould be greatly appreciated

    Thanks a lot!

    Please Login or Register  to view this content.
    Moderator's note: Please take the time to review our rules. There aren't many, and they are all important. Rule #2 requires code tags. I have added them for you this time because you are a new member. --6StringJazzer
    Last edited by 6StringJazzer; 04-14-2019 at 07:24 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: nested loops overwriting

    You are using p to index the array, but p is just the innermost loop, so you'll use the same slots for every year, every day. You need to take into account year and day as well, and you only need one loop, not three nested ones.

    You also need to specify that the first dimension starts at 1, unless you are using Option Base 1.

    I can't test this because you are not showing how you set npatches and patchsize, flowering, floweringSD. Also, you should declare year and day; better yet, name them something else. It works, but there are built-in functions called Day and Year and you don't want to tempt confusion. I strongly recommend to everyone that they declare variables. Doing so prevents a lot of bugs and runtime errors.

    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Expert JLGWhiz's Avatar
    Join Date
    02-20-2011
    Location
    Florida, USA
    MS-Off Ver
    Windows 10, Excel 2013
    Posts
    2,070

    Re: nested loops overwriting

    it is not over writing. The statement to enter the data is in the wrong place. See the edited version below.

    Please Login or Register  to view this content.
    Also, you need to change your 'Year' and 'Day' variables to something else. Both of those are Excel and VBA keywords that considered reserved. The code might run with them in use but results may be less than desirable. I woulr you something like 'yr' and 'dy' which still pretty much represents what you want.

    I see that @6StringJazzer went a little deeper into analysis than I did.
    Last edited by JLGWhiz; 04-14-2019 at 07:48 PM.
    Any code provided by me should be tested on a copy or a mock up of your original data before applying it to the original. Some events in VBA cannot be reversed with the undo facility in Excel. If your original post is satisfied, please mark the thread as "Solved". To upload a file, see the banner at top of this page.
    Just when I think I am smart, I learn something new!

  4. #4
    Registered User
    Join Date
    03-01-2019
    Location
    Mansfield
    MS-Off Ver
    2013
    Posts
    19

    Re: nested loops overwriting

    6StringJazzer I was not aware of the day and year built in functions. I have now renamed the variables so thank you for that! The code you have providiied doesn't seem to be working as a syntax error highlighting the Lbound(plants, 1) to Ubound(plants, 1) section appears. It's also worth mentioning that I did want the code to run the operations for each of the 30 plants for 80 days meaning that i would expect the values returned to be the same for plant 1 for 80 days until the operations begin for the next plant. After the operations are ran for all the 30 plants for 80 days each, next year begins (where i would like for the values to differ from the previous year). Apologise if that wasn't very clear from my thread.

    npatches = 3
    patchsize = 100
    flowering = 50
    floweringSD = 10

  5. #5
    Registered User
    Join Date
    03-01-2019
    Location
    Mansfield
    MS-Off Ver
    2013
    Posts
    19

    Re: nested loops overwriting

    JLGWhiz that doesn't seem to be working. Same thing happens :/

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 09-28-2018, 09:35 AM
  2. VBA Loops - Assign a BAD rating based on recurring instances without overwriting
    By olechkq in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-16-2018, 02:27 PM
  3. [SOLVED] Nested IF loops More than 7 Need Help
    By EricSomin in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2013, 02:25 PM
  4. [SOLVED] Help with nested loops
    By wishmaker in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-23-2013, 11:03 AM
  5. Nested loops VBA
    By zurich in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2012, 03:21 PM
  6. nested loops
    By jer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-15-2006, 05:15 PM
  7. [SOLVED] nested for loops and end for
    By SandyR in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-06-2005, 05:05 PM

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