+ Reply to Thread
Results 1 to 14 of 14

Loop Function

  1. #1
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Loop Function

    Hi, I have a piece of code below which goes through a workbook selecting a range from a pivot table and exporting it to specific sheets. The code is very long and time consuming to change. I have several codes similar to this format too. Is there a way of looping to select each publication in turn and export with one Looping function?

    Please Login or Register  to view this content.

  2. #2
    Forum Contributor
    Join Date
    04-16-2009
    Location
    Dorset, UK
    MS-Off Ver
    Office 2003, Office 2007
    Posts
    131

    Re: Loop Function

    It's difficult for me to test here, but try something like this:
    Please Login or Register  to view this content.

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Function

    A sample workbook showing before/after examples would make this easier to test for you...but try this:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 07-07-2010 at 12:02 PM.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    JBeaucaire I think your code is more like what I'm looking for, the sheet names are:

    "AGN", "AGS", "AGW", "BPI", "CNB", "EGK", "EGS", "EHK", "EHS", "FPN", "FRA", "HUK", "HUS", "LFN", "PHM", "PIL", "PPH", "SPD", "SSH"

    Ive attached screenshots (ac names blacked out) the spreadsheet was too large to attatch even when I deleted most of the data.the output.

    You can see from output that there is a tab for each publication. before the data is imported only rows 1&2 of each publication sheet exsist.
    Attached Images Attached Images

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Function

    We used all those sheet names as listed in the array in the code I gave. Take out the strings you don't want.

  6. #6
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    Sorry, Forgot to mention the errors:
    JBeaucaire; I get a runtime error '9'

    Subscription out of range.

    when I debug it takes me to this line:
    Sheets(MyShts(Sht)).Range("A3").PasteSpecial xlPasteValues

    And has pasted only 1 account name and no revenues into sheets AGN to EGK



    Kafrin; I just get a 'next without for' error for the 'next p' above 'end sub'

  7. #7
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    I know why Im getting that error JB, there is no EGS, EHS or HUS sheet as the old macro incorporates these into EGK, EHK and HUK respectively, but i can set up extra sheets if it wont work without them

  8. #8
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    The code works without errors now but only imports the last account name of each pivot refresh into cell A3 of each publication sheet.

  9. #9
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Function

    My bad...fix this:
    Please Login or Register  to view this content.

  10. #10
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    Thats great thanks, now to try and change the rest of my reports

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Function

    Again, just edit the array to only include the sheets you want to process.
    Last edited by JBeaucaire; 07-08-2010 at 06:53 AM.

  12. #12
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    Hi again, I've changed a similar macro and its working fine, but this one is a little more advanced:

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    06-21-2010
    Location
    Norwich, England
    MS-Off Ver
    Excel 2003
    Posts
    13

    Re: Loop Function

    When I run the above i get a:
    Run-time error '1004':

    Select method of range class failed
    and directed to the following part of the code:


    Sheets(MyShts(Sht)).Range("A4").Select

  14. #14
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Loop Function

    If that takes care of your need, please click EDIT in your original post, click GO ADVANCED and set the PREFIX box to SOLVED.

+ 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