Hi, I developed this tool analyze my workbook to identify which formula is too slow. This code works and I tested many times. But I don't have much low level knowledge of Excel in general. So can someone look at the code and tell me what's the limitation(s) of this code?
Code:
Private Declare PtrSafe Function getFrequency Lib "kernel32" _
Alias "QueryPerformanceFrequency" ( _
ByRef Frequency As Currency) _
As Long
Private Declare PtrSafe Function getTime Lib "kernel32" _
Alias "QueryPerformanceCounter" ( _
ByRef Counter As Currency) _
As Long
Sub WBCalculateTime()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
On Error Resume Next
Dim beginTime As Currency
Dim endTime As Currency
Dim perSecond As Currency
Dim totalTime As Currency
Dim wbSummary As Workbook
Dim wbSource As Workbook
Set wbSource = ActiveWorkbook
Dim path As String
path = Application.ActiveWorkbook.path
Dim fileName As String
fileName = Left(wbSource.Name, Len(wbSource.Name) - 5)
Set wbSummary = Workbooks.Add
wbSummary.Sheets(1).Name = "Summary_"
Dim sumSh As Worksheet
getFrequency perSecond
For Each wSheet In wbSource.Worksheets
Set sumSh = wbSummary.Sheets.Add(After:=wbSummary.Worksheets(wbSummary.Worksheets.Count))
sumSh.Name = "_" + wSheet.Name
For Each cCell In wSheet.UsedRange
If IsEmpty(cCell.Value) = False Then
getTime beginTime
cCell.Calculate
getTime endTime
endTime = 1000# * (endTime - beginTime) / perSecond
totalTime = totalTime + endTime
sumSh.Cells(cCell.Row, (cCell.Column - 1) * 3 + 1).Value = cCell.Address
sumSh.Cells(cCell.Row, (cCell.Column - 1) * 3 + 3).Value = "'" + Left(cCell.Formula, 254)
sumSh.Cells(cCell.Row, (cCell.Column - 1) * 3 + 2).NumberFormat = "#,##0.00"
sumSh.Cells(cCell.Row, (cCell.Column - 1) * 3 + 2).Value = endTime
End If
Next cCell
Next wSheet
Set sumSh = wbSummary.Sheets("Summary_")
sumSh.Range("A1").Value = "Total run time: " & Format(totalTime / 1000, "#,##0")
Application.AlertBeforeOverwriting = False
wbSummary.SaveAs fileName:=path & "\" & fileName & "_Timed" & ".xlsx", AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges
Application.ScreenUpdating = True
End Sub
Last edited: