+ Reply to Thread
Results 1 to 6 of 6

Loop through rows and generate new records

  1. #1
    Registered User
    Join Date
    09-21-2014
    Location
    DK
    MS-Off Ver
    MS 2013
    Posts
    3

    Loop through rows and generate new records

    First of all - great site. I have been reading through the forum for quite some time and have found many threads quite valuable. But it is time to create a thread for my own since I have not been able to find a discussion for my question.

    I am building a simplified re-order point system - if inventory position drops below a certain level (the yellow level is this case) one or more purchase order lines has to be created in another worksheet.

    I have one worksheet with the following field and data:

    ItemId....Red....Yellow....Green....Multiple....Inventory position

    0001.... 10.... 30.... 50.... 5.... 45

    0002.... 5.... 40.... 47.... 5.... 23

    0003.... 11.... 20.... 30.... 10.... 5

    I would like to generate new rows (in another worksheet) based on the above fields and three rows.

    Basically the end result should look as the following:

    ItemId....Qty....Start inv....Aggregated inventory....Prioritization

    0002.... 5.... 23.... 28.... Yellow

    0002.... 5.... 28.... 33.... Yellow

    0002.... 5.... 33.... 38.... Yellow

    0002.... 5.... 38.... 43.... Green

    0002.... 5.... 43.... 48.... Green

    0003.... 10.... 5.... 15.... Red

    0003.... 10.... 15.... 25.... Yellow

    0003.... 10.... 25.... 35.... Green

    The logic is quite simple - if inventory position is less than the yellow value new order lines should be created in multiple qty (based on the multiple field) until the aggregated value (in worksheet 2) is above the green value.

    The priotization value should be based on the start inv (in ws 2) compared to the values in red, yellow and green in ws 1.

    It would be much valuable if someone could guide me in the right direction towards an appropriate VBA design. I have found some code online which could solve the frame structure but I am looking for what to place into the middle of the code section.

    Please Login or Register  to view this content.

    I am looking forward some great answers.

    Much appreciated - thank you in advance.
    Last edited by alansidman; 09-21-2014 at 08:56 AM. Reason: code tags added

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2405 Win 11 Home 64 Bit
    Posts
    23,897

    Re: Loop through rows and generate new records

    Code Tags Added
    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found at http://www.excelforum.com/forum-rule...rum-rules.html



    (Because you are new to the forum, I have added them for you today. Please take a few minutes to read all Forum Rules and comply in the future.)
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    09-21-2014
    Location
    DK
    MS-Off Ver
    MS 2013
    Posts
    3

    Re: Loop through rows and generate new records

    Sorry for not complying to the rules - were not aware of this. :-)

    Have now read the rules and will comply to this going forward.

    Thanks for notifying me.

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Loop through rows and generate new records

    Hi JessPedersenSCM and welcome to ExcelForum,

    Try the attached file with the code also in this post. I think I misunderstood the colors, because my numbers match your numbers, but one of my colors seems to be incorrect.

    Lewis

    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    09-21-2014
    Location
    DK
    MS-Off Ver
    MS 2013
    Posts
    3

    Re: Loop through rows and generate new records

    Fantastic!! :-)

    This is really awesome - thank you so much for the help!

    Actually the color coding in my example was not correct - it was actually row No. 4 in the example which caused the issue, since it has to be yellow instead of green.

    But luckily I was able to change a few lines of code which made it work - actually the Inventory/Reorder position which is now calculated after the color coding settings.

    But I could not have done it without the complete solution which you provided - thank you, it is truly appreciated.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    09-21-2014
    Location
    DK
    MS-Off Ver
    MS 2013
    Posts
    3

    Re: Loop through rows and generate new records

    Just a follow-up question.

    I am actually a bit unsure what the TraverseInventoryList() does? Is it just to give the overall result in the VBA immediate window for debugging / search reasons?

  7. #7
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Loop through rows and generate new records

    I'm glad everything worked out for you, especially since you were able to make changes yourself.


    I am actually a bit unsure what the TraverseInventoryList() does? Is it just to give the overall result in the VBA immediate window for debugging / search reasons?
    Yes. The 'Immediate Window' is a great tool for debugging.

    Since it seems like you are new to VBA, it is for demonstration purposes, to show how I would read a list of data. Step one in my solution to your problem, was being able to read your data. I thought it might be helpful to you in the future.

    Here a few debugger tips which may also be helpful to you in the future:
    a. Press 'F8' to single step (goes into subroutines and functions).
    b. Press SHIFT 'F8' to single step OVER subroutines and functions.
    c. Press CTRL 'F8' to stop at the line where the cursor is.
    d. 'Left Click' the margin to the left of a line to set (or clear) a BREAKPOINT.
    e. Press CTRL 'G' to open the IMMEDIATE WINDOW. 'debug.print' statements send their
    output to the IMMEDIATE WINDOW.
    f. Select View > Locals to see all variables while debugging.
    g. To automatically set a BREAKPOINT at a certain location put in the line:
    'Debug.Assert False'
    h. To conditionally set a BREAKPOINT at a certain location put in lines similar to:
    if i >= 20 and xTV20 > 99.56 then
    Debug.Assert False
    endif
    i. A variable value will be displayed by putting the cursor over the variable name.

    Lewis

+ 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. [SOLVED] VBA to rearrange 11000 records into 550 rows (20 records combined into a single row)
    By Wedge120 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-27-2014, 11:56 PM
  2. [SOLVED] Generate List of Records that Match Multiple Criteria from Data Table
    By trandle in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 03-10-2014, 04:43 PM
  3. [SOLVED] Re: Loop through the rows and extract unique email id's to generate email drafts
    By spiwere in forum Excel Programming / VBA / Macros
    Replies: 28
    Last Post: 01-28-2014, 04:12 AM
  4. Loop to generate TextBox
    By Phoenix5794 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-11-2012, 09:36 AM
  5. How to generate lottery numbers from past records
    By icongene in forum Excel General
    Replies: 5
    Last Post: 06-24-2012, 06:21 AM

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