+ Reply to Thread
Results 1 to 14 of 14

Loop for all worksheets

  1. #1
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Loop for all worksheets

    Hi,

    Can someone please advise if the below code for vba is correct to loop through all worksheets?



    Sub Master()

    Dim ws As Worksheet

    For Each ws In ThisWorkbook.Worksheets

    Next ws

    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    Yes it is.
    Regards,
    Vandan

  3. #3
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    So, I am trying to loop through all worksheet in a workbook and copy the pivot tables that are on each worksheet. can someone please check to see if these codes are right?
    I am getting a 'Method or Data Member member not found error'

    Sub Master()

    Dim ws As Worksheet
    Dim SheetN As Long
    Dim CellRef As Long

    SheetN = 0
    CellRef = 4

    For Each ws In ThisWorkbook.Worksheets
    SheetN = SheetN + 1
    Worksheets.SheetSheetN.PivotTables("PivotTable1").TableRange2.Copy Destination:=Worksheets("Master").Range("CellRef")
    CellRef = CellRef + 10
    Next ws
    End Sub

    Ps. I have checked the code for copying the pivot tables and that works fine..

  4. #4
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    try changing

    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    Also, please use code tags around your code! read forum rules if you are not sure how to do this.

  5. #5
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    Please Login or Register  to view this content.
    Hi Vandan,

    The last part of the above formula where it says Range("CellRef"), I am trying to put the cell reference there, which is usually going to be Column A and some row number with 10 spaces in between.

    The CellRef long has the value 4 initially but i want the lettter A in front of it to say .Range("A4") or .Range("A14) etc.

    Any clue?

  6. #6
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    change

    .range("Cellref")

    to

    .cells(cellref,1)


    if this doesn't work, please upload your file so I can troubleshoot efficiently.

  7. #7
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    Hi Vandan,

    That doesn't work either. Attached is the file.

    There are more than 150 employees and so that many number of tabs in this worksheet but i have left only 3.

    So in the code, i am trying to loop until all 150 tabs (employees) are captured and trying to copy the pivot table that you see on row 55 in the three employee tabs.

    Thanks,
    Attached Files Attached Files

  8. #8
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    will this work:

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    i am getting an error saying that you can't move part of the pivottable or insert rows or columns in the pivottable.

    What i am trying to do is capture all employees pivot tables in the master tab. but instead of doing it one by one, i want to try to loop it.

  10. #10
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    Any suggestions?

  11. #11
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    sorry about my half assed job ghan7650...I am trying to do this between meetings. Try this....

    Also first clear the master sheet and then run the code. I get no errors when I run this.

    Please Login or Register  to view this content.

  12. #12
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    Hi Vandan,

    One quick question, I am trying to extract employee names from cell C3 of various tabs to the master tab and display them right on top of the pivot table on the master tab.

    is this the right code?
    (EmpName is defined as a String)

    Please Login or Register  to view this content.

  13. #13
    Valued Forum Contributor
    Join Date
    05-07-2012
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    354

    Re: Loop for all worksheets

    it isn't.

    copy command copies content to clipboard and return true or false depending on success or failure
    you can either
    Please Login or Register  to view this content.
    or do

    Please Login or Register  to view this content.

  14. #14
    Registered User
    Join Date
    05-22-2012
    Location
    Calgary, canada
    MS-Off Ver
    Excel 2010
    Posts
    25

    Re: Loop for all worksheets

    I ran the second code that you have mentioned and it works like a charm.

    Thanks for your help Vandan~!

+ 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