SAM MADZLAN
New Member
- Joined
- Feb 20, 2018
- Messages
- 4
Hi, i'm in need for some assistance. my code is not working the macro is supposed to pivot the data but i don't know why the code doesn't run / can someone simplify the code.
Thanks in advance
Thanks in advance
Code:
Sub PREP()
'
' File preparation Macro
'
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim y As Long, x As Long, LastRow As Long, Target As Range
Set Target = Range("A7")
Sheets(8).Select
Application.ActiveSheet.name = VBA.Right(Target, 8)
ActiveSheet.Select
Range("A8").Select
Range(Selection, Selection.End(xlDown).Offset(-3)).Select
Selection.Copy
Sheets.add after:=ActiveSheet
Sheets(9).Select
Application.ActiveSheet.name = VBA.Right(Target, 8) & "_DATA"
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlFixedWidth, _
FieldInfo:=Array(Array(0, 1), Array(7, 1), Array(22, 1), Array(67, 1), Array(83, 1), _
Array(84, 1), Array(85, 1), Array(104, 1)), TrailingMinusNumbers:=True
Rows("2:2").Select
Selection.Delete Shift:=xlUp
Range("E1").Select
ActiveCell.FormulaR1C1 = "PROD"
Range("F1").Select
ActiveCell.FormulaR1C1 = "SEG"
LastRow = Cells(Rows.count, "A").End(xlUp).Row
y = Range(Selection, Selection.End(xlDown)).Rows.count
Range("E2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[-1],3)"
Selection.AutoFill Destination:=Range("E2:E" & LastRow & "")
Range("F2").Select
ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'PRODUCT CODE'!C[-6]:C[-5],2,0)"
Selection.AutoFill Destination:=Range("F2:F" & LastRow & "")
mymymy = VBA.Right(Target, 8) & "_DATA"
Range("A1").Select
Selection.CurrentRegion.Select
DataArea = "mymymy!R1C1:R" & Selection.Rows.count & "C" & Selection.Columns.count
Sheets.add after:=ActiveSheet
Sheets(10).Select
Application.ActiveSheet.name = VBA.Right(Target, 8) & "_PIVOT"
'Sheets(10).name = "PIVOT"
'Sheets("PIVOT").Select
Cells(1, 1).Select
sibat = VBA.Right(Target, 8) & "_PIVOT"
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"DataArea", Version:=4).CreatePivotTable TableDestination:= _
"", TableName:="PivotTable4", DefaultVersion:=4
With ActiveSheet.PivotTables("PivotTable4").PivotFields("SEG")
.Orientation = xlRowField
.Position = 1
With ActiveSheet.PivotTables("PivotTable4").PivotFields("PROD")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable4").AddDataField ActiveSheet.PivotTables( _
"PivotTable4").PivotFields("DEBIT AMOUNT (RM)"), "Sum of DEBIT AMOUNT (RM)", xlSum
End With
ActiveSheet.PivotTables("PivotTable4").PivotFields("Sum of DEBIT AMOUNT (RM)") _
.NumberFormat = "#,##0.00"
ActiveSheet.PivotTables("PivotTable4").PivotFields("Sum of DEBIT AMOUNT (RM)") _
.caption = "(RM)"
End Sub
Last edited by a moderator: