SomethngWicked
Board Regular
- Joined
- Feb 18, 2015
- Messages
- 79
Hey guys, I have a VBA script that runs through a set of data and does the following:
Filters through the data and extracts a certain value. In this case, anything marked as 'Pen Distributions' and then it creates a table.
Next, adjusts the row height and converts two columns to a data formate
After that, it applies a CF statement whereby if the data value is less than three days old, highlight green, three to five days, yellow, and greater than seven days, red
finally it applies some additional formatting
The problem is I have this setup via 4 subprocedures. What is the best way to tighten/clean up this code so I only have one sub? Any help would be greatly appreciated.
Code is below:
Filters through the data and extracts a certain value. In this case, anything marked as 'Pen Distributions' and then it creates a table.
Next, adjusts the row height and converts two columns to a data formate
After that, it applies a CF statement whereby if the data value is less than three days old, highlight green, three to five days, yellow, and greater than seven days, red
finally it applies some additional formatting
The problem is I have this setup via 4 subprocedures. What is the best way to tighten/clean up this code so I only have one sub? Any help would be greatly appreciated.
Code is below:
VBA Code:
Sub FilterPen()
If Not Evaluate("isref(Pen Distributions!A1)") Then
With ActiveSheet
.Range("A1:L1").AutoFilter 6, "Pen Distributions"
Worksheets.Add.Name = "Pen Distributions"
.AutoFilter.Range.Copy Sheets("Pen Distributions").Range("a1")
.AutoFilterMode = False
End With
With Sheets("Pen Distributions")
.ListObjects.Add(xlSrcRange, .Range("A1").CurrentRegion, , xlYes).Name = "Pen"
End With
With Range("A1:L1").EntireColumn.AutoFit
End With
End If
End Sub
Sub AdjustRowHeight()
With Sheets("Pen Distributions")
Dim finalRow As Integer
Dim i As Integer
finalRow = Cells(Rows.Count, 1).End(xlUp).Row
Range("A1:A" & finalRow).EntireRow.AutoFit
End With
With Sheets("Pen Distributions")
Columns("H:I").Select
Selection.NumberFormat = "mm/dd/yy;@"
Range("Pen[[#Headers],[Number]]").Select
End With
End Sub
Sub MacroPenCF()
'
' Macro1 Macro
'
'
Range(Range("I1"), Range("I1").End(xlDown)).Select
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=NOW()-I1<3"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 5287936
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:= _
"=AND(NOW()-I1>3,NOW()-I1<7)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 65535
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Selection.FormatConditions.Add Type:=xlExpression, Formula1:="=NOW()-I1>7"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 255
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
Sub PenSortCF()
'
' PenSortCF Macro
'
'
ActiveWorkbook.Worksheets("Pen Distributions").ListObjects("Pen"). _
Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Pen Distributions").ListObjects("Pen"). _
Sort.SortFields.Add Key:=Range("Pen[[#All],[Updated]]"), SortOn:= _
xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Pen Distributions").ListObjects( _
"Pen").Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("Pen[[#Headers],[Number]:[Task type]]").Select
Range(Selection, Selection.End(xlToRight)).Select
With Selection.Font
.ThemeColor = xlThemeColorDark1
.TintAndShade = 0
End With
Range("Pen[[#Headers],[Number]]").Select
End Sub