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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Recently Microsoft released a brand new function called CONCAT. It's supposed to replace its predecessor CONCATENATE. But how does it work? And what's new? In this article, we take a closer look at all of this - we even included an exercise file for…
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.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now