Solved

How do you change the order tabs are copied and pasted when using VBA in MS Excel 2010?

Posted on 2014-07-29
3
331 Views
Last Modified: 2014-07-29
Hi All!

I'm hoping there is an easy solution to my issue. I have the code below that is set to go through all sheets in my workbook and copy then paste special values into the same spot. However, my tab that has my pivot table data is getting copied prior to all the other tabs, so my get pivot formulas are erring out because the data is getting pasted as values before the other tabs. I am trying to find a way to have my pivot table copy and paste special *after* all my other tabs. Any thoughts? Here is the VBA I am using to cycle through the tabs:

Sub PasteValues()
    Dim wks As Worksheet
    Dim wb As Workbook
    Set wb = ThisWorkbook
    For Each wks In wb.Worksheets
    wks.Cells.Copy
    wks.Cells.PasteSpecial Paste:=xlValues
    Next
End Sub

If this isn't clear, ask questions and I'll try to explain better. Thanks in advance for everyone's help!

- Dave
0
Comment
Question by:uga_godawgs
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40227407
You'd just need to add an exception to the For Each...Next loop and then go back to that sheet and do a final value replacement.  Here's an example:
Sub PasteValues()
    Dim wks As Worksheet
    Dim wb As Workbook
    Set wb = ThisWorkbook
    For Each wks In wb.Worksheets
        If wks.Name <> "pivotsheettabname" Then
            wks.Cells.Copy
            wks.Cells.PasteSpecial Paste:=xlValues
        End If
    Next
    With Sheets("pivotsheettabname")
        .Cells.Copy
        .Cells.PasteSpecial Paste:=xlValues
    End With
 End Sub

Open in new window


Regards,
-Glenn
0
 

Author Comment

by:uga_godawgs
ID: 40227463
Glenn - worked perfectly! Definitely a 'Doh!' moment - looked for an over-complicated solution! Thanks for your help!

-Dave
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40227515
You're welcome.  Sometimes it's a "forest-for-the-trees" thing...happens to me all the time.

-Glenn
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Outlook 2010 not responding when sending email. 4 36
VBA name newly created sheet 4 21
Excel IF formula 3 19
Unhide very hidden sheets with password 22 44
Microsoft Office Picture Manager was included in Office 2003, 2007, and 2010, but not in Office 2013. Users had hopes that it would be in Office 2016/Office 365, but it is not. Fortunately, the same zero-cost technique that works to install it with …
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question