Hello,
I am attempting to take a crack at a former employees VBA code that seems to be a "recorded" macro and would like to finesse it so it has more standardization and more definitive variables. Such as, I would like to take out all the Column Letters and only use Column Headers, I would like to use Spreadsheet Names and not Spreadsheet 15, 16 etc., only because if things were to get added (columns, sheets, etc) that would effect the code to run successfully.
My question for the code I am breaking down below is - There are multiple sheets that have FY22 Transactions, FY23 Transactions, FY24 Transactions etc. (the FY can vary between FY, CY and/or OY and the 22, 23, 24 will always change going forward due to the year) however these sheets are the only ones that have "Transactions" in the worksheet name so is there a code where I can have it wildcard *Transactions* and then do all of the formatting (obviously I will need to fix the column letters into headers and such? You can see the code repeats itself because there are 3 Transactions Sheets with different years so I wasn't sure if this would be a one stop shop to be able to wildcard it and have it due it on all 3 of those wildcard transaction sheets?
I am attempting to take a crack at a former employees VBA code that seems to be a "recorded" macro and would like to finesse it so it has more standardization and more definitive variables. Such as, I would like to take out all the Column Letters and only use Column Headers, I would like to use Spreadsheet Names and not Spreadsheet 15, 16 etc., only because if things were to get added (columns, sheets, etc) that would effect the code to run successfully.
My question for the code I am breaking down below is - There are multiple sheets that have FY22 Transactions, FY23 Transactions, FY24 Transactions etc. (the FY can vary between FY, CY and/or OY and the 22, 23, 24 will always change going forward due to the year) however these sheets are the only ones that have "Transactions" in the worksheet name so is there a code where I can have it wildcard *Transactions* and then do all of the formatting (obviously I will need to fix the column letters into headers and such? You can see the code repeats itself because there are 3 Transactions Sheets with different years so I wasn't sure if this would be a one stop shop to be able to wildcard it and have it due it on all 3 of those wildcard transaction sheets?
VBA Code:
Sub CSFormat()
'
' CSFormat Macro
'
'Speeds up macro'
Application.ScreenUpdating = False
'Select the correct worksheet and table then remove filters'
Worksheets(15).Activate
Set ListObject = Worksheets(15).ListObjects(1)
ListObject.AutoFilter.ShowAllData
'Adjust column widths'
Columns("E:E").ColumnWidth = 79
Columns("F:F").ColumnWidth = 36.82
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 58.27
Columns("J:J").ColumnWidth = 60.36
Columns("K:K").ColumnWidth = 107.91
'Sort for Q Serial, Transaction Code, Absolute Value, Description'
ActiveWorkbook.Worksheets(15).ListObjects(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(15).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(15).ListObjects(1).Name & "[QuarterSerial]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(15).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(15).ListObjects(1).Name & "[Transaction Code]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(15).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(15).ListObjects(1).Name & "[Absolute Value]"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(15).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(15).ListObjects(1).Name & "[Description]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(15).ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Create a column to use the IF function, creating TMX Coverage to be Retired no coverage and All Parts for Missing then delete the unused column'
Columns("L:L").Select
Selection.Insert Shift:=xlToRight
Range(Worksheets(15).ListObjects(1).Name & "[[#Headers],[Column1]]").Select
ActiveCell.Select
ActiveCell.FormulaR1C1 = "TriMedx Coverage2"
ActiveCell.Offset(1, 0).Range(Worksheets(15).ListObjects(1).Name & "[[#Headers],[EquipmentID]]"). _
Select
ActiveCell.Formula2R1C1 = _
"=IFS([@[Transaction Type]]=""Retirement"",""Retired - No Coverage"",[@[TriMedx Coverage]]=""Missing Coverage"",""All Parts & Labor"",[@[TriMedx Coverage]]<>""Missing Coverage"",[@[TriMedx Coverage]])"
Range("L2").Select
Selection.AutoFill Destination:=Range(Worksheets(15).ListObjects(1).Name & "[TriMedx Coverage2]")
Range(Worksheets(15).ListObjects(1).Name & "[TriMedx Coverage2]").Select
Selection.Copy
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("L:L").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'Hide Columns Retired Date, CEID, Proration Date, and Serial'
Range("Q:Q,P:P").Select
Range(Worksheets(15).ListObjects(1).Name & "[[#Headers],[Retired Date]]").Activate
Range("Q:Q,P:P,H:H,G:G").Select
Range(Worksheets(15).ListObjects(1).Name & "[[#Headers],[CEID]]").Activate
Selection.EntireColumn.Hidden = True
'Filter and remove all 0 or empty cells from TMX Coverage by filtering the Equipment ID to blanks, then deleting TMX Coverage Values'
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1, _
Criteria1:="="
Range("L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("L2").Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
'Adjust Column Width'
Columns("L:L").ColumnWidth = 40
'Select the correct worksheet and table then remove filters'
Worksheets(16).Activate
Set ListObject = Worksheets(16).ListObjects(1)
ListObject.AutoFilter.ShowAllData
'Adjust column widths'
Columns("E:E").ColumnWidth = 79
Columns("F:F").ColumnWidth = 36.82
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 58.27
Columns("J:J").ColumnWidth = 60.36
Columns("K:K").ColumnWidth = 107.91
'Sort for Q Serial, Transaction Code, Absolute Value, Description'
ActiveWorkbook.Worksheets(16).ListObjects(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(16).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(16).ListObjects(1).Name & "[QuarterSerial]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(16).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(16).ListObjects(1).Name & "[Transaction Code]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(16).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(16).ListObjects(1).Name & "[Absolute Value]"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(16).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(16).ListObjects(1).Name & "[Description]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(16).ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Create a column to use the IF function, creating TMX Coverage to be Retired no coverage and All Parts for Missing then delete the unused column'
Columns("L:L").Select
Selection.Insert Shift:=xlToRight
Range(Worksheets(16).ListObjects(1).Name & "[[#Headers],[Column1]]").Select
ActiveCell.Select
ActiveCell.FormulaR1C1 = "TriMedx Coverage2"
ActiveCell.Offset(1, 0).Range(Worksheets(16).ListObjects(1).Name & "[[#Headers],[EquipmentID]]"). _
Select
ActiveCell.Formula2R1C1 = _
"=IFS([@[Transaction Type]]=""Retirement"",""Retired - No Coverage"",[@[TriMedx Coverage]]=""Missing Coverage"",""All Parts & Labor"",[@[TriMedx Coverage]]<>""Missing Coverage"",[@[TriMedx Coverage]])"
Range("L2").Select
Selection.AutoFill Destination:=Range(Worksheets(16).ListObjects(1).Name & "[TriMedx Coverage2]")
Range(Worksheets(16).ListObjects(1).Name & "[TriMedx Coverage2]").Select
Selection.Copy
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("L:L").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'Hide Columns Retired Date, CEID, Proration Date, and Serial'
Range("Q:Q,P:P").Select
Range(Worksheets(16).ListObjects(1).Name & "[[#Headers],[Retired Date]]").Activate
Range("Q:Q,P:P,H:H,G:G").Select
Range(Worksheets(16).ListObjects(1).Name & "[[#Headers],[CEID]]").Activate
Selection.EntireColumn.Hidden = True
'Filter and remove all 0 or empty cells from TMX Coverage by filtering the Equipment ID to blanks, then deleting TMX Coverage Values'
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1, _
Criteria1:="="
Range("L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("L2").Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
'Adjust Column Width'
Columns("L:L").ColumnWidth = 40
'Select the correct worksheet and table then remove filters'
Worksheets(17).Activate
Set ListObject = Worksheets(17).ListObjects(1)
ListObject.AutoFilter.ShowAllData
'Adjust column widths'
Columns("E:E").ColumnWidth = 79
Columns("F:F").ColumnWidth = 40
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 58.27
Columns("J:J").ColumnWidth = 60.36
Columns("K:K").ColumnWidth = 107.91
'Sort for Q Serial, Transaction Code, Absolute Value, Description'
ActiveWorkbook.Worksheets(17).ListObjects(1).Sort.SortFields.Clear
ActiveWorkbook.Worksheets(17).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(17).ListObjects(1).Name & "[QuarterSerial]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(17).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(17).ListObjects(1).Name & "[Transaction Code]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(17).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(17).ListObjects(1).Name & "[Absolute Value]"), SortOn:=xlSortOnValues, Order:= _
xlDescending, DataOption:=xlSortNormal
ActiveWorkbook.Worksheets(17).ListObjects(1).Sort.SortFields.Add2 Key:=Range(Worksheets(17).ListObjects(1).Name & "[Description]"), SortOn:=xlSortOnValues, Order:= _
xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets(17).ListObjects(1).Sort
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Create a column to use the IF function, creating TMX Coverage to be Retired no coverage and All Parts for Missing then delete the unused column'
Columns("L:L").Select
Selection.Insert Shift:=xlToRight
Range(Worksheets(17).ListObjects(1).Name & "[[#Headers],[Column1]]").Select
ActiveCell.Select
ActiveCell.FormulaR1C1 = "TriMedx Coverage2"
ActiveCell.Offset(1, 0).Range(Worksheets(17).ListObjects(1).Name & "[[#Headers],[EquipmentID]]"). _
Select
ActiveCell.Formula2R1C1 = _
"=IFS([@[Transaction Type]]=""Retirement"",""Retired - No Coverage"",[@[TriMedx Coverage]]=""Missing Coverage"",""All Parts & Labor"",[@[TriMedx Coverage]]<>""Missing Coverage"",[@[TriMedx Coverage]])"
Range("L2").Select
Selection.AutoFill Destination:=Range(Worksheets(17).ListObjects(1).Name & "[TriMedx Coverage2]")
Range(Worksheets(17).ListObjects(1).Name & "[TriMedx Coverage2]").Select
Selection.Copy
Range("M2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("L:L").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'Hide Columns Retired Date, CEID, Proration Date, and Serial'
Range("Q:Q,P:P").Select
Range(Worksheets(17).ListObjects(1).Name & "[[#Headers],[Retired Date]]").Activate
Range("Q:Q,P:P,H:H,G:G").Select
Range(Worksheets(17).ListObjects(1).Name & "[[#Headers],[CEID]]").Activate
Selection.EntireColumn.Hidden = True
'Filter and remove all 0 or empty cells from TMX Coverage by filtering the Equipment ID to blanks, then deleting TMX Coverage Values'
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1, _
Criteria1:="="
Range("L2").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.ClearContents
Range("L2").Select
Selection.End(xlToLeft).Select
Selection.End(xlToLeft).Select
ActiveSheet.ListObjects(1).Range.AutoFilter Field:=1
'Adjust Column Width'
Columns("L:L").ColumnWidth = 40
'Remove code that speeds up macro'
Application.ScreenUpdating = True
End Sub