I'm trying to set up a loop to pull data from multiple cells in multiple worksheets

  • I have a workbook that contains about 265 vehicle data worksheets. Each uniquely numbered vehicle has it's own worksheet with detailed information about that vehicle. Every vehicle page is identical, accept for the data stored in it. These pages are setup in rows and columns of data, but not in a line-by-line list. One of these pages is a list of all of these vehicles with certain cells of information that i need to pull over from each of the individual vehicle workbooks. I've been trying for days now to set up a loop to bring this information from each individual page to this line-by-line list with little to no success.


    Here is an example of what I am trying to do.


    First vehicle on the list is vehicle;


    A-01 (vehicle number, not cell reference) , I want the VBA code to go to A-01's worksheet and pull data from cells "B30", "B18", "B9", "B12", "B15", "B21", "B33", "B27", "J30", "F24", "H24", "D18". Then paste that data back to a row in the list of vehicles into cells "C7", "D7", "E7", "F7", "G7", "H7", "I7", "J7", "K7", "L7", "M7", "N7", in that order.
    Then I want it to go to vehicle A-02 (next worksheet) and copy the data from the same set of cells (B30, B18, B9....etc.) and copy that to the next row on the list worksheet, Then go back and do the same thing for vehicle A-03, then for A-04, then A-05........untill the end of the list.


    I can get the data from the first worksheet (vehicle A-01) but I can't figure out how to get the VBA to go to the next worksheet to get the next set of data and paste it.
    I have tried a For All loop with some success, but there are 4 or 5 worksheets that I want it to skip over because they contain data that is not relevant to the worksheet I am trying to build.
    One other thing I should note, currently the first page contains the data (entered manually) in a table. I am trying to get VBA to paste the data into that table.
    I hope this is clear. I've tried to describe it to the best of my ability. Thanks for any help you can offer.


    I have uploaded a small sample of the list with what I would like it to look like after running the macro and updating all of the fields.forum.ozgrid.com/index.php?attachment/70701/

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Hi cshortt,
    For the VBA to go to the next worksheet to get the next set of data and paste it, you can try this:



    Thank

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee






    Thanks so much for the help and replies. I did as you suggested and it did not paste the correct data. I put a watch on ws and it is still selecting "Statistics" as the first workbook it pulls data from. Maybe I have the code in the wrong place or something else. I have pasted a copy of my full code for you to look at. Maybe that will help.




  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Quote

    Each uniquely numbered vehicle has it's own worksheet with detailed information about that vehicle. Every vehicle page is identical, accept for the data stored in it.


    In my humble opinion this is not the way to go. You could just have two tabs - one for the data and the second for the presentation which would have a drop-down for the vehicle number that upon selection would update the tab from the data tab via standard lookup formulas.


    That said, see how this macro goes:



    HTH


    Robert

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Trebor76, thanks so much for the reply. I agree with you about how the database is set up. I built this some 15 years ago and it has more than doubled in size since then. I've been playing with other options on rebuilding this in a more easily manageable way. I actually have started rebuilding it in just the way you stated. But until I get that to work and decide exactly how it will be done, i'm trying to get something working as it exists now. I'll give your code a try today and let you know how it works out.
    One problem with the existing database is it almost 100mb in size due to all the photos and takes forever to open. If I split the database into separate workbooks I'm thinking it would load faster. Still in the planning stages of the second iteration of this database.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Trebor76,
    Again, thanks for the help. The code works, accept for one thing. The loop still does not skip over the worksheets that I don't want it to pull data from. I don't get it. Everyone seems to use this command to try and get the code to skip those sheets, but nothing seems to make it skip them. I've tried renaming them still with no luck. I even set the properties to "VeryHidden" and that did not work either. Just keeps pulling data from them. The only way I can make it work correctly is to delete those sheets all together. Once I do that, none of my vehcile sheets work correctly since one of the sheets has my data validation information in it. Ugh! Frustrating.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Please don't quote every answer it makes the Thread untidy and hard to follow, use the reply button.


    You should attach an example workbook and Trebor76 is 100% correct, your present workbook is working in reverse and making hard work .

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Okay. I'm sorry about the quote thing. For some reason the reply (without quote) button was not showing up on my browser. I closed it down and restarted it, then reloaded the page and it's back now. My humble apologies.
    I did attach a sample workbook. It's in my first post.
    I think I may have found a work around. When the code copies the data in the cells it includes the sheets that I don't want it to include. Fortunately, it pus this incorrect data in the first three lines. So, on my list page, I inserted three lines above the first line and then hid the lines. This then puts the pasted data on the correct line. It's not perfect, but I'm getting closer.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Is there any reason why the tabs aren't named the exact same way as the unit number they're representing i.e. tab A1 is for unit A-01?


    If they were we could use use the unit number in column B of the List - By Vehicle Number tab to link the data through via the INDIRECT function - no code required.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Slightly different take on it cshortt,
    I usually use Cells(row #, col #) to reference my data.


    Try the following:

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Thanks kingtim.
    I get a "compile error, Variable Not Defined" when it hits NumSheets = ActiveWorkbook.Sheets.Count - 4. Does this need to be defined somewhere?

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Hmmm, I'm not sure.
    Try adding "NumSheets" to the Dim Statement since I hadn't defined it in my original example

    Code
    Dim r, NumSheets As Integer


    It worked for me without it, but sometimes it is not so forgiving.
    If it is still giving you problems it may be the version of Office you have. I am using MS Office 2010.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Try changing

    Code
    NumSheets = ActiveWorkbook.Sheets.Count - 4


    to

    Code
    NumSheets = ThisWorkbook.Sheets.Count - 4

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    Hi cshortt,


    Unless you can match the unit numbers in Col. B of the List - By Vehicle Number tab to the relevant vehicle tabs in the workbook the data won't align regardless of what macro you use.


    Robert

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    [ATTACH=CONFIG]70717[/ATTACH]


    Here is a shot of my screen and what I getting when I run the update macro. I'm looking to skip the discussed worksheets to eliminate the first two lines on this table. The main reason is that the blank cells above the lines are used for filtering criteria. The way I have it set up, the table can be filtered by any one of the column headings. The added lines are causing problems with the filters.

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    The code examples rely on the order of the vehicle detail sheets being exactly the same as column B on the "List - By Vehicle Number" sheet.


    You can overcome this by including the vehicle unit number (cell B6 on the details sheet) in the data that is being updated, then sorting the table by Unit number.


    Try the attached file. You will see that I mixed up the order of the sheets and added a new Vehicle Details sheet (A11), click the "Update Database" button and you will see that the table updates correctly.


    I have also added code to the ThisWorkbook Object module so that any changes made to any vehicle details sheet will be immediately reflected on the "List - By Vehicle Number" sheet. If a new Vehicle Details sheet is added then the new vehicle will automatically be added to the table on "List - By Vehicle Number" sheet.


    Code assigned to the Update button (in the "List - By Vehicle Number" sheet Object Module) is


    The code in the ThisWorkbook Object Module isPrivate Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

  • Re: I'm trying to set up a loop to pull data from multiple cells in multiple workshee


    I agree with Trebor, the entire structure of this workbook needs to be revised. A size of 100MB for the amount of data involved is crazy.


    As Trebor says you could have just 1 sheet for raw data for all the vehicles and a Summary sheet which could use formulas to auto-update.


    If you really need photos of every vehicle then have all images stored in a separate file and have a button to find and show the photo for a selected vehicle.


    Doing that would reduce the Excel file size for all 250+ vehicles to around 500KB at the most.

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!