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
Related
I have a macro that creates a PivotTable for me with the fields I want. However, I would like them to choose the indicated values.This is what the table looks like at the start
here is what I would like e.g.
this is my code:
Set sh_s = wbMe.Sheets("kredyty")
lr1 = sh_s.Range("A" & Rows.Count).End(xlUp).Row
ptver1 = 6
tbName1 = "Tabela przestawna"
Sheets.Add after:=Sheets(Sheets.Count)
Set sh_d = ActiveSheet
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, _
SourceData:=sh_s.Name & "!R1C1:R" & lr1 & "C58", Version:=ptver).CreatePivotTable _
TableDestination:=sh_d.Name & "!R3C1", TableName:=tbName1, _
DefaultVersion:=ptver1
With sh_d.PivotTables(tbName1)
With .PivotFields("CRD_RWG")
.Orientation = xlPageField
.Position = 1
With sh_d.PivotTables(tbName1)
With .PivotFields("NEW_DEFAULT")
.Orientation = xlPageField
.Position = 1
End With
.AddDataField .PivotFields("CRD_EKSP_PIER_DC_FIN"), "Suma z Ekspozycji", xlSum
.AddDataField .PivotFields("CRD_KOR_DC_FIN"), "Suma z Korekty", xlSum
End With
The below is the code that I have used. I want to fix my chart numbers of this and the other charts that I have created so that I can format them later. Also if someone can suggest how to shorten up the code.
'Declare Variables
Dim PCache7 As PivotCache
Dim PTable7 As PivotTable
'Define Data Range
LastRow = DSheet.Cells(Rows.Count, 1).End(xlUp).Row
LastCol = DSheet.Cells(1, Columns.Count).End(xlToLeft).Column
Set PRange = DSheet.Cells(1, 1).Resize(LastRow, LastCol)
'Define Pivot Cache
Set PCache7 = ActiveWorkbook.PivotCaches.Create _
(SourceType:=xlDatabase, SourceData:=PRange). _
CreatePivotTable(TableDestination:=PSheet.Cells(2, 12), _
TableName:="AnalyticsPivotTable7")
'Insert Blank Pivot Table
Set PTable7 = PCache.CreatePivotTable _
(TableDestination:=PSheet.Cells(2, 12), TableName:="AnalyticsPivotTable7")
'Insert Row Fields
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("Issue Type")
.Orientation = xlRowField
.Position = 1
End With
'Insert Data Field
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("IncidentID")
.Orientation = xlDataField
.Position = 1
.Function = xlCount
.NumberFormat = "#,##0"
End With
'Insert Data Field
With ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("IncidentID")
.Orientation = xlDataField
.Position = 2
.Function = xlCount
.Calculation = xlPercentOfTotal
.NumberFormat = "#0.0%"
End With
'Format Pivot Table
ActiveSheet.PivotTables("AnalyticsPivotTable7").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("AnalyticsPivotTable7").TableStyle2 = "PivotStyleMedium9"
Range("N4:N16").Select
ActiveSheet.PivotTables("AnalyticsPivotTable7").PivotFields("Issue Type"). _
AutoSort xlDescending, "Count of IncidentID2", ActiveSheet.PivotTables( _
"AnalyticsPivotTable7").PivotColumnAxis.PivotLines(2), 1
I run the same reports every day and would like to use a generic VBA code that selects all my data and creates the pivot table. I've recorded the MACRO:
I need the data range selected automatically to be allow any data amount
I need the data range to be selected automatically
The sheet should not matter
Finally I would like to name the sheet
code:
Sheets.Add
ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable26").PivotCache. _
CreatePivotTable TableDestination:="Sheet4!R3C1", TableName:="PivotTable27" _
, DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Date Opened")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Date Opened")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Queue")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Sub Queue")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Status")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable27").AddDataField ActiveSheet.PivotTables( _
"PivotTable27").PivotFields("Source"), "Count of Source", xlCount
With ActiveSheet.PivotTables("PivotTable27").PivotFields("Queue")
.PivotItems("(blank)").Visible = False
End With
End Sub
Here's a little code to get you what you asked in your post:
Option Explicit
Sub AutoPivot()
Dim PT As PivotTable
Dim PTCache As PivotCache
Dim PtDataRng As Range
Dim ws As Worksheet
Dim LastRow As Long
Dim LastCol As Long
Set ws = Worksheets("PivotData") ' <-- modify "PivotData" to your sheet's name that holds the dynamic data
With ws
LastCol = .Cells(1, .Columns.Count).End(xlToLeft).Column ' <-- get last column with data in row 1
LastRow = .Cells(.Rows.Count, "A").End(xlToLeft).Row ' <-- get last row with data in column A
Set PtDataRng = .Range(.Cells(1, 1), .Cells(LastRow, LastCol)) ' <-- set the dynamic Range
End With
' create a new wroksheet, place it at the end, and use an InputBox to select it's name
Set ws = ThisWorkbook.Sheets.Add(after:=Sheets(ThisWorkbook.Sheets.Count))
ws.Name = Application.InputBox("Select A Valid Name for new sheet", Type:=2)
' set the Pivot Cache to updated dynamic Ranget
Set PTCache = ThisWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=PtDataRng)
' Create Pivot Table from Pivot Cache
Set PT = ws.PivotTables.Add(PivotCache:=PTCache, TableDestination:=ws.Range("A3"), TableName:="PivotTableTest")
' set the fields setting in the Pivot Table
With PT
With .PivotFields("Date Opened")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Date Opened")
.Orientation = xlColumnField
.Position = 1
End With
With .PivotFields("Queue")
.Orientation = xlRowField
.Position = 1
End With
With .PivotFields("Sub Queue")
.Orientation = xlRowField
.Position = 2
End With
With .PivotFields("Status")
.Orientation = xlColumnField
.Position = 1
End With
.AddDataField .PivotFields("Source"), "Count of Source", xlCount
With .PivotFields("Queue")
.PivotItems("(blank)").Visible = False
End With
End With
End Sub
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
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?