+ Reply to Thread
Results 1 to 11 of 11

vba to loop thru a table and update another worksheet

  1. #1
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    vba to loop thru a table and update another worksheet

    Hi folks, I have provided an example workbook of the help I am asking for. I have a form (sheet8) that I input information, then I have code in module 1 that runs when called and copies data off of sheet8 onto sheet1. What I am looking for is there a better way than all of my "IF" statements? I only did for the first four lines of the form on sheet8, but there is 15 lines and three more worksheets with the same form. I numbered them in order, but that may not and usually is never the case. So I was wondering if someone with more knowledge than I could help out with this? Much appreciated and thanks!
    Attached Files Attached Files
    Last edited by thecdnmole; 12-29-2018 at 05:58 PM.

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: vba to loop thru a table and update another worksheet

    Hi thecdnmole,

    Try this:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: vba to loop thru a table and update another worksheet

    It would make more sense if the sheet1 columns were in exactly the same order as your sheet8 data.

    This boils down to filtering the data to show all the 'Pass' records. Then assuming the columns are in the same order using code like. Unless there's a particular reason to use looping code then always use filtering for this sort of stuff. Looping code carroes a big time overhead. Particularly where many rows are involved.

    Please Login or Register  to view this content.
    However you have committed the cardinal sin and have used merged cells on row 13 for the column header labels.
    Most of us avoid merged cells like the plague. They just cause too much trouble and will catch you out in all sorts of ways. This is one such example and means you can't use the extremely efficient and simple filtering technique to grab some data and put it somewhere else in just a few lines of code. There is absolutely no benefit in having merged cells here. You can always use cells format 'center across selection' to achieve the same look.

    However the column labels immediately above your data should always be unmerged

    If you adjust your form so that each field is in a single column then reupload it will then be a trivial task to add the code above. If you want the column labesls in a different position on sheet1 then an Advanced Data Filter would be required
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: vba to loop thru a table and update another worksheet

    Thanks Robert, I gave it a try and works pretty good, but it is copy/pasting the row that failed. It must skip over that row and only copy the rows that pass. Can this be done?

    Richard, I will unmerge the cells and upload a new version. Thanks.

  5. #5
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: vba to loop thru a table and update another worksheet

    As per Richard's request, cells are now unmerged.
    Attached Files Attached Files
    Last edited by thecdnmole; 12-29-2018 at 08:29 PM.

  6. #6
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: vba to loop thru a table and update another worksheet

    That wasn't quite the end of the merged cells matter. The row 13 column labels shpuld have been a contiguous range.
    I've removed all the blank columns in the attached. You may need to tidy up the stuff at the top. As I said try and avoid merged cells ANYWHERE.

    This Autofilter will copy exactly the same layout.

    If you wanted to limit the output to a few column headers then an Advanced Filter would be needed.
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: vba to loop thru a table and update another worksheet

    but it is copy/pasting the row that failed. It must skip over that row and only copy the rows that pass. Can this be done?
    Not really sure what you mean

    The original code looked up what row number to paste via a match formula which there is only one value in Sheet1. I didn't change that part of the code.

    As Richard's code presumably doesn't loop as it uses a filter, his would be the preferred method in any case.

    Robert

  8. #8
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: vba to loop thru a table and update another worksheet

    Hi Robert, your code was copying the line with the failed hose (row 16), but I only want the ones that passed copied. I was able to modify your code to make this work. Thanks.

    Sorry Richard, I cannot copy and past the block of cells because of where they are located on the master list as one could be next in line or be 50 lines down. Maybe not as efficient, but the code Robert supplied is much more what I was wanting and with a bit of tweaking, works.

    Thanks to both of you for helping with this and yes Richard, any new forms I make I will try and avoid merging cells!

  9. #9
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: vba to loop thru a table and update another worksheet

    Ah, now I see what you mean. So you changed this line of code...

    Please Login or Register  to view this content.
    ...with this:

    Please Login or Register  to view this content.
    Thanks for marking the thread as solved and for the rep

    Robert
    Last edited by Trebor76; 12-29-2018 at 10:19 PM.

  10. #10
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: vba to loop thru a table and update another worksheet

    Duplicate post (mod can delete)

  11. #11
    Forum Contributor
    Join Date
    08-22-2009
    Location
    Manitoba
    MS-Off Ver
    Office 2010
    Posts
    524

    Re: vba to loop thru a table and update another worksheet

    Yes Robert, that is exactly what I did. Took me a while and a few different tries but exact same code. Again, thanks for your help!

+ 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. Auto Update Formula Containing Worksheet Name in a Table
    By rcl126062 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-27-2017, 03:09 AM
  2. [SOLVED] Update table from another worksheet
    By LewisBosworth in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 06-13-2016, 09:48 PM
  3. Parallel for loop to update information from worksheet to textbox
    By harin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-08-2016, 04:00 AM
  4. lookup value in master - loop through another worksheet and update value based upon rule
    By rickmeister in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-11-2014, 07:20 AM
  5. Update a single table with data from a worksheet with multiple tabs
    By Shaliza Riley in forum Access Tables & Databases
    Replies: 0
    Last Post: 06-26-2013, 01:20 PM
  6. Replies: 1
    Last Post: 03-02-2010, 10:40 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