How can I measure time more precisely in VBA

0 votes
I have several VBA scripts in an Excel sheet. It's difficult to measure times precisely with "Time." Exist other choices?
Jan 13, 2023 in Others by Kithuzzz
• 38,000 points
406 views

1 answer to this question.

0 votes

At the top of the script you can add this:

Private Declare PtrSafe Function get_frequency Lib "kernel32" _
      Alias "QueryPerformanceFrequency" ( _
      ByRef Frequency As Currency) _
      As Long
 
Private Declare PtrSafe Function get_time Lib "kernel32" _
      Alias "QueryPerformanceCounter" ( _
      ByRef Counter As Currency) _
      As Long

Then in your code, you can get high-precision time measurements like this...

' run this once
Dim per_second As Currency: get_frequency per_second

' ...

' get start time
Dim t0 As Currency: get_time t0

' HERE perform the operations you want to time 
' ...

' get end time
Dim t1 As Currency: get_time t1

' calculate high precision duration
Dim elapsed_millis As Double: elapsed_millis = (t1 - t0) / per_second * 1000#
answered Jan 13, 2023 by narikkadan
• 63,600 points

Related Questions In Others

0 votes
1 answer

How can I replace the nth occurence using regex in VBA?

Use: =RegexReplace("This 9 is 8 a 77 6 ...READ MORE

answered Jan 13, 2023 in Others by narikkadan
• 63,600 points
1,208 views
0 votes
0 answers

how can I deploy a test in pytest framework to airflow using DAG?

I have established an automative testing framework ...READ MORE

Oct 4, 2021 in Others by Yuan
• 120 points
676 views
0 votes
1 answer

How do I get the current date and time in PHP?

The time would go by your server ...READ MORE

answered Feb 16, 2022 in Others by Aditya
• 7,680 points
726 views
0 votes
1 answer

Retrieve epay.info Balance with VBA and Excel

This code should log you in, provided ...READ MORE

answered Sep 5, 2018 in Blockchain by digger
• 26,740 points
1,258 views
0 votes
1 answer

How to load file to Excel Power query from SFTP site

Currently, I don't think there is a ...READ MORE

answered Dec 3, 2018 in Power BI by Upasana
• 8,620 points
3,695 views
0 votes
1 answer

Using VBA Excel to create a gramatically correct list

The Excel AND function is a logical ...READ MORE

answered Feb 9, 2022 in Others by gaurav
• 23,260 points
966 views
0 votes
2 answers

How to copy a formula horizontally within a table using Excel VBA?

Hi so basically, create an adjacent column ...READ MORE

answered Feb 16, 2022 in Others by Edureka
• 13,690 points
1,086 views
0 votes
1 answer

How can I find and replace text in Word using Excel VBA?

Try this code Option Explicit Const wdReplaceAll = 2 Sub ...READ MORE

answered Oct 15, 2022 in Others by narikkadan
• 63,600 points
5,012 views
0 votes
1 answer

How can I perform a reverse string search in Excel without using VBA?

This one is tested and does work ...READ MORE

answered Jan 5, 2023 in Others by narikkadan
• 63,600 points
1,365 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP