Hi All,
Great forum with plenty of help and thank you all for being active as your input is quite often valuable and useful. It came a time when I thought to get your view on my work and some performance problem I deal with i.e. time of macro being run.
I developed a sheet that shows top level values and when pointing on '+' sign it drills into details of that value (while in reality it does execute another macro to provide details - based on advanced filtering) all runs ok no issues but due to data size it is a little slower than I would expect i.e. on average 14 seconds per drill down. I have tried to streamline code where possible and also switched off additional functions when macro run but it resulted in improvement from 17 seconds to 13 seconds only. Would that be max I can get from it. Or is there anything else I could improve to get to a better execution time. To note there are just over 20k rows to deal with at any time from the data I filter.
To support it see first code for calling '+' action and then below second for getting into drilling mode
Option Explicit
Dim PageBreakState As Boolean
Sub Worksheet_SelectionChange(ByVal Target As Range)
'Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("C3:C100")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
If Target.Value = "+" And Range("P" & Target.Row).Value = "C" Then 'Show Invoices
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
PageBreakState = ActiveSheet.DisplayPageBreaks
ActiveSheet.DisplayPageBreaks = False
'Application.DisplayStatusBar = False
Show_Details_Top_Level
End
End If
If Target.Value = "-" And Range("P" & Target.Row).Value = "C" Then 'Hide Invoices
Hide_Details_Top_Level
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
'Application.DisplayStatusBar = True
'PageBreakState = ActiveSheet.DisplayPageBreaks
'ActiveSheet.DisplayPageBreaks = True
End
End If
If Target.Value = "+" And Range("P" & Target.Row).Value = "N" Then 'Show Transactions
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Show_Transactions_Top_Level
End
End If
If Target.Value = "-" And Range("P" & Target.Row).Value = "N" Then 'Hide Transactions
Hide_Transactions_Top_Level
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
End
End If
End If
End Sub
Display More
Second part to execute ShowTopLevel macro
Option Explicit
Dim Directorate As String, MA_Des As String
Dim DirectorateQty As Long, LastDirectorateRow As Long, LastFiltRow As Long, ActRow As Long
Sub Show_Details_Top_Level()
With Sheet1
ActRow = ActiveCell.Row
Directorate = .Range("A" & ActRow).Value 'Get Directorate #
MA_Des = "*" & .Range("B" & ActRow).Value & "*" 'Get Details #
Sheet1.Range("F7:G7").Copy Destination:=Sheet3.Range("HC4:HD4") 'Paste period for data
Sheet3.Range("GG1:GL1").Value = Sheet3.Range("HE4:HJ4").Value 'Paste Top Level Calcs#
Sheet3.Range("GM1").Value = Sheet3.Range("HL4").Value 'Paste Top Level Calcs#
LastDirectorateRow = Sheet3.Range("B20999").End(xlUp).Row 'Get last Row of Directorate
Sheet3.Range("HA2:OV2,HA5:HL999").ClearContents 'Clear any previous data
Sheet3.Range("HB2").Value = Directorate
Sheet3.Range("II2").Value = MA_Des
Sheet3.Range("ON2").Value = "Yes"
Sheet3.Range("A1:GV" & LastDirectorateRow).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheet3.Range("HA1:OV2"), CopyToRange:=Sheet3.Range("HA4:HL4"), Unique:=True
LastFiltRow = Sheet3.Range("HA999").End(xlUp).Row 'Last FilterRow
If LastFiltRow < 5 Then GoTo NoDirectorate
DirectorateQty = LastFiltRow - 4 'Get The Number Of Directorate Lines
ActiveCell.Value = "-"
ActiveCell.EntireRow.Offset(1).Resize(DirectorateQty + 1).Insert Shift:=xlDown 'Inserts # of Ros + 1 for Header
With Range("D" & ActRow + 1 & ":P" & ActRow + 1)
.Value = .Range("AA2:AM2").Value 'Add Nominal Header
.HorizontalAlignment = xlCenter 'Justify Center Nominal Header
End With
End If
With Range("F" & ActRow + 1 & ":O" & ActRow + 1)
.HorizontalAlignment = xlCenter 'Justify Centre Nominal Header
.VerticalAlignment = xlBottom 'Justify Center Nominals Header
.WrapText = True ' Cells wrap
End With
With Range("C" & ActRow + 2 & ":C" & ActRow + 1 + DirectorateQty)
.Value = "+" 'Mark Line as Expand For Payment
.NumberFormat = "General" 'Format Invoice Numbers
End With
.Range("D" & ActRow + 2 & ":P" & ActRow + 1 + DirectorateQty).Value = Sheet3.Range("HA5:HL" & LastFiltRow).Value 'Add Details
.Range("D" & ActRow + 1 & ":E" & ActRow + 1 + DirectorateQty).Columns.AutoFit 'Column autofit
.Range("O" & ActRow + 2 & ":O" & ActRow + 1 + DirectorateQty).NumberFormat = "_(* #,##0_);_(* (#,##0);_(* ""-""??_);_(@_)" ' Format Amounts
.Range("L" & ActRow + 2 & ":L" & ActRow + 1 + DirectorateQty).Style = "Percent" 'Format Amounts
With Range("P" & ActRow + 2 & ":P" & ActRow + 1 + DirectorateQty)
.Value = "N" 'Mark Line as Nominal
.HorizontalAlignment = xlCenter ' Category Column Justify Centre
.NumberFormat = "General" 'Format Category Column
End With
End With
Exit Sub
NoDirectorate:
MsgBox "There are no Details for this line"
End Sub
Sub Hide_Details_Top_Level()
With Sheet1
ActRow = ActiveCell.Row
LastDirectorateRow = ActiveCell.Offset(1, -1).End(xlDown).Row - 1
ActiveCell.Value = "+"
.Range(ActRow + 1 & ":" & LastDirectorateRow).EntireRow.Delete
.Range("F" & ActRow + 1 & ":O" & ActRow + 9999).ColumnWidth = 11.22 'Column autofit
Columns("D:E").Hidden = True 'Hide columns with Nominals and Description
End With
End Sub
Display More
Please advise what I can do extra to make it better.
Really appreciate it.