+ Reply to Thread
Results 1 to 10 of 10

Loop through table and add rows

  1. #1
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Loop through table and add rows

    Hi,

    Stuck on getting VBA to do something! I have a form with some values that need to be added to a table. On clicking the command button I need it to first add a new row into table procStep using the values in the form. Then, I need to loop through all the cells in the Product Name column of that table and check some conditions, if the conditions are met then add a new row to a different table called resTable.

    I'm using the code below.

    Please Login or Register  to view this content.
    What's happening is that it is adding the new row to procStep table with the form values ok, but then it's just adding one blank row to the resTable table when there should be 4 rows that meet the conditions given.

    Apologies if the code's a bit messy, VBA is not my forte!!

    Any help would be appreciated though

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Loop through table and add rows

    You don't appear to be increment in resRow within the loop. It looks as though it is set once before entering the loop.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Re: Loop through table and add rows

    Yes, it's set as resTabFull.ListRows.Add. I thought that would add a new row onto the table for each cell that met the conditions? Must be where I'm going wrong but I'm stuck as to how to increment?

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Loop through table and add rows

    Just do what you've already done but within the loop. Or use resRow = resRow + 1

  5. #5
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Re: Loop through table and add rows

    Ah ok, I think I understand. So, I've changed it to the below but now it's adding a new row to procStep with the correct values but then it just adds 20 blank rows to the resTable! (There are 20 rows in the procStep table so something isn't working right with the If statement?)

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Re: Loop through table and add rows

    Actually I realise the Set resRow line should be after the IF Then part. But it won't add any rows in, more than likely because of the If statement. I'll double check it all but can't understand why it isn't working

  7. #7
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Re: Loop through table and add rows

    Jeez, finally getting somewhere! Thanks for the help

    I'm having an issue now with the part of the If statement that says If cell.Offset(0,1) = materialCode.Value The cell it's looking at on the sheet is a number format and it's comparing it to a text box value on the form. Doesn't seem to like it! So brings up a false for every row. If I remove that condition the macro works as it should, but unfortunately I really need that condition!

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Loop through table and add rows

    Try:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-14-2021
    Location
    N.Ireland
    MS-Off Ver
    2016
    Posts
    6

    Re: Loop through table and add rows

    Ah! So simple! Love it

    All working now, thanks for your help!

  10. #10
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,454

    Re: Loop through table and add rows

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ 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] How to best loop through a table's rows
    By brucemc777 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-23-2020, 11:24 AM
  2. [SOLVED] Loop through Rows in a table and add data to a Chart
    By jgoodz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-27-2016, 11:50 PM
  3. Help on how to Loop Copy Paste to End of the Table VBA/Macro that do two rows only
    By jomapac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-17-2013, 12:18 PM
  4. [SOLVED] Loop through rows/columns H and K in filtered table and send emails - Help Please Gurus ;)
    By Alina1404 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-30-2012, 03:23 AM
  5. Loop a code using the values of various columns and rows within a table
    By TommyToe in forum Excel - New Users/Basics
    Replies: 1
    Last Post: 01-17-2012, 06:01 PM
  6. Auto loop pivot table rows at a time
    By johnheng82 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-01-2011, 12:07 AM
  7. Loop rows in pivot table
    By johnheng82 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 11-17-2010, 07:20 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