0

vba excel can I set sleep lower than 500 (.5 secs) for sendkeys and still run smoothly

the first delay method that I used is application.wait now + 0.00001 that is (1secs). Now I found that

Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
sleep 500 '(0.5 secs)

is way better than application.wait

Is there a way or a method that can use delay less than .5secs?

  • 1
    https://stackoverflow.com/q/21039489/7599798 – FunThomas Feb 17 '21 at 09:07
  • Does this answer your question? [SendKeys doesn't always work](https://stackoverflow.com/questions/21039489/sendkeys-doesnt-always-work) – FunThomas Feb 17 '21 at 09:32
  • no, it has not. i'd tried `sleep 1` not working haha – user10182078 Feb 18 '21 at 00:35
  • And what does *"not working"* mean? Possibly `sleep 1` waits more than 1 ms, but testing with running 1000 time `sleep 10` took precisely 10s on my computer. What is your issue (and what is so funny?) – FunThomas Feb 18 '21 at 08:13
  • my bad. Sorry for not including details. When I tried `sendkeys "^c"` (copy) `sendkeys "^v"` (paste) with `sleep 1` on every field or textbox on another application. It seems to confuse which cell has to copy. The results is: the value needed to copy and paste on the assign field or textbox are not correct. Sorry for my english, im not very good at it. and as for my "haha comment" im laughing about my stupidness. Again Sorry and Thank you for your response. Appreciate it ^^ – user10182078 Feb 19 '21 at 00:28

1 Answers1

0

if you use Application.Wait, this is a VBA function, and you can have a minimum wait time of 1 second,

Application.Wait (Now + TimeValue("0:00:10"))

the example is a 10 second wait.

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • I already tried that method. As you said, 1secs is the minimum needed when using a sendkeys. While using "Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)" Sleep. Minimum is 500 that is 0.5 secs. Now, I need more faster than that. I dont know if that is impossible hehe – user10182078 Feb 18 '21 at 00:04
  • Apologies, I have tried to look at other solutions in VBA, if you managed to do this for less than 0.5 seconds. How reliable can it be? I have by using sleep it uses the processors ticks as well so could be a little accurate. – Shaqil Ismail Feb 18 '21 at 14:03