I have a Macro which was working fine for months and about a week ago I got a message to say there were updates which needed to be applied to office home and business 2016.
Following the updates the macro crashes
I recorded the actions again from scratch and saved to a new macro. This also crashes.
Basically I have a csv txt file which is imported into sheet (TCB) and then 3 pivot tables created on a second sheet using the same data source but filtered for different operators
the first table is created ok and the macro stops when creating the second table. However the debug window highlights a line in code before the first table is created.
The Arrow and Yellow highlight is on the line
Cells.EntireColumn.AutoFit
About 42 lines down
any guidance would be great.
Entire code below
Following the updates the macro crashes
I recorded the actions again from scratch and saved to a new macro. This also crashes.
Basically I have a csv txt file which is imported into sheet (TCB) and then 3 pivot tables created on a second sheet using the same data source but filtered for different operators
the first table is created ok and the macro stops when creating the second table. However the debug window highlights a line in code before the first table is created.
The Arrow and Yellow highlight is on the line
Cells.EntireColumn.AutoFit
About 42 lines down
any guidance would be great.
Entire code below
Code:
Sub cashbooks()
'
' cashbooks Macro
'
' Keyboard Shortcut: Ctrl+Shift+J
'
ChDir "Z:"
Workbooks.OpenText Filename:="Z:\tcb.txt", Origin:=xlMSDOS, StartRow:=1, _
DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter _
:=False, Tab:=True, Semicolon:=False, Comma:=False, Space:=False, _
Other:=True, OtherChar:="~", 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), Array(16, 1), _
Array(17, 1), Array(18, 1), Array(19, 1), Array(20, 1), Array(21, 1), Array(22, 1), Array( _
23, 1), Array(24, 1), Array(25, 1), Array(26, 1), Array(27, 1), Array(28, 1), Array(29, 1), _
Array(30, 1), Array(31, 1), Array(32, 1), Array(33, 1), Array(34, 1), Array(35, 1), Array( _
36, 1), Array(37, 1), Array(38, 1), Array(39, 1), Array(40, 1)), TrailingMinusNumbers _
:=True
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 12
ActiveWindow.ScrollColumn = 11
ActiveWindow.ScrollColumn = 10
ActiveWindow.ScrollColumn = 9
ActiveWindow.ScrollColumn = 8
ActiveWindow.ScrollColumn = 7
ActiveWindow.ScrollColumn = 6
ActiveWindow.ScrollColumn = 5
ActiveWindow.ScrollColumn = 4
ActiveWindow.ScrollColumn = 3
ActiveWindow.ScrollColumn = 2
ActiveWindow.ScrollColumn = 1
Cells.Select
Cells.EntireColumn.AutoFit
Columns("G:L").Select
Selection.Delete Shift:=xlToLeft
Columns("I:I").Select
Selection.Delete Shift:=xlToLeft
Columns("K:M").Select
Selection.Delete Shift:=xlToLeft
Columns("M:S").Select
Selection.Delete Shift:=xlToLeft
'Columns("N:O").Select
'Selection.Delete Shift:=xlToLeft
Range("A1").Select
Selection.AutoFilter
Range("O12").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"tcb!R1C1:R1048576C13", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTable1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 1).Select
On Error Resume Next
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Op")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Cash")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Cheque")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Card")
.Orientation = xlRowField
.Position = 4
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Bank")
.Orientation = xlRowField
.Position = 5
End With
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Cash"), "Count of Cash", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Cheque"), "Count of Cheque", xlCount
ActiveSheet.PivotTables("PivotTable1").AddDataField ActiveSheet.PivotTables( _
"PivotTable1").PivotFields("Card"), "Count of Card", xlCount
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Cash")
.Caption = "Sum of Cash"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Cheque")
.Caption = "Sum of Cheque"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Count of Card")
.Caption = "Sum of Card"
.Function = xlSum
End With
Range("B3:D19").Select
Selection.Style = "Currency"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Op")
.PivotItems("(blank)").Visible = False
End With
Range("F3").Select
ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable1").PivotCache. _
CreatePivotTable TableDestination:="Sheet1!R3C6", TableName:="PivotTable2" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 6).Select
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Op")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cash")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Cheque")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Card")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Cash"), "Count of Cash", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Cheque"), "Count of Cheque", xlCount
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _
"PivotTable2").PivotFields("Card"), "Count of Card", xlCount
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Cash")
.Caption = "Sum of Cash"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Cheque")
.Caption = "Sum of Cheque"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Count of Card")
.Caption = "Sum of Card"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable2").PivotFields("Op")
.PivotItems("35").Visible = False
.PivotItems("36").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("K3").Select
ActiveWorkbook.Worksheets("Sheet1").PivotTables("PivotTable2").PivotCache. _
CreatePivotTable TableDestination:="Sheet1!R3C11", TableName:="PivotTable3" _
, DefaultVersion:=xlPivotTableVersion14
Sheets("Sheet1").Select
Cells(3, 11).Select
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Op")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Cash")
.Orientation = xlRowField
.Position = 2
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Cheque")
.Orientation = xlRowField
.Position = 3
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Card")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Cash"), "Count of Cash", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Cheque"), "Count of Cheque", xlCount
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Card"), "Count of Card", xlCount
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Cash")
.Caption = "Sum of Cash"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Cheque")
.Caption = "Sum of Cheque"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Count of Card")
.Caption = "Sum of Card"
.Function = xlSum
End With
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Op")
.PivotItems("2").Visible = False
.PivotItems("4").Visible = False
.PivotItems("3").Visible = False
.PivotItems("6").Visible = False
.PivotItems("7").Visible = False
.PivotItems("8").Visible = False
.PivotItems("12").Visible = False
.PivotItems("22").Visible = False
.PivotItems("13").Visible = False
.PivotItems("25").Visible = False
.PivotItems("30").Visible = False
.PivotItems("31").Visible = False
.PivotItems("32").Visible = False
.PivotItems("33").Visible = False
.PivotItems("34").Visible = False
.PivotItems("37").Visible = False
.PivotItems("38").Visible = False
.PivotItems("39").Visible = False
.PivotItems("(blank)").Visible = False
End With
Range("A2:D2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Total For Day"
Range("F2:I2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Newry Cash Book"
Range("K2:N2").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
ActiveCell.FormulaR1C1 = "Banbridge Cashbook"
Range("L21").Select
End Sub
Last edited by a moderator: