PivotTable Macro Field name error in Excel - vba

I am getting following error for the Macro I have created to insert Pivot table.
It was working before without any problem, I didn't change anything.
Here is the Error
Here is my code
Cells.Select
Sheets.Add.Name = "Detail"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Log!R1C1:R65536C11", Version:=xlPivotTableVersion10).CreatePivotTable _
TableDestination:="Detail!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion10
Sheets("Detail").Select
Cells(3, 1).Select

The error is indicating that you have an untitled column(s). Go back and make sure none of the columns in the SourceData range (i.e. columns 1 to 11) have a blank header.

That can occur with blank names in the header row, or if you're creating a PivotTable with the same name with PivotTable1 (thus why it worked the first time).
Were you deleting the PivotTable in the past?

Related

Excel VB Advanced Filter Copy with Condition

I am trying to put a condition on each row copied. I want all uniques but only if they also have a specific value in another field.
This is what I have to grab all uniques (and it works) but I can't figure out how to get only the rows with a specific value in column J.
r1.Columns(20).AdvancedFilter xlFilterCopy, , Sheet11.Range("A1"), unique:=True
I have tried doing a CriteriaRange but I can't seem to get the syntax correct for it. Additionally I thought about an If statement but logically in my head it means it would fire off the whole list every time it has a true statement, not on a per row basis.
Here is how I thought it might work. But I get a type mismatch error.
r1.Columns(20).AdvancedFilter xlFilterCopy, r1.Columns(10).Value = "November", Sheet11.Range("A1"), unique:=True
Thoughts?
First of all, your Criteria Range should be just that - a Range with the header corresponding to the column to be filtered, and criteria underneath. For example, D1:D2 in this snapshot:
Secondly, you won't be able to copy just a single column (20) while filtering another column (10) in the same step.
You can tweak the Advanced Filter to
First filter the entire list in place based on the criterion provided
And then copy the visible cells in the column in question
Something like this (change Sheet and Range references as needed):
Sub MyFilter()
Dim lastRow As Long
With Sheet1
lastRow = .Cells(.Rows.Count, 1).End(xlUp).Row
.Range("A1:B" & lastRow).AdvancedFilter _
Action:=xlFilterInPlace, CriteriaRange:=.Range("D1:D2"), Unique:=True
With .Range("B1:B" & lastRow).SpecialCells(xlCellTypeVisible)
.Copy Sheet2.Range("A1")
End With
.ShowAllData
End With
End Sub
To be able to keep the other parts of the code that worked perfectly. I added a hidden sheet and wrote a macro to copy the filtered results out to the new hidden sheet. Then I ran my original code against the filtered data on that hidden sheet.
Sub FilterLiveToDataSheet()
' Unhide Required Sheets
Sheets("Original-Data").Visible = True
Sheets("Filtered-Data").Visible = True
' Delete Old Data
Sheets("Filtered-Data").Select
Cells.Select
Selection.ClearContents
' Copy Filtered Data
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
ActiveSheet.ListObjects("TBL_ATTR_Spend").Range.AutoFilter Field:=10, _
Criteria1:="Delta"
Cells.Select
Selection.Copy
' Paste to Data Sheet
Sheets("Filtered-Data").Select
Cells.Select
ActiveSheet.Paste
' Unfilter Original Data Page
Sheets("Original-Data").Select
Range("TBL_ATTR_Spend[[#Headers],[HeaderName]]").Select
Selection.AutoFilter
' Hide Required Sheets
Sheets("Original-Data").Visible = False
Sheets("Filtered-Data").Visible = False
' Go to Results Sheet
Sheets("Results").Select

Autofiltering Pivot and Non-pivot Columns using Excel VBA

I have a pivot table from column B to column N. On the left, in column A, there's some data for which I used the content in the pivot table to look up for some values. On the right, in column O to X, there's also some other data which are calculated based on the pivot table's content.
I need to filter column X = 1 and then column V = 0. Below are my codes:
Dim ws As Worksheet, LastCell As Long
Set ws= ThisWorkbook.Worksheets("Sheet1")
LastCell = ws.Cells(Rows.Count, 2).End(xlUp).Row
ws.Range("$A$4:$X$" & LastCell).AutoFilter Field:=24, Criteria1:="1"
ws.Range("$A$4:$X$" & LastCell).AutoFilter Field:=22, Criteria1:="0"
Problem is, I get "Run-time error '1004': AutoFilter method of Range class failed".
But the code that I get from recording the macro is not that far off either, I just added my own variables:
ActiveSheet.Range("$A$4:$X$900").AutoFilter Field:=24, Criteria1:="1"
ActiveSheet.Range("$A$4:$X$900").AutoFilter Field:=22, Criteria1:="0"
I tried to run back the recorded code afterwards, and I get the same error. What is causing this and how can I fix it?
Thanks in advance!
Welp, I got the answer after trying to explore some other ideas.
Instead of using:
ws.Range("A4:X" & LastCell).AutoFilter Field:=24, Criteria1:="1"
ws.Range("A4:X" & LastCell).AutoFilter Field:=22, Criteria1:="0"
I used:
ws.Range("A4").AutoFilter Field:=24, Criteria1:="1"
ws.Range("A4").AutoFilter Field:=22, Criteria1:="0"
I noticed this as I was trying to use autofilter manually on Excel. I highlighted the whole data and was unable to click the filter button. Normally, it doesn't matter whether you only select a cell or the whole sheet, but since I'm using autofilter on pivot table as well, the only way I was able to use it is by selecting any cell other than the pivot table's.

Defining variable Source range for pivot chart code in VBA

I have recorded a macro for pivot table and chart. Now I what to change the range as per the changes in the rows and columns of the source sheet.
The code from the recorded macro is:
SourceData:= _
"DATA INPUT SHEET!R2C1:R23C12", Version:=xlPivotTableVersion15)
Any way out to change the Range R2C1:R23C12 to Range(Cells(2,1),Cells(FinalRow,8+NoBids)), where Finalrow is the last row of the source sheet and NoBids is to the no. of columns to be added after the 8th column of the source sheet.
There is a way, try the code below:
SourceData:= "DATA INPUT SHEET!" & Range(Cells(2, 1), Cells(FinalRow, 8 + NoBids)).Address(True, True, xlR1C1)
If you want to learn more about the Range.Address Property, read HERE

How to select data range dynamically for pivot table

I have searched this topic exhaustively however I am struggling to find a solution which works for my macro. I need the source data for a pivot table to include all rows (containing data) on a sheet. The amount of rows will change daily.
Here is what I've got so far:
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "RAW_DATA"
Range("A1").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"RAW_DATA!R1C1:R159C24", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet4!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet4").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Asset")
.Orientation = xlRowField
.Position = 1
End With
The values which represent my pivot tables source data are RAW_DATA!R1C1:R159C24. The problem is that I need this range to dynamically increase or decrease depending on the size of the populated source data range.
First of all, you might be able to easily solve your problem by just setting the columns as your datarange (E.g. RAW_DATA!$A:$X).
When the data is appended a simple update on the pivot will include new data or exclude the data that is no longer there.
That said, here's a VBA solution:
This Example will change the source data for PivotTable1 on Sheet1 to be "RAW_DATA!$A$1:$X$ whatever the last row is"
Sub ChangePivotData()
Dim lastrow as double
lastrow = Worksheets("RAW_DATA").Range("A" & Rows.Count).End(xlUp).Row
With Worksheets("Sheet1").PivotTables("PivotTable1")
.ChangePivotCache ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="RAW_DATA!A2:X" & CStr(lastrow), _
Version:=xlPivotTableVersion14)
End With
End Sub
That is the core of it. However, you might want to check for blank column headers to do some error prevention, automatically refresh the table after the new cache has been set, etc.
More extensive example can be found here:
http://www.thespreadsheetguru.com/the-code-vault/2014/7/9/change-a-pivot-tables-data-source-range
Enjoy :)
You can use the below if your data is the only thing in the sheet
Worksheets("RAW_DATA").Usedrange
and the corresponding range address
Worksheets("RAW_DATA").Usedrange.Address
Hope this helps

Application-defined or object-defined error

So I have a sheet called "Dashboard" that I have several charts on, but every week I'm adding data to the tables these charts are based on. The second sheet that has the data for this table is "Historical Totals" and the data I want is in columns A through E (this includes labels and headers). I'm trying to automate the charts updating, and I don't quite know why this isn't working. Here is the code I have so far:
Sheets("Dashboard").Select
ActiveSheet.ChartObjects("Chart 4").Activate
ActiveChart.SetSourceData Source:=Sheets("Historical Totals").Range("A1", Range("E1").End(xlDown))
I've tried seperating our the range, and it is selecting the appropriate data, so I don't really know where I'm making the mistake.
try the following. You need to reference the data sheet every time you use Range:
Sheets("Dashboard").ChartObjects("Chart 4").Activate
ActiveChart.SetSourceData _
Source:=Sheets("Historical Totals").Range(Sheets("Historical Totals").Range("A1"), _
Sheets("Historical Totals").Range("E1").End(xlDown))
Assuming you only have the table in the worksheet "Historical Totals" I would usually go xlUp rather than xlDown but the following is effectively the same as Philip A Barnes's answer:
Sub setArea()
With Excel.ActiveWorkbook
'.Sheets("Dashboard").Select '<==no need to select for the code to execute
.Sheets("Dashboard").ChartObjects("Chart 1").Activate
End With
With Excel.ActiveWorkbook.Sheets("Historical Totals")
Excel.ActiveChart.SetSourceData _
Source:=.Range( _
.Cells(1, 1), _
.Cells(.Cells(.Rows.Count, 5).End(Excel.xlUp).Row, 5) _
)
End With
End Sub