I have some code I run each morning that converts transactional data into a table then creates a Pivot Table based off of that data.
I have a button in a separate spreadsheet that I use to run these macros, but when I use the button the code formats the dates in US format not UK.
Sub TUFRFormat()
Application.DisplayAlerts = False
Columns("A:Y").Select
Selection.Columns.AutoFit 'Ensures data is displayed correctly
Columns("B:B").Select 'Rearanges columns to be presented correctly
Selection.Cut Destination:=Columns("Z:Z")
Columns("B:B").Select
Selection.Delete Shift:=xlToLeft
Range("F:F,G:G,H:H,I:I,J:J,U:U,V:V,W:W,X:X").Select
Selection.EntireColumn.Hidden = True 'Hides unrelevant columns
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$Y$50000"), , xlYes).Name _
= "Table1"
Range("Table1[#All]").Select
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleLight9" 'Selects a table theme
Range("Table1[[#Headers],[Unit]]").Select
ChDir "P:\Desktop\Prior Day Journals\Tueday - Friday"
' Creates a pivot table based off of the sheet 'Data'
Sheets.Add After:=Sheets(Sheets.Count)
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Table" 'Adds new sheet and names it table
Sheets("Table").Select
Sheets("Table").Move Before:=Sheets(1) 'Moves to the first sheet in the document
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Table1", Version:=xlPivotTableVersion14).CreatePivotTable TableDestination _
:="Table!R1C1", TableName:="PivotTable1", DefaultVersion:= _
xlPivotTableVersion14
Sheets("Table").Select
Cells(1, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Date")
.Orientation = xlPageField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Year")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Period")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Unit")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Sum Amount3"), "Count of Sum Amount3", xlCount
ActiveSheet.PivotTables("PivotTable1").RowAxisLayout xlOutlineRow
ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted")
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("Posted"). _
EnableMultiplePageItems = True
I have tried looking for an obvious answer, but when I run the code directly rather than through the button it formats the dates as the UK format.
Any help would be appreciated
Related
**I m trying to create three different sheets with this Macro code. So when i run this code those the sheets are creating as it should, but i want to rename these sheets created with particular name and delete them or replace them when i run the code again.
So the below code is the modified in such a way that it creates 2 pivot sheet and one sheet with data that creates the count of range defined... with countifs
SO when i searched the internet for an alternative i tried the other code but the but the range(dynamic) is not getting selected while creating pivot table. it throws an error
SetwsPT=wb.Worksheets.Add
Please help.
Sub MacroPivotReceivedResolved()
Sheets.Add
pivotWS = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"ReceivedMacro!R6C1:R20000C54", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:=pivotWS & "!R3C1", TableName:="PivotTable5" _
, DefaultVersion:=xlPivotTableVersion15
Sheets(pivotWS).Select
Cells(3, 3).Select
ActiveSheet.PivotTables("PivotTable5").RowAxisLayout xlTabularRow
With ActiveSheet.PivotTables("PivotTable5").PivotFields("Receipt Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable5").AddDataField ActiveSheet.PivotTables( _
"PivotTable5").PivotFields("Receipt Date"), "Count of Case Age", xlCount
Sheets("ResolvedMacro").Select
Range("A6").Select
Sheets.Add
pivotWS1 = ActiveSheet.Name
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"ResolvedMacro!R6C1:R20000C54", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:=pivotWS1 & "!R3C1", TableName:="PivotTable6" _
, DefaultVersion:=xlPivotTableVersion15
Sheets(pivotWS1).Select
Cells(3, 3).Select
With ActiveSheet.PivotTables("PivotTable6").PivotFields("Resolved Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _
"PivotTable6").PivotFields("Resolved Date"), "Count of Case Age", xlCount
Sheets("ReceivedMacroAge").Select
Range("A6").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets.Add After:=ActiveSheet
Range("A1").Select
ActiveSheet.Paste
Application.CutCopyMode = False
With Selection
.VerticalAlignment = xlTop
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlLTR
.MergeCells = False
End With
Columns("A:A").ColumnWidth = 16.29
Cells.Select
Selection.ColumnWidth = 17.57
Columns("J:J").Select
Selection.Insert Shift:=xlToRight
Range("J2").Select
ActiveCell.FormulaR1C1 = "=COUNT"
Columns("J:J").Select
Selection.Delete Shift:=xlToLeft
Rows("1:1").Select
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Selection.Insert Shift:=xlDown
Range("H1").Select
ActiveCell.FormulaR1C1 = "Total Outstanding"
Range("I1").Select
ActiveCell.FormulaR1C1 = "=SUM(R[1]C:R[5]C)"
Range("H2").Select
ActiveCell.FormulaR1C1 = "Over 8 Weeks (Over 56 Days)"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=COUNTIFS(R[9]C:R[1000]C, "">=57"")"
Range("H3").Select
ActiveCell.FormulaR1C1 = "6-8 Weeks (42-56 days)"
Range("I3").Select
ActiveCell.FormulaR1C1 = _
"=SUMPRODUCT(INT(R[9]C:R[1000]C>=42), INT(R[9]C:R[1000]C<57))"
Range("H4").Select
ActiveCell.FormulaR1C1 = "4-6 weeks (28 - 41)"
Range("I4").Select
ActiveCell.FormulaR1C1 = _
"=SUMPRODUCT(INT(R[9]C:R[1000]C>=28), INT(R[9]C:R[1000]C<42))"
Range("H5").Select
ActiveCell.FormulaR1C1 = "2-4 Weeks (14 - 27)"
Range("I5").Select
ActiveCell.FormulaR1C1 = _
"=SUMPRODUCT(INT(R[9]C:R[1000]C>=14), INT(R[9]C:R[1000]C<28))"
Range("H6").Select
ActiveCell.FormulaR1C1 = "0-2 Weeks (0-13)"
Range("I6").Select
ActiveCell.FormulaR1C1 = _
"=SUMPRODUCT(INT(R[9]C:R[1000]C>=1), INT(R[9]C:R[1000]C<14))"
Range("H7").Select
ActiveCell.FormulaR1C1 = "Cases to breach next day ( Day 56)"
Range("I7").Select
ActiveCell.FormulaR1C1 = "=COUNTIFS(R[9]C:R[1000]C, ""=56"")"
Range("H8").Select
End Sub
Here's a basic example of how to remove and replace a worksheet:
Sub MacroPivotReceivedResolved()
Const PIVOTA_NAME As String = "Pivot A"
Dim wsPivot As Worksheet, wb As Workbook, pc As PivotCache, pt As PivotTable
Set wb = ThisWorkbook 'for example
DeleteSheet wb, PIVOTA_NAME 'delete the sheet if it exists
Set wsPivot = wb.Sheets.Add 'add new sheet for pivot table
wsPivot.Name = PIVOTA_NAME
'create the pivot cache
Set pc = wb.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:="ReceivedMacro!R6C1:R20000C54", _
Version:=xlPivotTableVersion15)
'create the pivot table
Set pt = pc.CreatePivotTable(TableDestination:=pivotWS.Range("A3"), _
TableName:="PivotTable5", _
DefaultVersion:=xlPivotTableVersion15)
'now you can use `pt` instead of `ActiveSheet.PivotTables("PivotTable5")`
pt.RowAxisLayout xlTabularRow
With pt.PivotFields("Receipt Date")
.Orientation = xlRowField
.Position = 1
End With
pt.AddDataField pt.PivotFields("Receipt Date"), "Count of Case Age", xlCount
End Sub
'Remove any worksheet named `wsName` from workbook `wb`,
' ignoring any error if no sheet with that name is found
Sub DeleteSheet(wb As Workbook, wsName As String)
Dim ws As Worksheet, da As Boolean
On Error Resume Next 'ignore error if sheet doesn't exist
Set ws = wb.Worksheets(wsName)
On Error GoTo 0 'stop ignoring errors
If Not ws Is Nothing Then
da = Application.DisplayAlerts 'get current setting
Application.DisplayAlerts = False 'turn off alerts
wb.Worksheets(wsName).Delete
Application.DisplayAlerts = da 'restore previous setting
End If
End Sub
I've been trying to run the following macro to create a pivot table out of a range from another sheet of data and I canĀ“t seem to make it work.
Sub topc()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Worksheets("TOP C").Delete
Sheets.Add Before:=ActiveSheet
ActiveSheet.Name = "TOP C"
Application.DisplayAlerts = True
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Complaints!R1C1:R3000C32", Version:=6).CreatePivotTable _
TableDestination:="TOP C!R1C1", TableName:="PivotTable1", _
DefaultVersion:=6
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Recurrence")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Subtype Name")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Type Inquiry")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Value"), "Sum of Value", xlSum
End Sub
I keep getting error "Invalid Procedure or Argument" in this area:
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Complaints!R1C1:R3000C32", Version:=6).CreatePivotTable _
TableDestination:="TOP C!R1C1", TableName:="PivotTable1", _
DefaultVersion:=6
and keeps telling me xlDatabase = 1, idk if thats the problem. PLEASE HELP!!
I have an issue with macros returning errors, and I was wondering if you guys could shed some light on this issue.
The macro I have looks like the following:
Sub pivot1_macro()
'
' pivot1_macro Macro
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"7-14 week!R1C2:R199C35", Version:=xlPivotTableVersion15).CreatePivotTable _
TableDestination:="Sheet5!R3C1", TableName:="PivotTable3", DefaultVersion _
:=xlPivotTableVersion15
Sheets("Sheet5").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PartMOD")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("EnteredShift")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Defect")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Defect Quantity"), "Sum of Defect Quantity", xlSum
Columns("D:D").EntireColumn.AutoFit
Columns("A:A").EntireColumn.AutoFit
Columns("B:B").EntireColumn.AutoFit
Columns("C:C").EntireColumn.AutoFit
Columns("D:D").EntireColumn.AutoFit
Range("B3").Select
Range("B3").Select
End Sub
I'd like to apply this macro to any active worksheet, but I'm not quite sure
how to make set 'SourceData' to current active worksheet.
You can use '&' to concatenate strings, so wouldn't
SourceData := ActiveSheet.Name & "!R1C2:R199C35"
work for you to get the current sheet's name?
I tried to record macros of creating a pivot chart. When I run the recorde33d macros after deleting the old chart, an error is being generated. "invalid procedure call or argument'
The error highlights the first line. Can pivot charts be done by macros? Or it just doesn't work when macros recorded?
Sub Macro5()
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Warranty!R1C1:R1048576C52", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Customer Pareto!R12C6", TableName:= _
"PivotTable4", DefaultVersion:=xlPivotTableVersion15
Sheets("Customer Pareto").Select
ActiveSheet.Shapes.AddChart2(201, xlColumnClustered).Select
ActiveChart.SetSourceData Source:=Range("'Customer Pareto'!$F$12:$H$29")
ActiveChart.PivotLayout.PivotTable.AddDataField ActiveChart.PivotLayout. _
PivotTable.PivotFields("VEH_IDENT_NBR"), "Count of VEH_IDENT_NBR", xlCount
With ActiveChart.PivotLayout.PivotTable.PivotFields("Step")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("SA Status")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveChart.PivotLayout.PivotTable.PivotFields("SA_Failure_Mode")
.Orientation = xlRowField
.Position = 1
End With
ActiveChart.ChartType = xlBarStacked
With ActiveSheet.PivotTables("PivotTable4").PivotFields("SA_Failure_Mode")
.PivotItems("").Visible = False
.PivotItems("(blank)").Visible = False
End With
ActiveSheet.ChartObjects("Chart 6").Activate
ActiveSheet.Shapes("Chart 6").IncrementLeft 102
ActiveSheet.Shapes("Chart 6").IncrementTop -87
With ActiveSheet.PivotTables("PivotTable4").PivotFields("Step")
.PivotItems("").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("G13").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Step").PivotItems( _
"Implementation").ShowDetail = False
Range("H13").Select
ActiveSheet.PivotTables("PivotTable4").PivotFields("Step").PivotItems( _
"Solution").ShowDetail = False
ActiveSheet.ChartObjects("Chart 6").Activate
End Sub
Modify and use this as per your requirement:
Key things:
1.TableDestination = should be a range with no pivot table.
2.Change: TableDestination = Sheet Name instead of demo and DefaultVersion = xlPivotTableVersion15 instead of 10
3.Before calling the below Sub, activesheet should be the sheet containing sourceData
Sub Make_PivotTable()
Debug.Print ActiveWorkbook.PivotCaches.Count
ActiveWorkbook.PivotCaches.Add(SourceType:=xlDatabase, SourceData:= _
ActiveSheet.UsedRange).CreatePivotTable TableDestination:="demo!R1C1", TableName:= _
"PivotTable1", DefaultVersion:=xlPivotTableVersion10
Sheets("demo").PivotTables("PivotTable1").AddFields RowFields:="Name"
Sheets("demo").PivotTables("PivotTable1").PivotFields("Age").Orientation = _
xlDataField
Debug.Print ActiveWorkbook.PivotCaches.Count
End Sub
' You can see MS KB link for office 2007 to get informed about your error.
'If this helps Mark as Answer and/upvote, otherwise comment what happened after implementation.
The issue is with spaces in destination file name. The workaround that I have developed is as follows:
Dim wsNew As Worksheet
Set wsNew = Sheets.Add(After:=Sheets(Sheets.Count))
wsNew.Name = "Your Sheet Name with Spaces"
Sheets("Where my table sits as source data").Select
Range("A2").Activate
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"MyTableName", Version:=6).CreatePivotTable TableDestination:= _
"'" & wsNew.Name & "'!R3C2", TableName:="MyNewPivotName", DefaultVersion:=6
Sheets("DestinationSheet").Select
I got this Run-time error '1004':- This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following: If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again. If you're creating a PivotTable report or PivotChart report, type a [it stops here]
I got the aforementioned error at:-
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
I'm trying to run this code:-
Sub CreateTable()
Dim lastRow As Long
Dim pvc As PivotCache
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Set pvc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Database", Version:=xlPivotTableVersion12)
pvc.CreatePivotTable TableDestination:=Worksheets("TABLE").Range("A1"), _
TableName:="INFO", DefaultVersion:=xlPivotTableVersion12
Sheets("TABLE").Select
Cells(1, 1).Select
ActiveWorkbook.ShowPivotTableFieldList = True
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("GRADE"), "Sum of GRADE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("SIZE"), "Sum of SIZE", xlSum
ActiveSheet.PivotTables("INFO").AddDataField ActiveSheet.PivotTables( _
"INFO").PivotFields("QTY"), "Sum of QTY", xlSum
With ActiveSheet.PivotTables("INFO").PivotFields("MODEL")
.Orientation = xlColumnField
.Position = 2
End With
With ActiveSheet.PivotTables("INFO").PivotFields("TYPE")
.Orientation = xlColumnField
.Position = 3
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of GRADE")
.Orientation = xlPINFOField
.Position = 1
End With
With ActiveSheet.PivotTables("INFO").PivotFields("Sum of SIZE")
.Orientation = xlRowField
.Position = 1
End With
ActiveWorkbook.ShowPivotTableFieldList = False
Range("B3").Select
ActiveSheet.PivotTables("INFO").CompactLayoutColumnHeader = "MODEL"
Range("A5").Select
ActiveSheet.PivotTables("INFO").CompactLayoutRowHeader = "SIZE"
Range("A1").Select
ActiveSheet.PivotTables("INFO").PivotFields("GRADE").Caption = "GRADE"
Cells.Select
Cells.EntireColumn.AutoFit
Columns("B:BB").Select
Selection.Style = "Comma"
Cells.Select
Cells.EntireColumn.AutoFit
Range("C1").Select
End Sub
Pls help.
Thanks.
It appears the reason that this error occurs is due to the fourth row:
lastRow = ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
by the look of this, you are finding the last row of data in your "DATA" sheet by looking in column 6. Now if we look what you are combining this with:
ActiveWorkbook.Names.Add Name:="Database", RefersTo:="=DATA!$G$1:$K$" & lastRow
Now, if your source data is in cells G1:K** then:
ActiveSheet.Cells(Rows.Count, 6).End(xlUp).Row
will always return 1 unless you have data in column 6, but from the line above, it seems that your data starts in column 7 ("G"). Therefore, to get this to work you have to change the fourth row to:
lastRow = ActiveSheet.Cells(Rows.Count, 7).End(xlUp).Row