Problem using macro to copy multiple data sets from pivot table in to multiple tabs

BasicMan

New Member
Joined
Jan 10, 2019
Messages
1
Hello I am new to the site and hoping someone can help me.

I have large data set that feeds in to a pivot table for multiple categories which then needs to be split on to separate worksheets and formatted. I am a novice when it comes to VBA so have recorded a macro and replicated it for each category and I run them all together to process the data.

In some months, there may be no data in a category so it would need to skip and move on to the next category macro. I have tried adding an error handler which does move on to the next macro but it doesn't leave that category tab blank, it copies the previous category data in to the wrong worksheet.

I presume I need a better way of handling the error to make sure the worksheet remains blank if there is no data in the pivot for that category.

Can anyone help please? Example of one of the macros is shown below:

Code:
Sub CarParking()
'
' CarParking Macro
'

'
On Error GoTo InvalidValue:
   ActiveSheet.PivotTables("PivotTable2").PivotFields("RECODE LOOKUP"). _
        CurrentPage = "CAR PARKING"
    Range("A7").Select
    Set pt = ActiveSheet.PivotTables("PivotTable2")
    pt.RowRange.Select
    Selection.Copy
    Sheets("Car Parking").Select
    Range("A1").Select
    ActiveSheet.Paste
    Sheets("Tab Creation Pivot").Select
    pt.DataBodyRange.Select
    Selection.Copy
    Sheets("Car Parking").Select
    Range("G2").Select
    ActiveSheet.Paste
    Range("A1:G1").Select
    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorLight2
        .TintAndShade = 0.599993896298105
        .PatternTintAndShade = 0
    End With
    Selection.Font.Bold = True
    Range("C4").Select
    Range("A1:G1").Select
    Range("G1").Activate
    Selection.Borders(xlDiagonalDown).LineStyle = xlNone
    Selection.Borders(xlDiagonalUp).LineStyle = xlNone
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    Range("B3").Select
    Sheets("Tab Creation Pivot").Select
    Exit Sub
InvalidValue:
MsgBox "No data available for Car Parking click next to continue "

End Sub
 
Last edited by a moderator:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,004
Messages
6,122,659
Members
449,091
Latest member
peppernaut

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top