I am new to VBA. With my previous jobs I was able to make simple macros work with just recording them. They were for my personal use in the job. The new macro's I am trying to create will eventually be used by people who's experience with excel will range to zero, to basic formulas, i.e. calculator functions. Basically, I am pulling data report from ERP and formatting and filtering out irrelevant data and then creating a pivot table with applicable data for them to work with. My issue is that the rows of data will vary while the columns will stay the same. I need to turn these into dynamic ranges so it will select everything. I am having the same problem with my various conditional formatting not copying to the last row as well. I made several recorded macros, in case there is a data change I don't have to redo the entire macro. I know its probably not the most efficient, but the best I can do as I go self teaching myself VBA. I will list the macros in the order of which they are ran.
I hope to get to the point that I can understand enough to simplify the code. The very last bit of code is what I used for a more basic macro I made for a different sheet I am working on and it works for it. I think it should work for these macro's too but I am not sure where I should be entering it and which other codes I should be modifying to get it to work.
Thank you in advance!
I hope to get to the point that I can understand enough to simplify the code. The very last bit of code is what I used for a more basic macro I made for a different sheet I am working on and it works for it. I think it should work for these macro's too but I am not sure where I should be entering it and which other codes I should be modifying to get it to work.
Thank you in advance!
VBA Code:
Sub PO_Group_SO_1_Format()
'
' PO_Group_SO_1_Format Macro
'
'
Range("A1:T779").Select
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Selection.Columns.AutoFit
Range("E2").Select
ActiveWindow.SmallScroll ToRight:=4
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Select
ActiveCell.FormulaR1C1 = "Proj#"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[12], 11)"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I779")
Range("I2:I779").Select
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K1").Select
ActiveCell.FormulaR1C1 = "PO#"
Range("K2").Select
ActiveCell.FormulaR1C1 = "Righ"
Range("K2").Select
ActiveCell.FormulaR1C1 = "=RIGHT(RC[10], 11)"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K779")
Range("K2:K779").Select
Range("A1:V779").Select
Range("K2").Activate
Selection.Columns.AutoFit
Range("P1").Select
Selection.Columns.AutoFit
ActiveWindow.SmallScroll ToRight:=-8
Range("B1").Select
Selection.Columns.AutoFit
Range("E1").Select
Selection.Columns.AutoFit
Range("F1").Select
Selection.Columns.AutoFit
Range("H1").Select
Selection.Columns.AutoFit
Range("D1").Select
Selection.Columns.AutoFit
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Columns.AutoFit
Range("C1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("C:C").ColumnWidth = 21.71
Rows("1:1").Select
With Selection
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("A1").Select
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("C2:C779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("I2:I779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("J2:J779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("K2:K779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort
.SetRange Range("A1:V779")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
VBA Code:
Sub PO_Group_SO_2_CondFormat()
'
' PO_Group_SO_2_CondFormat Macro
'
'
Range("E2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(E2))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13421823
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="On File", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"Requires Review", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"Waiting on customer", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($E2=""On File"", $N2=$L2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("E2:E779"), Type:=xlFillFormats
Range("E2:E779").Select
Range("H2").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="Fully Billed", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("H2:H779"), Type:=xlFillFormats
Range("H2:H779").Select
Range("I2:K2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($O2<=$N2, $N2<>0, $O2<=$M2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("I2:K779"), Type:=xlFillFormats
Range("I2:K779").Select
Range("L2:O2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($M2=$N2, $N2=$O2, $M2<>0, $N2<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($O2<$N2, $N3<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=OR($M2>$N2, $M2<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 8420607
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("L2:O779"), Type:=xlFillFormats
Range("L2:O779").Select
Range("F2").Select
End Sub
VBA Code:
Sub PO_GROUP_SO_2_Pivot()
'
' PO_GROUP_SO_2_Pivot Macro
'
'
Range("A1:V779").Select
Application.CutCopyMode = False
Sheets.Add
ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:= _
"POListGroupedbySalesOrderResul!R1C1:R779C22", Version:=8).CreatePivotTable _
TableDestination:="Sheet2!R3C1", TableName:="PivotTable3", DefaultVersion _
:=8
Sheets("Sheet2").Select
Cells(3, 1).Select
With ActiveSheet.PivotTables("PivotTable3")
.ColumnGrand = True
.HasAutoFormat = True
.DisplayErrorString = False
.DisplayNullString = True
.EnableDrilldown = True
.ErrorString = ""
.MergeLabels = False
.NullString = ""
.PageFieldOrder = 2
.PageFieldWrapCount = 0
.PreserveFormatting = True
.RowGrand = True
.SaveData = True
.PrintTitles = False
.RepeatItemsOnEachPrintedPage = True
.TotalsAnnotation = False
.CompactRowIndent = 1
.InGridDropZones = False
.DisplayFieldCaptions = True
.DisplayMemberPropertyTooltips = False
.DisplayContextTooltips = True
.ShowDrillIndicators = True
.PrintDrillIndicators = False
.AllowMultipleFilters = False
.SortUsingCustomLists = True
.FieldListSortAscending = False
.ShowValuesRow = False
.CalculatedMembersInFilters = False
.RowAxisLayout xlCompactRow
End With
With ActiveSheet.PivotTables("PivotTable3").PivotCache
.RefreshOnFileOpen = False
.MissingItemsLimit = xlMissingItemsDefault
End With
ActiveSheet.PivotTables("PivotTable3").RepeatAllLabels xlRepeatLabels
With ActiveSheet.PivotTables("PivotTable3").PivotFields( _
"Project: Next Review Date")
.Orientation = xlRowField
.Position = 1
End With
ActiveSheet.PivotTables("PivotTable3").PivotFields("Project: Next Review Date" _
).Subtotals = Array(False, False, False, False, False, False, False, False, False, False _
, False, False)
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Proj#")
.Orientation = xlRowField
.Position = 2
End With
ActiveSheet.PivotTables("PivotTable3").PivotFields("Proj#").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable3").PivotFields("SO#")
.Orientation = xlRowField
.Position = 3
End With
ActiveSheet.PivotTables("PivotTable3").PivotFields("SO#").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable3").PivotFields("PO#")
.Orientation = xlRowField
.Position = 4
End With
ActiveSheet.PivotTables("PivotTable3").PivotFields("PO#").Subtotals = Array( _
False, False, False, False, False, False, False, False, False, False, False, False)
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Item Number")
.Orientation = xlRowField
.Position = 5
End With
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("QTY Ordered"), "Sum of QTY Ordered", xlSum
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Qty Billed"), "Sum of Qty Billed", xlSum
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("QTY Rcvd"), "Sum of QTY Rcvd", xlSum
ActiveSheet.PivotTables("PivotTable3").AddDataField ActiveSheet.PivotTables( _
"PivotTable3").PivotFields("Qty Shipped"), "Sum of Qty Shipped", xlSum
ActiveSheet.PivotTables("PivotTable3").TableStyle2 = "PivotStyleMedium4"
ActiveSheet.PivotTables("PivotTable3").ShowTableStyleRowStripes = True
ActiveSheet.PivotTables("PivotTable3").ShowTableStyleColumnStripes = True
Columns("B:E").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A3").Select
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("PivotTable3"), _
"Project: Next Review Date").Slicers.Add ActiveSheet, , _
"Project: Next Review Date", "Project: Next Review Date", 193.2, 508.5, 144, _
159.95
ActiveSheet.Shapes.Range(Array("Project: Next Review Date")).Select
ActiveSheet.Shapes("Project: Next Review Date").IncrementLeft 105.75
ActiveSheet.Shapes("Project: Next Review Date").IncrementTop -167.25
ActiveWorkbook.SlicerCaches("Slicer_Project___Next_Review_Date").Slicers( _
"Project: Next Review Date").NumberOfColumns = 2
ActiveWorkbook.SlicerCaches("Slicer_Project___Next_Review_Date").Slicers( _
"Project: Next Review Date").NumberOfColumns = 3
ActiveWorkbook.SlicerCaches("Slicer_Project___Next_Review_Date").Slicers( _
"Project: Next Review Date").NumberOfColumns = 4
ActiveSheet.Shapes("Project: Next Review Date").ScaleWidth 2.9166666667, _
msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("Project: Next Review Date").ScaleWidth 0.9717756342, _
msoFalse, msoScaleFromTopLeft
ActiveSheet.Shapes("Project: Next Review Date").IncrementLeft 3
ActiveSheet.Shapes("Project: Next Review Date").IncrementTop -3.75
ActiveWorkbook.SlicerCaches("Slicer_Project___Next_Review_Date").Slicers( _
"Project: Next Review Date").DisableMoveResizeUI = True
Range("E4").Select
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("PivotTable3"), _
"PO Status").Slicers.Add ActiveSheet, , "PO Status", "PO Status", 193.2, 493.5 _
, 144.15, 159.95
ActiveSheet.Shapes.Range(Array("PO Status")).Select
ActiveSheet.Shapes("PO Status").IncrementLeft 101.25
ActiveSheet.Shapes("PO Status").IncrementTop -6.75
ActiveSheet.Shapes("PO Status").ScaleHeight 0.6965282083, msoFalse, _
msoScaleFromTopLeft
ActiveWorkbook.SlicerCaches("Slicer_PO_Status").Slicers("PO Status"). _
DisableMoveResizeUI = True
Range("E7").Select
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("PivotTable3"), _
"Setting Status").Slicers.Add ActiveSheet, , "Setting Status", "Setting Status" _
, 193.2, 493.5, 144.15, 159.95
ActiveSheet.Shapes.Range(Array("Setting Status")).Select
ActiveSheet.Shapes("Setting Status").IncrementLeft 246.75
ActiveSheet.Shapes("Setting Status").IncrementTop -7.5
ActiveSheet.Shapes("Setting Status").ScaleHeight 0.6960448458, msoFalse, _
msoScaleFromTopLeft
Range("E5").Select
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("PivotTable3"), _
"Project Type").Slicers.Add ActiveSheet, , "Project Type", "Project Type", _
193.2, 493.5, 144.15, 159.95
ActiveSheet.Shapes.Range(Array("Project Type")).Select
ActiveSheet.Shapes("Project Type").IncrementLeft 395.25
ActiveSheet.Shapes("Project Type").IncrementTop -7.5
ActiveSheet.Shapes("Project Type").ScaleHeight 0.6913734039, msoFalse, _
msoScaleFromTopLeft
ActiveWorkbook.SlicerCaches("Slicer_Project_Type").Slicers("Project Type"). _
DisableMoveResizeUI = True
Range("D5").Select
ActiveWorkbook.SlicerCaches.Add2(ActiveSheet.PivotTables("PivotTable3"), _
"Project Status").Slicers.Add ActiveSheet, , "Project Status", "Project Status" _
, 193.2, 493.5, 144.15, 159.95
ActiveSheet.Shapes.Range(Array("Project Status")).Select
ActiveSheet.Shapes("Project Status").IncrementLeft 101.25
ActiveSheet.Shapes("Project Status").IncrementTop 111
ActiveSheet.Shapes("Project Status").ScaleWidth 2.8281573499, msoFalse, _
msoScaleFromTopLeft
ActiveWorkbook.SlicerCaches("Slicer_Project_Status").Slicers("Project Status"). _
NumberOfColumns = 2
ActiveSheet.Shapes("Project Status").ScaleHeight 0.7147306135, msoFalse, _
msoScaleFromTopLeft
ActiveWorkbook.SlicerCaches("Slicer_Project_Status").Slicers("Project Status"). _
DisableMoveResizeUI = True
Range("A3").Select
End Sub
Sub PO_Group_SO_1_Format()
'
' PO_Group_SO_1_Format Macro
'
'
Range("A1:T779").Select
With Selection.Font
.Name = "Arial"
.Size = 12
.Strikethrough = False
.Superscript = False
.Subscript = False
.OutlineFont = False
.Shadow = False
.Underline = xlUnderlineStyleNone
.ColorIndex = xlAutomatic
.TintAndShade = 0
.ThemeFont = xlThemeFontNone
End With
Selection.Columns.AutoFit
Range("E2").Select
ActiveWindow.SmallScroll ToRight:=4
Columns("I:I").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("I1").Select
ActiveCell.FormulaR1C1 = "Proj#"
Range("I2").Select
ActiveCell.FormulaR1C1 = "=LEFT(RC[12], 11)"
Range("I2").Select
Selection.AutoFill Destination:=Range("I2:I779")
Range("I2:I779").Select
Columns("K:K").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("K1").Select
ActiveCell.FormulaR1C1 = "PO#"
Range("K2").Select
ActiveCell.FormulaR1C1 = "Righ"
Range("K2").Select
ActiveCell.FormulaR1C1 = "=RIGHT(RC[10], 11)"
Range("K2").Select
Selection.AutoFill Destination:=Range("K2:K779")
Range("K2:K779").Select
Range("A1:V779").Select
Range("K2").Activate
Selection.Columns.AutoFit
Range("P1").Select
Selection.Columns.AutoFit
ActiveWindow.SmallScroll ToRight:=-8
Range("B1").Select
Selection.Columns.AutoFit
Range("E1").Select
Selection.Columns.AutoFit
Range("F1").Select
Selection.Columns.AutoFit
Range("H1").Select
Selection.Columns.AutoFit
Range("D1").Select
Selection.Columns.AutoFit
Range("C2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Columns.AutoFit
Range("C1").Select
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlBottom
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
With Selection
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
.WrapText = True
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Columns("C:C").ColumnWidth = 21.71
Rows("1:1").Select
With Selection
.VerticalAlignment = xlCenter
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Selection.AutoFilter
Range("A1").Select
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Clear
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("C2:C779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("I2:I779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("J2:J779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort.SortFields. _
Add2 Key:=Range("K2:K779"), SortOn:=xlSortOnValues, Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("POListGroupedbySalesOrderResul").Sort
.SetRange Range("A1:V779")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Sub PO_Group_SO_2_CondFormat()
'
' PO_Group_SO_2_CondFormat Macro
'
'
Range("E2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=LEN(TRIM(E2))=0"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13421823
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:="On File", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"Requires Review", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlTextString, String:= _
"Waiting on customer", TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent4
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($E2=""On File"", $N2=$L2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("E2:E779"), Type:=xlFillFormats
Range("E2:E779").Select
Range("H2").Select
Selection.FormatConditions.Add Type:=xlTextString, String:="Fully Billed", _
TextOperator:=xlContains
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5296274
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("H2:H779"), Type:=xlFillFormats
Range("H2:H779").Select
Range("I2:K2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($O2<=$N2, $N2<>0, $O2<=$M2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Bold = True
.Italic = False
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("I2:K779"), Type:=xlFillFormats
Range("I2:K779").Select
Range("L2:O2").Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($M2=$N2, $N2=$O2, $M2<>0, $N2<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent6
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND($O2<$N2, $N3<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.399945066682943
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=OR($M2>$N2, $M2<>0)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 8420607
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.AutoFill Destination:=Range("L2:O779"), Type:=xlFillFormats
Range("L2:O779").Select
Range("F2").Select
End Sub
VBA Code:
'example of code used in another macro to select dynamicly
Dim sht As Worksheet
Dim LastRow As Long
Dim LastColumn As Long
Dim StartCell As Range
Set sht = Worksheets("BCIHollandCTPickShipResults")
Set StartCell = Range("A1")
'Find Last Row and Column
LastRow = sht.Cells(sht.Rows.Count, StartCell.Column).End(xlUp).Row
LastColumn = sht.Cells(StartCell.Row, sht.Columns.Count).End(xlToLeft).Column
'Select Range
sht.Range(StartCell, sht.Cells(LastRow, LastColumn)).Select
End Sub