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
333 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

821 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