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