Using Macros to create a pivot table with the click of a button. I download new reports every day so it's a different Excel worksheet and sheet name each time.
I've managed to get around the fact that it's a different workbook name by simply renaming the relevant tab "data" each time. I've gotten it to create the pivot table I want, except it's giving me "Count" instead of "Sum" like I need. The error I get is
Run-time error '424':
Object required
The highlighted line is .Position = 1 near the end and I'm not sure what's broken here.
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), _
Array(7, 1), Array(8, 1), Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), Array(13, 1 _
), Array(14, 1), Array(15, 1))
Cells.Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"data!R1C1:R1048576C15", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable2", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Date"), "Count of Date", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Store Listing Visitors"), _
"Count of Store Listing Visitors", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Installers"), "Count of Installers", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Date")
.Orientation = xlRowField
.Position = 1
End With
ExecuteExcel4Macro _
"PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Store Listing Visitors"",,,2)"
ExecuteExcel4Macro _
"PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Installers"",,,2)"
I appreciate your help in advance!
here is a rewrite of your code
i think that you would have an easier time debugging, if you formatted your code in a more legible way.
Sub pivTest()
Columns("A:A").TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=True, _
Semicolon:=False, _
Comma:=True, _
Space:=False, _
Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), _
Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1), _
Array(13, 1), Array(14, 1), Array(15, 1))
' Cells.Select ' do not use
Sheets.Add ' what is the name of this sheet ?
ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:="data!R1C1:R1048576C15", _
Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet2!R3C1", _
TableName:="PivotTable2", _
DefaultVersion:=xlPivotTableVersion14
' Sheets("Sheet1").Select ' do not use
' Cells(3, 1).Select ' do not use
Dim pt As PivotTable
Set pt = Sheets("Sheet1").PivotTables("PivotTable2")
pt.AddDataField pt.PivotFields("Date"), "Count of Date", xlCount
pt.AddDataField pt.PivotFields("Store Listing Visitors"), "Count of Store Listing Visitors", xlCount
pt.AddDataField pt.PivotFields("Installers"), "Count of Installers", xlCount
pt.PivotFields("Count of Date").Orientation = xlRowField
pt.PivotFields("Date").Position = 1 ' it is not "Count of Date" anymore, because previous line moved it out of the "Sum Values"
ExecuteExcel4Macro "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Store Listing Visitors"",,,2)"
ExecuteExcel4Macro "PIVOT.FIELD.PROPERTIES(""PivotTable2"",""Count of Installers"",,,2)"
End Sub
Related
I have code that opens a .txt file saved off of the desktop. It works great unless the user has OneDrive, then I receive an error. Is there a way to make the code dynamic to recognise if it is a OneDrive environment or not?
Sub OPENER()
'
' new_open Macro
'
'
user = Environ("Username")
ChDir "C:\Users\" & user & "\Desktop"
Workbooks.OpenText Filename:="C:\Users\" & user & "\Desktop\next.txt", Origin:=437 _
, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, Comma:=True, _
Space:=False, Other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array( _
3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1)), _
TrailingMinusNumbers:=True
End Sub
Hi I am trying to create a code which converts column A to columns with a | delimiter and want column A to be in Text format.
My code:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").Select
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|"
FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
It doesn't seem to like this at the FieldInfo part.
Please help
You are missing the , _ after OtherChar line:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").Select
Selection.TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|", _
FieldInfo _
:=Array(1, 1), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
However, to also fix the rest of the code:
Select and selection are unnecessary, slow things down and can cause errors. There are articles in this forum as to how and why to avoid them.
To format a column as text, xlTextFormat returns a 2, so your FieldInfo should be a bit different:
Sub TEST_Text_to_Columns()
ActiveSheet.Range("A:A").TextToColumns _
Destination:=Range("A1"), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=False, _
Comma:=False, _
Space:=False, _
Other:=True, _
OtherChar:="|", _
FieldInfo _
:=Array(1, 2), TrailingMinusNumbers:=True
ActiveSheet.Columns("A:GZ").AutoFit
End Sub
So basically I have multiple columns of text that I want to change to numbers.
Now the issue i face is ive got the code but i dont understand how to run a loop on it to choose the next third column.
This is my code:
Sub Texscolumn()
Range("AI2:AI96").Select
Selection.TextToColumns Destination:=Range("AI2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=True, Other:=False, FieldInfo _
:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), TrailingMinusNumbers:= _
True
End Sub
So the next column I want is suppose AJ2:AJ96.
I cant seem to figure out how to make a for loop with changing columns like this.
At least 2 options :
Sub Texscolumn()
Dim j As Integer
With Sheets("sheet1")
For j = 35 To 36
.Range(.Cells(2, j), .Cells(96, j)).TextToColumns Destination:=.Cells(2, j), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=True, _
Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), _
TrailingMinusNumbers:=True
Next i
End With
End Sub
or
Sub Texscolumn()
Dim Rg As Range
Dim i As Integer
Set Rg = Sheets("sheet1").Range("AI2:AI96")
With Rg
For i = 1 To 2
.TextToColumns Destination:=.Cells(1, 1), _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=True, _
Tab:=True, _
Semicolon:=False, _
Comma:=False, _
Space:=True, _
Other:=False, _
FieldInfo:=Array(Array(1, 1), Array(2, 9), Array(3, 9), Array(4, 9), Array(5, 9), Array(6, 9), Array(7, 9), Array(8, 9)), _
TrailingMinusNumbers:=True
Set Rg = .Offset(0, 1)
Next i
End With
End Sub
I am trying to run the Text to Column function. I recorded a macro and it appears to work for the first attempt in the cell I recorded the macro for. I need this macro to run for any cells I select so I changed the range to "ActiveCell" but this seems to break the code.
Recorded Macro:
Selection.TextToColumns Destination:=Range("X32"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
End Sub
Selection.TextToColumns Destination:=ActiveCell, DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), TrailingMinusNumbers:=True
works for me when I select a cell whose text have semicolons and it gets split into many columns
In order for the TextToColumns to work, I hope you selected a single column as the source for this funtion to work, i.e selecting cells "E5:E10" will work.
Selecting multiple columns as an Input will result with a run-time error.
Documentation link MSDN TextToColumns
Code
Sub TxtToCol()
Dim DestRng As Range
' setting the destination range using a variable
Set DestRng = Range("X32")
With Selection
.TextToColumns Destination:=DestRng, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, _
Semicolon:=True, _
Comma:=False, _
Space:=False, _
Other:=False, _
FieldInfo:=Array(Array(1, 2), Array(2, 1), Array(3, 1)), _
TrailingMinusNumbers:=True
End With
End Sub
I got another Problem with VBA. I used the TexttoColumns Sub to separate every Cell in my Column by semicolon. Now I wanted to insert Columns after that one, which should contain the separated values. It all worked pretty well in the beginning, but now it suddenly won't insert new cells, but overwrite the old ones.
Example (Wish) :
Row1 Row2 Row3
Tree;PC;House |Data1 |Data2 --> Tree|PC|House|Data1|Data2
Example(How it is):
Tree;PC;House|Data1|Data2 --> Tree|PC|House
Workbooks(Ziel).Worksheets(Zieltab).Columns(Spalte).TextToColumns Destination:=Columns(Spalte), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 2))
A Range.TextToColumns method does not insert columns. It will always overwrite data if allowed to proceed.
With Workbooks(Ziel).Worksheets(Zieltab).Columns(Spalte)
'insert two columns to the right
.Cells(1).Resize(1, 2).Offset(0, 1).EntireColumn.Insert
'split the first column into itself and the two new column
.TextToColumns Destination:=.Cells(1), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=True, Comma:=False, Space:=False, Other:=False, _
FieldInfo:=Array(Array(1, 2), Array(2, 2), Array(3, 2))
End With