Mousehunter
New Member
- Joined
- May 6, 2008
- Messages
- 25
- Office Version
- 2013
- 2010
- Platform
- Windows
Hello!
Yesterday I posted a question received a solution from @John_w who suggested I used a Windows API call (sleep) instead of the VBA Wait function I was trying to use. I did use it and it worked fine.
In a big fat nutshell, I am trying to alter the contents of a cell (adding 1 for instance) based on an input that is Beats per Minute. So the application must sleep for so many seconds per minute each time so that I have the desired outcome.
The problem is that each time I am trying to modify a cell, a ton of code runs backstage and this slows down everything. My hands are tied because I cannot deactivate the AddIn due to corporate policies. Having that situation in mind, I modified the code so that it keeps track of the Beats per Minute every minute. How many for the 1st minute, how many for the second etc. The more the beats I want the greater the discrepancy between the intended and the actual beats I receive. For instance If I want 255 BPM I receive 233, if I want 84, I receive 79, If I want 60, I receive 58 etc.
Below is the code I use
And here is a printscreen from the file
Can you please test the code and tell me about the discrepancies in your PC? Am I doing something wrong or is it the xlam things slowing it down?
Please email me at [email address removed] to receive the file if you want to give it a try using my file.
Thanks,
Adam
Yesterday I posted a question received a solution from @John_w who suggested I used a Windows API call (sleep) instead of the VBA Wait function I was trying to use. I did use it and it worked fine.
In a big fat nutshell, I am trying to alter the contents of a cell (adding 1 for instance) based on an input that is Beats per Minute. So the application must sleep for so many seconds per minute each time so that I have the desired outcome.
The problem is that each time I am trying to modify a cell, a ton of code runs backstage and this slows down everything. My hands are tied because I cannot deactivate the AddIn due to corporate policies. Having that situation in mind, I modified the code so that it keeps track of the Beats per Minute every minute. How many for the 1st minute, how many for the second etc. The more the beats I want the greater the discrepancy between the intended and the actual beats I receive. For instance If I want 255 BPM I receive 233, if I want 84, I receive 79, If I want 60, I receive 58 etc.
Below is the code I use
VBA Code:
Option Explicit
#If VBA7 Then
Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
#Else
Public Declare Sub Sleep Lib "kernel32" (ByVal milliseconds As Long)
#End If
Dim MinuteCounter As Integer
Sub StartClock()
Dim BPM As Byte, Rythm, TimeLapse As Double, TimeLapseInSeconds
Dim NewCountofBeats As Integer, OldCountofBeats As Integer, LastUsedStatsRow As Integer
If IsEmpty([j4]) Then Exit Sub
Let BPM = [j4]
Let [d4] = 0
Let MinuteCounter = 1
Let OldCountofBeats = 0
Let NewCountofBeats = 0
[a8:d8].Resize([d1].Value).ClearContents
[b2:b3].ClearContents
Let [d1] = 0
Let [b1].Value = "Start"
Let Rythm = 60 / BPM
If [b2].Value = "" Then [b2].Value = Now
Label:
VBA.DoEvents
Let [b3].Value = Now
Let [d4] = [d4] + 1
Sleep CDbl(Rythm) * 1000
If [b1].Value = "Stop" Then
Let [d1] = MinuteCounter
Exit Sub
End If
Let TimeLapse = [b4].Value
Let TimeLapseInSeconds = TimeLapse * 86400
If TimeLapseInSeconds \ 60 >= MinuteCounter Then
NewCountofBeats = [d4].Value
Let [a7].Offset(MinuteCounter) = BPM
Let [b7].Offset(MinuteCounter) = MinuteCounter
Let [c7].Offset(MinuteCounter) = NewCountofBeats - OldCountofBeats
Let [d7].Offset(MinuteCounter) = 1 - ([c7].Offset(MinuteCounter).Value / [a7].Offset(MinuteCounter).Value)
Let OldCountofBeats = NewCountofBeats
Let MinuteCounter = MinuteCounter + 1
End If
GoTo Label
End Sub
Sub StopClock()
Let [b1].Value = "Stop"
Let [d1] = MinuteCounter
End Sub
Sub Reset_Timer()
Dim LastUsedStatsRow As Integer
Let [b1].Value = "Stop"
Let [d4] = 0
Let [d1] = MinuteCounter
[a8:d8].Resize([d1].Value).ClearContents
[b2:b3].ClearContents
End Sub
And here is a printscreen from the file
Can you please test the code and tell me about the discrepancies in your PC? Am I doing something wrong or is it the xlam things slowing it down?
Please email me at [email address removed] to receive the file if you want to give it a try using my file.
Thanks,
Adam
Last edited by a moderator: