I'm new at VBA and Macros. Following Macro Recording, I ended up with this bug.
The file name is available on desktop with the correct name.
Thanks for any help,
Sub Macro2435Pivot()
'
' Macro2435Pivot Macro
'
'
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"2435_Monthly Sales with Adjustm'!1:15000", Version:=xlPivotTableVersion15). _
CreatePivotTable TableDestination:="Sheet2!R3C1", TableName:= _
"PivotTable" & [=round(Rand()*1000,0)], DefaultVersion:=xlPivotTableVersion15
Sheets("Sheet2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Month")
.Orientation = xlPageField
.Position = 1
End With
Related
I recorded a macro that makes some pivot tables in a new sheet and it works fine, so i tried to make it able to overwrite if the sheet already existed but i can't seem to make it work. The problem is that when i use the macro once it does add the new sheet but it also adds another one on top of the one already made, and when i try to use the macro again to see if it overwrites the other one, it doesn't it just adds another unnamed sheet.
The code looks like this:
Sub Makro7()
Sheets.Add After:=ActiveSheet
On Error Resume Next
Sheets.Add().Name = "Statistics"
On Error GoTo 0
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Base!R1C1:R18288C12", Version:=6).CreatePivotTable TableDestination:= _
"Statistics!R1C1", TableName:="Pivottabel22", DefaultVersion:=6
Sheets("Statistics").Select
Cells(1, 1).Select
ActiveSheet.PivotTables("Pivottabel22").AddDataField ActiveSheet.PivotTables( _
"Pivottabel22").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel22").PivotFields("FACULTY_ID")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel22").PivotFields("PROGRAM_TYPE_NAME")
.Orientation = xlRowField
.Position = 1
End With
Range("A1").Select
ActiveSheet.PivotTables("Pivottabel22").DataPivotField.PivotItems( _
"Antal af FACULTY_ID").Caption = "Antal"
Range("B1").Select
ActiveSheet.PivotTables("Pivottabel22").CompactLayoutColumnHeader = "Fakultet"
Range("A7").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel22").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R7C1", TableName:= _
"Pivottabel23", DefaultVersion:=6
Sheets("Statistics").Select
Cells(7, 1).Select
ActiveSheet.PivotTables("Pivottabel23").AddDataField ActiveSheet.PivotTables( _
"Pivottabel23").PivotFields("FACULTY_ID"), "Antal af FACULTY_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("PROGRAM_TYPE_NAME")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("FACULTY_ID")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel23").PivotFields("Antal af FACULTY_ID")
.Calculation = xlPercentOfTotal
.NumberFormat = "0.00%"
End With
Range("A7").Select
ActiveSheet.PivotTables("Pivottabel23").DataPivotField.PivotItems( _
"Antal af FACULTY_ID").Caption = "Procentvis"
Range("B7").Select
ActiveSheet.PivotTables("Pivottabel23").CompactLayoutColumnHeader = "Fakultet"
Range("A13").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel23").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R13C1", TableName:= _
"Pivottabel24", DefaultVersion:=6
Sheets("Statistics").Select
Cells(13, 1).Select
ActiveSheet.PivotTables("Pivottabel24").AddDataField ActiveSheet.PivotTables( _
"Pivottabel24").PivotFields("ENROLL_LOCATION_NAME"), _
"Antal af ENROLL_LOCATION_NAME", xlCount
With ActiveSheet.PivotTables("Pivottabel24").PivotFields("ENROLL_LOCATION_NAME" _
)
.Orientation = xlRowField
.Position = 1
End With
Range("B13").Select
ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems( _
"Antal af ENROLL_LOCATION_NAME").Caption = "Antal"
Range("A13").Select
ActiveSheet.PivotTables("Pivottabel24").CompactLayoutRowHeader = "Campus"
Range("B13").Select
ActiveSheet.PivotTables("Pivottabel24").DataPivotField.PivotItems("Antal"). _
Caption = "Antal af studerende"
Range("A22").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel24").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R22C1", TableName:= _
"Pivottabel25", DefaultVersion:=6
Sheets("Statistics").Select
Cells(22, 1).Select
ActiveSheet.PivotTables("Pivottabel25").AddDataField ActiveSheet.PivotTables( _
"Pivottabel25").PivotFields("ENROLL_LOCATION_NAME"), _
"Antal af ENROLL_LOCATION_NAME", xlCount
With ActiveSheet.PivotTables("Pivottabel25").PivotFields("ENROLL_LOCATION_NAME" _
)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("Pivottabel25").PivotFields( _
"Antal af ENROLL_LOCATION_NAME")
.Calculation = xlPercentOfTotal
.NumberFormat = "0.00%"
End With
Range("A22").Select
ActiveSheet.PivotTables("Pivottabel25").CompactLayoutRowHeader = "Campus"
Range("B22").Select
ActiveSheet.PivotTables("Pivottabel25").DataPivotField.PivotItems( _
"Antal af ENROLL_LOCATION_NAME").Caption = "Procentvis af studerende"
Range("I1").Select
ActiveWorkbook.Worksheets("Statistics").PivotTables("Pivottabel25").PivotCache. _
CreatePivotTable TableDestination:="Statistics!R1C9", TableName:= _
"Pivottabel26", DefaultVersion:=6
Sheets("Statistics").Select
Cells(1, 9).Select
ActiveSheet.PivotTables("Pivottabel26").AddDataField ActiveSheet.PivotTables( _
"Pivottabel26").PivotFields("STUDYBOARD_ID"), "Antal af STUDYBOARD_ID", xlCount
With ActiveSheet.PivotTables("Pivottabel26").PivotFields("STUDYBOARD_ID")
.Orientation = xlRowField
.Position = 1
End With
Range("I1").Select
ActiveSheet.PivotTables("Pivottabel26").CompactLayoutRowHeader = "Studienævn"
Range("J1").Select
ActiveSheet.PivotTables("Pivottabel26").DataPivotField.PivotItems( _
"Antal af STUDYBOARD_ID").Caption = "Antal af studerende"
Range("L15").Select
End Sub
The problem is this piece of code:
Sheets.Add After:=ActiveSheet
On Error Resume Next
Sheets.Add().Name = "Statistics"
On Error GoTo 0
This basically tells Excel to add a new sheet after the active sheet and when you run the macro the second time, the sheetname "Statistics" is already taken. (And if it wasn't for On error resume next, an error message would appear the second time). Add this at the top of your macro instead:
Dim newSheet As Worksheet
Application.DisplayAlerts = False
Set newSheet = Sheets.Add(After:=ActiveSheet)
With newSheet
On Error Resume Next
ThisWorkbook.Sheets("Statistics").Delete
On Error GoTo 0
.name = "Statistics"
End With
Application.DisplayAlerts = True
I need help!
In order to convert a table to a list I am using the following VBA formula. It was created by using record macro and the PivotTable Wizard (not the most elegant solution), but it works.
ActiveWorkbook.PivotCaches.Create(SourceType:=xlConsolidation, SourceData:= _
Array("PasteSheet!R1C1:R300C200"), Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="", TableName:="PivotTable3", _
DefaultVersion:=xlPivotTableVersion14
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable3").DataPivotField.PivotItems( _
"Count of Value").Position = 1
ActiveSheet.PivotTables("PivotTable3").PivotFields("Row").Orientation = _
xlHidden
ActiveSheet.PivotTables("PivotTable3").PivotFields("Column").Orientation = _
xlHidden
Range("A4").Select
Selection.ShowDetail = True
My issue is I want to be able to set the SourceData to reference a stored variable as the source data range will change each time the macro is run, but I can't get it to work and googled everywhere with no result. My best shot was trying the following.
Dim newRange As Variant
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
Set newRange = Selection
ActiveWorkbook.PivotCaches.Create(SourceType:=xlConsolidation, SourceData:= _
newRange, Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="", TableName:="PivotTable3", _
DefaultVersion:=xlPivotTableVersion14
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable3").DataPivotField.PivotItems( _
"Count of Value").Position = 1
ActiveSheet.PivotTables("PivotTable3").PivotFields("Row").Orientation = _
xlHidden
ActiveSheet.PivotTables("PivotTable3").PivotFields("Column").Orientation = _
xlHidden
Range("A4").Select
Selection.ShowDetail = True
Help would be much appreciated!
You need to reference the source array differently. I am sure there is a better way than my example below, but this does work
Dim newRange As Range
Range("A1").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlToRight)).Select
newRange = Selection
ActiveWorkbook.PivotCaches.Create(SourceType:=xlConsolidation, SourceData:= Array("R" & newRange.Row & "C" & newRange.Column & ":R" & newRange.Row + newRange.Rows.Count - 1 & "C" & newRange.Column + newRange.Columns.Count - 1), Version:=xlPivotTableVersion14). _
CreatePivotTable TableDestination:="", TableName:="PivotTable3", DefaultVersion:=xlPivotTableVersion14
ActiveSheet.PivotTableWizard TableDestination:=ActiveSheet.Cells(3, 1)
ActiveSheet.Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable3").DataPivotField.PivotItems("Count of Value").Position = 1
ActiveSheet.PivotTables("PivotTable3").PivotFields("Row").Orientation = xlHidden
ActiveSheet.PivotTables("PivotTable3").PivotFields("Column").Orientation = xlHidden
Range("A4").Select
Selection.ShowDetail = True
I stumbled on this post as I was looking for a solution to a similar problem
anyways. to answer to this old post. I might help someone else.
The SourceData can be addressed in two ways. either the one that witchild used:
"nameofyourworksheet!R1C1:R" & Nbroftherowsofthesourcedataorarray & "C17", Version:=xlPivotTableVersion15).CreatePivotTable _'
or, the 2nd way:
ActiveWorkbook.Worksheets("nameofyourworksheet").Range("A1:Q" & Nbroftherowsofthesourcedataorarray).Address(, , xlR1C1, True), Version:=xlPivotTableVersion15).CreatePivotTable _
The code line: .Address(, , xlR1C1, True) means that the range will be converted to the format "R..C.." to make it works. I didnt try it but I trust the forum :p
hope I was able to help people searching for a solution to this problem
I have vba code that creates a pivot table automatically from some data that is copied from another workbook to the current workbook. When I run it on my machine, it is fine. Another person has no problems, either, but one woman is getting an error about "Run-time error '5': Invalid procedure call or argument" and it highlights my code for
"ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(3,1), TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
My code copies the new data sheet and renames it every time to "Call Data - Date Time" and creates a new pivot table on a new sheet every time, so I don't think it is the table name causing issues, especially since it runs fine for me. She is the only one having issues. Any ideas?
Here is all of the code:
Sub GeneratePivot()
'
' Macro11 Macro
'
Dim myDate As Date, aDate
myDate = Date + 7 - Weekday(Date)
aDate = Format(myDate, "mm.dd.yyyy")
Dim LValue As Date
LValue = Now
Range("A1").Select
Range(Selection, ActiveCell.SpecialCells(xlLastCell)).Select
ActiveWorkbook.Names.Add Name:="WorkRange", RefersTo:=Selection
Range("C5").Select
Application.Goto Reference:="WorkRange"
Sheets.Add
ActiveSheet.Name = "SummaryData " & Format(DateTime.Now, "MM.dd.yy hh.mm.ss")
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:=ActiveSheet.Cells(3, 1), TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
ActiveSheet.Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Month")
.Orientation = xlColumnField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Site/Location")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Called Number")
.Orientation = xlRowField
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Called Number"), "Count of Called Number", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Site/Location")
.PivotItems("#N/A").Visible = False
End With
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
End Sub
So I updated the suspect lines to:
Dim PC As PivotCache
Dim pt As PivotTable
Set PC = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"WorkRange", Version:=xlPivotTableVersionCurrent)
Set pt = PTCache.CreatePivotTable _
(TableDestination:=ActiveSheet.Cells(3, 1), _
TableName:="PivotTable1", _
DefaultVersion:=xlPivotTableVersionCurrent)
PC.CreatePivotTable TableDestination:=ActiveSheet.Cells(3, 1), TableName:="PivotTable1", DefaultVersion _ :=xlPivotTableVersionCurrent
And it failes on Set PC. I also tried xlPivotTableVersion15, but that failed, as well.
I think the problem is because of the version of the PivotTable namely this section: Version:=xlPivotTableVersion14
Version14 is for Excel 2010. I'm not sure what the version number is for 2012.
Try changing it to:
DefaultVersion:=xlPivotTableVersionCurrent and see if it runs.
Here is a list (taken from here: http://msdn.microsoft.com/en-us/library/microsoft.office.interop.excel.xlpivottableversionlist(v=office.15).aspx) with the available versions. I don't see Excel 2012 on there, so your mileage may vary:
xlPivotTableVersion2000 refers to Excel 2000
xlPivotTableVersion10 refers to Excel 2002
xlPivotTableVersion11 refers to Excel 2003
xlPivotTableVersion12 refers to Excel 2007
xlPivotTableVersion14 refers to Excel 2010
xlPivotTableVersion15 refers to Excel 2013
xlPivotTableVersionCurrent refers to Provided only for backward compatibility
I'm trying to create my first VBA code to basically:
-select a cell
-sort the column by A-Z
-pivot the data
-create a line chart
-re-size the chart to make it bigger
I'm getting a runtime error 9 message subscript out of range and it highlights the following line
ActiveWorkbook.Worksheets("advertiserConversionReport_3045").Sort.SortFields. _
Clear
What I did was to open another workbook and tried to run the macro on the data.
Looking through the code (I'm not a developer) I'm seeing that the errors could be the reference to the initial workbook advertiserConversionReport_3045 and perhaps a previously set range for the pivot Range("A2:F97")
Sub ActionReport()
'
' ActionReport Macro
' This macro will pivot data from the action report and create a line chart to show trending of credited conversions by day.
'
' Keyboard Shortcut: Ctrl+shift+a
'
Range("B1").Select
ActiveWorkbook.Worksheets("advertiserConversionReport_3045").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("advertiserConversionReport_3045").Sort.SortFields. _
Add Key:=Range("B1"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("advertiserConversionReport_3045").Sort
.SetRange Range("A2:F97")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"advertiserConversionReport_3045!R1C1:R97C6", Version:=xlPivotTableVersion14) _
.CreatePivotTable TableDestination:="Sheet1!R3C1", TableName:="PivotTable1" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Day")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Conversion Action")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Credited Conversions"), _
"Sum of Credited Conversions", xlSum
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Conversion Action")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$R$11")
ActiveWorkbook.ShowPivotTableFieldList = False
ActiveSheet.Shapes("Chart 1").ScaleWidth 1.6583333333, msoFalse, _
msoScaleFromTopLeft
ActiveSheet.Shapes("Chart 1").ScaleHeight 1.7065974045, msoFalse, _
msoScaleFromTopLeft
ActiveWindow.SmallScroll Down:=-24
ActiveSheet.Shapes("Chart 1").IncrementLeft -38.25
ActiveSheet.Shapes("Chart 1").IncrementTop -81.75
End Sub`
Does anyone know how to fix this and execute the macro successfully?
You are getting that error because the "Activeworkbook" doesn't have the sheet called "advertiserConversionReport_3045".
Avoid using ActiveWorkbook. Create Objects and then work with them. You may want to see this link
Try something like this
Sub Sample()
Dim thiswb As Workbook, wbNew As Workbook
Set thiswb = ThisWorkbook
'~~> Change as applicable
Set wbNew = Workbooks.Open("C:\Sample.xlsm")
With wbNew
.Worksheets("advertiserConversionReport_3045").Sort.SortFields.Clear
End With
End Sub
I am trying to record a macro which will create pivot chart out of excel data and here is the code that has been recorded:
Sub chart1()
'
' chart1 Macro
'
'
Range("E1:F11").Select
Sheets.Add
In Debugger, code within the **** **** is shown in Yellow color
***** ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"data!R1C5:R11C6", Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12 ********
Sheets("Sheet1").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields("question1")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("answer1"), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1").Orientation = _
xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields("answer1")
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub
Why am I getting the Run Time Error 1004: Application-Defined or Object Defined Error when i try to run this macro ?
Thanks in advance..
Since you want the chart to be on a new sheet,
you have to change the macro's "Sheet1" to new worksheet's name,
The following macro should work for you, I have named the new worksheet as newWs
And fyi, your macro's error message I believe is due to trying to creating 2 pivot table of the same name on "Sheet1" is not allowed.
He also like to know what to create pivotTable base on Selected Area, so I have done modification to the code.
Edited: I Assume you select 2 columns each time
Sub chart1()
'
' chart1 Macro
'
'
Dim selectedSheetName As String
Dim newWs As Worksheet
Dim rangeName As String
Dim header1 As String
Dim header2 As String
header1 = ActiveSheet.Cells(1, Selection.Column).Value
header2 = ActiveSheet.Cells(1, Selection.Column + 1).Value
selectedSheetName = ActiveSheet.Name
rangeName = Selection.Address
Set newWs = Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
selectedSheetName & "!" & rangeName, Version:=xlPivotTableVersion12).CreatePivotTable _
TableDestination:=newWs.Name & "!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion12
newWs.Activate
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header1)
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields(header2), "Count of answer1", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlPageField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields(header2).Orientation = _
xlHidden
With ActiveSheet.PivotTables("PivotTable1").PivotFields(header2)
.Orientation = xlColumnField
.Position = 1
End With
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range(newWs.Name & "!$A$3:$D$6")
ActiveChart.ChartType = xlColumnClustered
End Sub