Hello,
I have a pretty lengthy code that takes about 4-5 minutes to run and was wondering if there is any improvements that could be made to it to help cut the run time down some?
I have a pretty lengthy code that takes about 4-5 minutes to run and was wondering if there is any improvements that could be made to it to help cut the run time down some?
VBA Code:
Sub AscCSFormat()
Application.Calculation = xlManual
Application.ScreenUpdating = False
Application.EnableEvents = False
'Select the correct worksheet and table then remove filters'
Worksheets(15).Activate
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
With Cells
.EntireColumn.Hidden = False
.EntireRow.Hidden = False
End With
'Adjust column widths'
Columns("E:E").ColumnWidth = 80
Columns("F:F").ColumnWidth = 37
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 60
Columns("J:J").ColumnWidth = 60
Columns("K:K").ColumnWidth = 108
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
Dim oLo As ListObject, l As Long, note As String
Set oLo = Worksheets(15).ListObjects(1)
With oLo
For l = 1 To .ListRows.Count
note = "Retired - No Coverage"
If .ListColumns("Transaction Type").DataBodyRange(l, 1).Value = "Retirement" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
note = "All Parts & Labor"
If .ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = "Missing Coverage" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
Next l
End With
'Change Header to Proration Date'
Rows("1:1").Replace What:="*Proration Date*", Replacement:="Proration Date", LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
Dim LastColumn As Long
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LastColumn
If UCase(Cells(1, i)) = "CEID" Or UCase(Cells(1, i)) = "SERIAL" Or UCase(Cells(1, i)) = "RETIRED DATE" Or UCase(Cells(1, i)) = "PRORATION DATE" Then Columns(i).Hidden = True
Next
ActiveSheet.ResetAllPageBreaks
Dim hdr As Range, f As Range, r As Range
Dim cell As String
Set hdr = Rows(1).Find("Transaction Date", , xlValues, xlWhole, , , False)
If Not hdr Is Nothing Then
Set r = Columns(hdr.Column)
Set f = r.Find("*Total*", , xlValues, xlPart, , , False)
If Not f Is Nothing Then
cell = f.Address
Do
f.Offset(1).PageBreak = xlPageBreakManual
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If
End If
Dim ws As Worksheet
Set ws15 = Worksheets(15)
Set oLo = ws15.ListObjects(1)
With oLo
For i = 1 To .ListRows.Count
If .ListColumns("Annual Service Price").DataBodyRange.Cells(i, 1).Value = 0 Then
If InStr(.ListColumns("Transaction Date").DataBodyRange.Cells(i, 1).Value, "Total") = 0 Then
ws15.Rows(oLo.HeaderRowRange.Row + i).Hidden = True
End If
End If
Next i
End With
'Select the correct worksheet and table then remove filters'
Worksheets(16).Activate
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
With Cells
.EntireColumn.Hidden = False
.EntireRow.Hidden = False
End With
'Adjust column widths'
Columns("E:E").ColumnWidth = 80
Columns("F:F").ColumnWidth = 37
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 60
Columns("J:J").ColumnWidth = 60
Columns("K:K").ColumnWidth = 108
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
Set oLo = Worksheets(16).ListObjects(1)
With oLo
For l = 1 To .ListRows.Count
note = "Retired - No Coverage"
If .ListColumns("Transaction Type").DataBodyRange(l, 1).Value = "Retirement" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
note = "All Parts & Labor"
If .ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = "Missing Coverage" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
Next l
End With
'Change Header to Proration Date'
Rows("1:1").Replace What:="*Proration Date*", Replacement:="Proration Date", LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LastColumn
If UCase(Cells(1, i)) = "CEID" Or UCase(Cells(1, i)) = "SERIAL" Or UCase(Cells(1, i)) = "RETIRED DATE" Or UCase(Cells(1, i)) = "PRORATION DATE" Then Columns(i).Hidden = True
Next
ActiveSheet.ResetAllPageBreaks
Set hdr = Rows(1).Find("Transaction Date", , xlValues, xlWhole, , , False)
If Not hdr Is Nothing Then
Set r = Columns(hdr.Column)
Set f = r.Find("*Total*", , xlValues, xlPart, , , False)
If Not f Is Nothing Then
cell = f.Address
Do
f.Offset(1).PageBreak = xlPageBreakManual
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If
End If
Dim ws16 As Worksheet
Set ws16 = Worksheets(16)
Set oLo = ws16.ListObjects(1)
With oLo
For i = 1 To .ListRows.Count
If .ListColumns("Annual Service Price").DataBodyRange.Cells(i, 1).Value = 0 Then
If InStr(.ListColumns("Transaction Date").DataBodyRange.Cells(i, 1).Value, "Total") = 0 Then
ws16.Rows(oLo.HeaderRowRange.Row + i).Hidden = True
End If
End If
Next i
End With
'Select the correct worksheet and table then remove filters'
Worksheets(17).Activate
On Error Resume Next
ActiveSheet.ShowAllData
On Error GoTo 0
With Cells
.EntireColumn.Hidden = False
.EntireRow.Hidden = False
End With
'Adjust column widths'
Columns("E:E").ColumnWidth = 80
Columns("F:F").ColumnWidth = 37
Columns("G:G").EntireColumn.AutoFit
Columns("H:H").EntireColumn.AutoFit
Columns("I:I").ColumnWidth = 60
Columns("J:J").ColumnWidth = 60
Columns("K:K").ColumnWidth = 108
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
Set oLo = Worksheets(17).ListObjects(1)
With oLo
For l = 1 To .ListRows.Count
note = "Retired - No Coverage"
If .ListColumns("Transaction Type").DataBodyRange(l, 1).Value = "Retirement" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
note = "All Parts & Labor"
If .ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = "Missing Coverage" Then
.ListColumns("TriMedx Coverage").DataBodyRange(l, 1).Value = note
End If
Next l
End With
'Change Header to Proration Date'
Rows("1:1").Replace What:="*Proration Date*", Replacement:="Proration Date", LookAt _
:=xlPart, SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To LastColumn
If UCase(Cells(1, i)) = "CEID" Or UCase(Cells(1, i)) = "SERIAL" Or UCase(Cells(1, i)) = "RETIRED DATE" Or UCase(Cells(1, i)) = "PRORATION DATE" Then Columns(i).Hidden = True
Next
ActiveSheet.ResetAllPageBreaks
Set hdr = Rows(1).Find("Transaction Date", , xlValues, xlWhole, , , False)
If Not hdr Is Nothing Then
Set r = Columns(hdr.Column)
Set f = r.Find("*Total*", , xlValues, xlPart, , , False)
If Not f Is Nothing Then
cell = f.Address
Do
f.Offset(1).PageBreak = xlPageBreakManual
Set f = r.FindNext(f)
Loop While Not f Is Nothing And f.Address <> cell
End If
End If
Dim ws17 As Worksheet
Set ws17 = Worksheets(17)
Set oLo = ws17.ListObjects(1)
With oLo
For i = 1 To .ListRows.Count
If .ListColumns("Annual Service Price").DataBodyRange.Cells(i, 1).Value = 0 Then
If InStr(.ListColumns("Transaction Date").DataBodyRange.Cells(i, 1).Value, "Total") = 0 Then
ws17.Rows(oLo.HeaderRowRange.Row + i).Hidden = True
End If
End If
Next i
End With
Sheets("Cover Page").Select
Range("O1").Select
Application.Calculation = xlAutomatic
Application.ScreenUpdating = False
Application.EnableEvents = True
End Sub