Sub Create_Variance()
Sheets("Variance").Select
Range("K2").Select
ActiveCell.FormulaR1C1 = "=DATE(YEAR(TODAY()), MONTH(TODAY()), 1)"
Range("Q2").Select
ActiveCell.FormulaR1C1 = "=YEAR(RC[-6])"
Range("E2").Select
ActiveCell.FormulaR1C1 = "=TEXT(RC[6], ""mmmm"")"
Range("F2").Select
ActiveCell.FormulaR1C1 = "=MONTH(RC[5])"
Range("G2").Select
ActiveCell.FormulaR1C1 = "0"
Range("I2").Select
ActiveCell.FormulaR1C1 = "OK"
Range("J2").Select
ActiveCell.FormulaR1C1 = "0"
Range("L2").Select
ActiveCell.FormulaR1C1 = "0"
Range("M2").Select
ActiveCell.FormulaR1C1 = "0"
Range("N2").Select
ActiveCell.FormulaR1C1 = "0"
Range("O2").Select
ActiveCell.FormulaR1C1 = "0"
Range("E2:Q2").Select
Selection.Copy
Range("E2:Q500").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("A1").Select
Sheets("Var").Select
Range("A1").Select
Workbooks.Open Filename:="X:\SalesJournal\Variance.xls"
Workbooks.Open Filename:="X:\SalesJournal\Variance-1.xls"
' Here is where I'd like an if statement comparing two months and copy/pasting the information from Variance-1.xls to the Var-1 tab if they aren't 1 apart. Or everything below this could be a new macro and I could create another macro almost identical but it would NOT copy/paste the Variance-1 information. I would just need to write an if statement to run one of the two macros based on the comparison.
Range("A1:Q5000").Select
Selection.Copy
Windows("Variance_Setup.xlsm").Activate
Sheets("Var-1").Select
Range("A1").Select
ActiveSheet.Paste
Windows("Variance.xls").Activate
Range("A1:Q5000").Select
Application.CutCopyMode = False
Selection.Copy
Windows("Variance_Setup.xlsm").Activate
Sheets("Var").Select
Range("A1").Select
ActiveSheet.Paste
Windows("Variance.xls").Activate
Application.CutCopyMode = False
ActiveWindow.Close
Windows("Variance-1.xls").Activate
ActiveWindow.Close
Range("A1").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Var!R1C1:R1048576C17", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet1!R3C1", TableName:="PivotTableVar", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Var PT"
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTableVar")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
With ActiveSheet.PivotTables("PivotTableVar").PivotFields("CustomerCode")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTableVar").PivotFields("ProdGroup")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTableVar").AddDataField ActiveSheet.PivotTables( _
"PivotTableVar").PivotFields("Variance"), "Count of Variance", xlCount
With ActiveSheet.PivotTables("PivotTableVar").PivotFields("Count of Variance")
.Caption = "Sum of Variance"
.Function = xlSum
End With
Range("A7").Select
ActiveSheet.PivotTables("PivotTableVar").PivotFields("CustomerCode").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
Range("B7").Select
ActiveSheet.PivotTables("PivotTableVar").PivotFields("ProdGroup").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
Sheets("Var-1").Select
Range("A1").Select
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"Var-1!R1C1:R1048576C17", Version:=xlPivotTableVersion14).CreatePivotTable _
TableDestination:="Sheet2!R3C1", TableName:="PivotTableVar-1", DefaultVersion _
:=xlPivotTableVersion14
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Var-1 PT"
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTableVar-1")
.InGridDropZones = True
.RowAxisLayout xlTabularRow
End With
With ActiveSheet.PivotTables("PivotTableVar-1").PivotFields("CustomerCode")
.Orientation = xlRowField
.Position = 1
End With
With ActiveSheet.PivotTables("PivotTableVar-1").PivotFields("ProdGroup")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTableVar-1").AddDataField ActiveSheet.PivotTables( _
"PivotTableVar-1").PivotFields("Variance"), "Count of Variance", xlCount
With ActiveSheet.PivotTables("PivotTableVar-1").PivotFields("Count of Variance")
.Caption = "Sum of Variance"
.Function = xlSum
End With
Range("A7").Select
ActiveSheet.PivotTables("PivotTableVar-1").PivotFields("CustomerCode").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
Range("B7").Select
ActiveSheet.PivotTables("PivotTableVar-1").PivotFields("ProdGroup").Subtotals = _
Array(False, False, False, False, False, False, False, False, False, False, False, False)
Sheets("Var PT").Select
Columns("A:C").Select
Selection.Copy
Sheets("Calc").Select
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Var-1 PT").Select
Columns("A:C").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Calc").Select
Range("E1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("B:B,F:F").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("B5").Select
ActiveCell.FormulaR1C1 = "=RC[-1]"
Range("B5").Select
Selection.Copy
Range("G5").Select
ActiveSheet.Paste
Range("B6").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""", R[-1]C, RC[-1])"
Range("B6").Select
Selection.Copy
Range("B6:B500").Select
ActiveSheet.Paste
Range("G6:G500").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("A4").Select
Selection.Copy
Range("B4").Select
ActiveSheet.Paste
Range("F4").Select
Selection.Copy
Range("G4").Select
ActiveSheet.Paste
Range("A:A,F:F").Select
Selection.Delete Shift:=xlToLeft
Range("A:A,E:E").Select
Range("E1").Activate
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A5").Select
ActiveCell.FormulaR1C1 = "=CONCATENATE(RC[1], RC[2])"
Range("A5").Select
Selection.Copy
Range("F5").Select
ActiveSheet.Paste
Range("A5").Select
Application.CutCopyMode = False
Selection.AutoFill Destination:=Range("A5:A500"), Type:=xlFillDefault
Range("A5:A500").Select
Range("F5").Select
Selection.AutoFill Destination:=Range("F5:F500"), Type:=xlFillDefault
Range("F5:F500").Select
Range("G4:I4").Select
Selection.Copy
Range("K4").Select
ActiveSheet.Paste
Application.CutCopyMode = False
Range("M5").Select
ActiveCell.FormulaR1C1 = _
"=IF(ISERROR(VLOOKUP(RC[-12],C[-7]:C[-4], 4, FALSE)), RC[-9], IF(RC[-9]=VLOOKUP(RC[-12],C[-7]:C[-4], 4, FALSE), """", RC[-9]-VLOOKUP(RC[-12],C[-7]:C[-4], 4, FALSE)))"
Range("L5").Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]="""", """", RC[-9])"
Range("K5").Select
ActiveCell.FormulaR1C1 = "=IF(RC[2]="""", """", RC[-9])"
Range("K5:M5").Select
Selection.AutoFill Destination:=Range("K5:M500"), Type:=xlFillDefault
Range("K5:M500").Select
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
Range("K5:M500").Select
Selection.Copy
Sheets("Variance").Select
Range("A2").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]="""", x, IF(RC[1]=""Grand Total"", x, 0))"
Range("A1").Select
Selection.AutoFill Destination:=Range("A1:A500"), Type:=xlFillDefault
Range("A1:A500").Select
Range("A1").Select
Selection.SpecialCells(xlCellTypeFormulas, 16).Select
Selection.EntireRow.Delete
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
Range("A2:A500").Select
Selection.Cut
Range("H2").Select
ActiveSheet.Paste
Range("C2:C500").Select
Selection.Cut
Range("P2").Select
ActiveSheet.Paste
Range("B2:B500").Select
Selection.Cut
Range("C2").Select
ActiveSheet.Paste
Range("D2").Select
Workbooks.Open Filename:="X:\SalesJournal\CustomerList.xls"
Workbooks.Open Filename:="X:\SalesJournal\CustomerSalespersonList.xls"
Workbooks.Open Filename:="X:\SalesJournal\SalespersonList.xls"
Windows("Variance_Setup.xlsm").Activate
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[4], [CustomerList.xls]Sheet1!C1:C2, 2, FALSE)"
Range("D2").Select
Selection.AutoFill Destination:=Range("D2:D500"), Type:=xlFillDefault
Range("D2:D500").Select
Range("A2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[7], [CustomerSalespersonList.xls]Sheet1!C1:C2, 2, FALSE)"
Range("B2").Select
ActiveCell.FormulaR1C1 = _
"=VLOOKUP(RC[6], [CustomerSalespersonList.xls]Sheet1!C1:C3, 3, FALSE)"
Range("A2:B2").Select
Selection.AutoFill Destination:=Range("A2:B500"), Type:=xlFillDefault
Range("A2:B500").Select
Range("A:A").Select
Selection.SpecialCells(xlCellTypeFormulas, 16).Select
Selection.EntireRow.Delete
Range("A1").Select
Cells.Select
Selection.Copy
Range("A1").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Range("A4").Select
Application.CutCopyMode = False
Range("A1").Select
Windows("CustomerList.xls").Activate
ActiveWindow.Close
Windows("CustomerSalespersonList.xls").Activate
ActiveWindow.Close
Windows("SalespersonList.xls").Activate
ActiveWindow.Close
End Sub