Hello,
I'm new to writing macros and i need to run a report which takes a large amount of data and presents it on a form. Ive used pivot tables and lookups but the macro takes a long time to run. Could anyone help in making my code a bit sleeker?
Sub Intorducer()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
'<o></o>
' Intorducer Macro<o></o>
'<o></o>
Columns("B:C").Select<o></o>
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("B1").Select<o></o>
Range("B1", Range("A65536").End(xlUp).Offset(, 1)).Formula = "=Left(A1,5)"<o></o>
Range("C2").Select<o></o>
Range("C2", Range("B65536").End(xlUp).Offset(, 1)).Formula = "=If(B2>0,vlookup(B2,'Date Table'!A:B,2,))"<o></o>
Range("C1").Select<o></o>
ActiveCell.FormulaR1C1 = "DATE"<o></o>
Columns("C:C").Select<o></o>
Selection.NumberFormat = "mmm/yy"<o></o>
Range("B1").Select<o></o>
ActiveCell.FormulaR1C1 = "VALUE"<o></o>
Range("B:B").Select<o></o>
With Selection<o></o>
Selection.NumberFormat = "General"<o></o>
.Value = .Value<o></o>
End With<o></o>
Columns("I:J").Select<o></o>
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("I1").Select<o></o>
Range("I1", Range("H65536").End(xlUp).Offset(, 1)).Formula = "=Left(H1,5)"<o></o>
Range("J2").Select<o></o>
Range("J2", Range("I65536").End(xlUp).Offset(, 1)).Formula = "=If(I2>0,vlookup(I2,'Date Table'!A:B,2,))"<o></o>
Range("J1").Select<o></o>
ActiveCell.FormulaR1C1 = "DATE 2"<o></o>
Columns("J:J").Select<o></o>
Selection.NumberFormat = "mmm/yy"<o></o>
Range("I1").Select<o></o>
ActiveCell.FormulaR1C1 = "VALUE 2"<o></o>
Range("I:I").Select<o></o>
With Selection<o></o>
Selection.NumberFormat = "General"<o></o>
.Value = .Value<o></o>
End With<o></o>
Rows("2:13").Select<o></o>
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("J2").Select<o></o>
ActiveCell.FormulaR1C1 = "May-2010"<o></o>
Range("J3").Select<o></o>
ActiveCell.FormulaR1C1 = "Jun-2010"<o></o>
Range("J4").Select<o></o>
ActiveCell.FormulaR1C1 = "Jul-2010"<o></o>
Range("J5").Select<o></o>
ActiveCell.FormulaR1C1 = "Aug-2010"<o></o>
Range("J6").Select<o></o>
ActiveCell.FormulaR1C1 = "Sep-2010"<o></o>
Range("J7").Select<o></o>
ActiveCell.FormulaR1C1 = "Oct-2010"<o></o>
Range("J8").Select<o></o>
ActiveCell.FormulaR1C1 = "Nov-2010"<o></o>
Range("J9").Select<o></o>
ActiveCell.FormulaR1C1 = "Dec-2010"<o></o>
Range("J10").Select<o></o>
ActiveCell.FormulaR1C1 = "Jan-2011"<o></o>
Range("J11").Select<o></o>
ActiveCell.FormulaR1C1 = "Feb-2011"<o></o>
Range("J12").Select<o></o>
ActiveCell.FormulaR1C1 = "Mar-2011"<o></o>
Range("J13").Select<o></o>
ActiveCell.FormulaR1C1 = "Apr-2011"<o></o>
Range("J14").Select<o></o>
Range("F2").Select<o></o>
ActiveCell.FormulaR1C1 = "KFC"<o></o>
Range("F2").Select<o></o>
Selection.AutoFill Destination:=Range("F2:F13"), Type:=xlFillDefault<o></o>
Range("F2:F13").Select<o></o>
Range("F12").Select<o></o>
Range("A1").Select<o></o>
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _<o></o>
"Data!R1C1:R1048576C18", Version:=xlPivotTableVersion12).CreatePivotTable _<o></o>
TableDestination:="Pipeline!R1C1", TableName:="PivotTable2", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("Pipeline").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable2").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable2").PivotFields("DATE"), "Count of DATE", xlCount<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="Rejects!R1C1", TableName:="PivotTable3" _<o></o>
, DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("Rejects").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable3").PivotFields("SUB STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable3").PivotFields("DATE"), "Count of DATE", xlCount<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="COMs!R1C1", TableName:="PivotTable4", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("COMs").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable4").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
Columns("A:A").Select<o></o>
Selection.Delete Shift:=xlToLeft<o></o>
Range("A1").Select<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="COMs!R1C1", TableName:="PivotTable6", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("COMs").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable6").PivotFields("DATE 2")<o></o>
.Orientation = xlColumnField<o></o>
.Position = 1<o></o>
End With<o></o>
With ActiveSheet.PivotTables("PivotTable6").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable6").PivotFields("STATUS DATE"), "Count of STATUS DATE", xlCount<o></o>
Sheets(Array("Date Table", "Pipeline", "Rejects", "COMs", "Data")). _<o></o>
Select<o></o>
Sheets("Date Table").Activate<o></o>
ActiveWindow.SelectedSheets.Visible = False<o></o>
Sheets("Cover").Select<o></o>
<o> </o>
End Sub<o></o>
I Know its long winded but any help or ideas would be appreciated!!
I'm new to writing macros and i need to run a report which takes a large amount of data and presents it on a form. Ive used pivot tables and lookups but the macro takes a long time to run. Could anyone help in making my code a bit sleeker?
Sub Intorducer()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
'<o></o>
' Intorducer Macro<o></o>
'<o></o>
Columns("B:C").Select<o></o>
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("B1").Select<o></o>
Range("B1", Range("A65536").End(xlUp).Offset(, 1)).Formula = "=Left(A1,5)"<o></o>
Range("C2").Select<o></o>
Range("C2", Range("B65536").End(xlUp).Offset(, 1)).Formula = "=If(B2>0,vlookup(B2,'Date Table'!A:B,2,))"<o></o>
Range("C1").Select<o></o>
ActiveCell.FormulaR1C1 = "DATE"<o></o>
Columns("C:C").Select<o></o>
Selection.NumberFormat = "mmm/yy"<o></o>
Range("B1").Select<o></o>
ActiveCell.FormulaR1C1 = "VALUE"<o></o>
Range("B:B").Select<o></o>
With Selection<o></o>
Selection.NumberFormat = "General"<o></o>
.Value = .Value<o></o>
End With<o></o>
Columns("I:J").Select<o></o>
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("I1").Select<o></o>
Range("I1", Range("H65536").End(xlUp).Offset(, 1)).Formula = "=Left(H1,5)"<o></o>
Range("J2").Select<o></o>
Range("J2", Range("I65536").End(xlUp).Offset(, 1)).Formula = "=If(I2>0,vlookup(I2,'Date Table'!A:B,2,))"<o></o>
Range("J1").Select<o></o>
ActiveCell.FormulaR1C1 = "DATE 2"<o></o>
Columns("J:J").Select<o></o>
Selection.NumberFormat = "mmm/yy"<o></o>
Range("I1").Select<o></o>
ActiveCell.FormulaR1C1 = "VALUE 2"<o></o>
Range("I:I").Select<o></o>
With Selection<o></o>
Selection.NumberFormat = "General"<o></o>
.Value = .Value<o></o>
End With<o></o>
Rows("2:13").Select<o></o>
Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove<o></o>
Range("J2").Select<o></o>
ActiveCell.FormulaR1C1 = "May-2010"<o></o>
Range("J3").Select<o></o>
ActiveCell.FormulaR1C1 = "Jun-2010"<o></o>
Range("J4").Select<o></o>
ActiveCell.FormulaR1C1 = "Jul-2010"<o></o>
Range("J5").Select<o></o>
ActiveCell.FormulaR1C1 = "Aug-2010"<o></o>
Range("J6").Select<o></o>
ActiveCell.FormulaR1C1 = "Sep-2010"<o></o>
Range("J7").Select<o></o>
ActiveCell.FormulaR1C1 = "Oct-2010"<o></o>
Range("J8").Select<o></o>
ActiveCell.FormulaR1C1 = "Nov-2010"<o></o>
Range("J9").Select<o></o>
ActiveCell.FormulaR1C1 = "Dec-2010"<o></o>
Range("J10").Select<o></o>
ActiveCell.FormulaR1C1 = "Jan-2011"<o></o>
Range("J11").Select<o></o>
ActiveCell.FormulaR1C1 = "Feb-2011"<o></o>
Range("J12").Select<o></o>
ActiveCell.FormulaR1C1 = "Mar-2011"<o></o>
Range("J13").Select<o></o>
ActiveCell.FormulaR1C1 = "Apr-2011"<o></o>
Range("J14").Select<o></o>
Range("F2").Select<o></o>
ActiveCell.FormulaR1C1 = "KFC"<o></o>
Range("F2").Select<o></o>
Selection.AutoFill Destination:=Range("F2:F13"), Type:=xlFillDefault<o></o>
Range("F2:F13").Select<o></o>
Range("F12").Select<o></o>
Range("A1").Select<o></o>
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _<o></o>
"Data!R1C1:R1048576C18", Version:=xlPivotTableVersion12).CreatePivotTable _<o></o>
TableDestination:="Pipeline!R1C1", TableName:="PivotTable2", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("Pipeline").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable2").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable2").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable2").PivotFields("DATE"), "Count of DATE", xlCount<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="Rejects!R1C1", TableName:="PivotTable3" _<o></o>
, DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("Rejects").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable3").PivotFields("SUB STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable3").PivotFields("DATE"), "Count of DATE", xlCount<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="COMs!R1C1", TableName:="PivotTable4", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("COMs").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable4").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
Columns("A:A").Select<o></o>
Selection.Delete Shift:=xlToLeft<o></o>
Range("A1").Select<o></o>
Sheets("Data").Select<o></o>
ActiveWorkbook.Worksheets("Pipeline").PivotTables("PivotTable2").PivotCache. _<o></o>
CreatePivotTable TableDestination:="COMs!R1C1", TableName:="PivotTable6", _<o></o>
DefaultVersion:=xlPivotTableVersion12<o></o>
Sheets("COMs").Select<o></o>
Cells(1, 1).Select<o></o>
With ActiveSheet.PivotTables("PivotTable6").PivotFields("DATE 2")<o></o>
.Orientation = xlColumnField<o></o>
.Position = 1<o></o>
End With<o></o>
With ActiveSheet.PivotTables("PivotTable6").PivotFields("STATUS")<o></o>
.Orientation = xlRowField<o></o>
.Position = 1<o></o>
End With<o></o>
ActiveSheet.PivotTables("PivotTable6").AddDataField ActiveSheet.PivotTables( _<o></o>
"PivotTable6").PivotFields("STATUS DATE"), "Count of STATUS DATE", xlCount<o></o>
Sheets(Array("Date Table", "Pipeline", "Rejects", "COMs", "Data")). _<o></o>
Select<o></o>
Sheets("Date Table").Activate<o></o>
ActiveWindow.SelectedSheets.Visible = False<o></o>
Sheets("Cover").Select<o></o>
<o> </o>
End Sub<o></o>
I Know its long winded but any help or ideas would be appreciated!!