+ Reply to Thread
Results 1 to 18 of 18

Looping Through Collections

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Looping Through Collections

    I am trying to write a code where the loop will run through all the sheets of a template Workbook and copy a certain data and paste it in the Master Workbook.

    I am having some issue with a Part of the Code.

    Please Login or Register  to view this content.
    Can anyone clarify what is wrong?
    Cheers!
    Deep Dave

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,770

    Re: Looping Through Collections

    Try this... In the below code After performing paste action keep the cursor to the blank cell so that the next paste process will continue in another location. Otherwise the same area will be replaced with the data.

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    Hey!! Thnx for the revert.

    The reason why I did not use "For Each Ws In ActiveWorkbook.Worksheets" is because a different workbook will be open by the time the Macro reaches that part,.

    Below is the whole Macro and related files -

    Please Login or Register  to view this content.
    Please help me with the issue, as i am trying to solve it since a long time..

    Help appreciated!
    Attached Files Attached Files

  4. #4
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    @ SixthSense

    I have changed the code. The code is running however it is not running through all the worksheets in a workbook. I have to manually run the Macro again for the next worksheet.

    Secondly it is extremely slow. Can you suggest something to increase the speed?

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    Why are opening, closing and then opening the master workbook?
    If posting code please use code tags, see here.

  6. #6
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    If I close the Master workbook, then I can refer the Template workbook as the activeworkbook. Thats the only reason.

    Id be grateful if you can suggest a better way.

    I have also shifter the Macro to the Personal Macro Workbook, hence only 2 files will be required. Master Bom and Template (refer Attachment in Post #3)
    Last edited by NeedForExcel; 04-24-2013 at 08:45 AM.

  7. #7
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Looping Through Collections

    Don't use ActiveWorkbook, refer to the template by it's name or, and probably better, create a reference to it.

    Which workbook is the code in?

    If it's in the template then you can use ThisWorkbook to refer to it.

  8. #8
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    I used Activeworkbook as there are 100z such templates.

    However, I worked on it and was finally able to get it working..

    Please see the sheets attached. The code is the Upload.xlsm sheet.

    If possible please guide how can I get the code to work even faster?

    Thanx a ton for the reply.
    Attached Files Attached Files

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Looping Through Collections

    I don't see anything like the code you posted in the workbook Upload.xlsm.

  10. #10
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    I changed it coz the 1st code was incorrect.

    But it basically does what I wanted to achieve in the 1st code I stated above.

    Is the new code alright?

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Looping Through Collections

    What code should I be looking at?

  12. #12
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    Here is the code. I am just trying to know if I can make the Code better/faster?

    Please Login or Register  to view this content.

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Looping Through Collections

    This seems quite fast, probably because it only opens 'Download.xlsm' once.
    Please Login or Register  to view this content.

  14. #14
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    Thats amazing.. Thanx..

  15. #15
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    Just 1 Query -

    Please Login or Register  to view this content.
    Referring to the above 2 lines where you set the Workbook, and the Download.xlsm Workbook opens.

    In the next line when you mention Thisworkbook, will it not refer to Download.xlsm as it the the activeworkbook?

  16. #16
    Forum Guru benishiryo's Avatar
    Join Date
    03-25-2011
    Location
    Singapore
    MS-Off Ver
    Excel 2013
    Posts
    5,147

    Re: Looping Through Collections

    looks like you got it solved. anyway, i'm not someone you should pin your hopes on when it comes to VBA. "ThisWorkbook" always refers to the workbook where the codes are stored, not the ActiveWorkbook.

    Thanks, if you have clicked on the * and added our rep.

    If you're satisfied with the answer, click Thread Tools above your first post, select "Mark your thread as Solved".

    "Contentment is not the fulfillment of what you want, but the realization of what you already have."


    Tips & Tutorials I Compiled | How to Get Quick & Good Answers

  17. #17
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    Perfect!! I had a misunderstanding regarding the subject.

    Now it is sorted out..

  18. #18
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,873

    Re: Looping Through Collections

    @ Norie

    Hi!!

    I am facing a problem. The Code you wrote is working fine. I tried it on the 1st workbook and it worked perfectly.

    However I am not able to run the same code on other similar Workbooks.

    Please Login or Register  to view this content.
    I have a Mastersheet where the data is copied and pasted. And the Templates keep on changing. What I am doing right now is Open the template workbook, make a COde Module, Paste the code and run the Macro.

    Can you help me out??
    Last edited by NeedForExcel; 04-26-2013 at 04:09 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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