Following link explains what is the difference between SendKeys wait argument false or true.
Wait Optional. Boolean value specifying the wait mode. If False (default), control is returned to the procedure immediately after the keys are sent. If True, keystrokes must be processed before control is returned to the procedure.
But I dont understand what that means.
Here is the first code:
Sub Macro1_ForFalseExample()
'If False (default), control is returned to the procedure immediately after the keys are sent
Range("A1").Select
SendKeys "Fruit Name", False
Range("A2").Value = "Apple"
End Sub
Here is the second code:
Sub Macro2_ForTrueExample()
'If True, keystrokes must be processed before control is returned to the procedure.
Range("B1").Select
SendKeys "Car Name", True
Range("B2").Value = "Toyota"
End Sub
I cant see any difference when I run the codes above.
With
SendKeysit can be helpful to think of thewaitargument is giving you the option ofSendKeysoperating synchronously (wait=True) or asynchronously (wait=False). It is of course not quite that simple but it can help to understand that implications.If the message is simple and has no processing overheads, then you will not notice the difference between
TrueorFalse. Both will appear to return immediately.A good default is to send
Trueunless your code specifically needs to continue processing without waiting for the message to be received. The only side effect of this is that your code will pause momentarily and wait for the program to respond. Think of this as giving the application time to react to your message. Send Keys is generally used to initiate keyboard shortcuts or to automate user interfaces that do not have an API, as such usages ofSendKeysmight initiate long running processes or start external programs and you might not want to wait for them to complete before you continue, that is when you sendFalse.A common example where wait=
Truematters is when using copy^cor paste^vwith large ranges. These take time to process and you would want that operation to complete before the next line of your code executes. otherwise if your next line of code tried to select another range, you might find that the application has hung or if your code tries to paste before the copy operation has completed it might paste the previous captured content, or nothing at all.If you have no follow up code after
SendKeys, then it is irrelevant if you wait or not, in this casewait=Falsemight give the impression of greater performance because your code will continue executing immediately.The general implication of waiting or not is that the application might have key event handlers logic that takes time to execute, in some cases the application might even prevent processing. If your logic checks the UI after
SendKeysthen you will always want to usewait=True.Take the example from the MS VBA Reference that is automating the calculator:
If you did not wait for the application to finish processing the key press your code might run faster than the UI and could result in the UI missing some of the key press messages. If all of send keys worked then you should get an answer of
5050. Imagine that the calculator needed 300ms to process the messages, some or all of the messages might be missed if your loop logic operated faster than that.SendKeysis not a universal queue, if the application is not ready to receive input then the message will be ignored,wait=Truemeans that you want to allow the process in focus to take the time that it needs (if it needs any) to complete the operation before you start sending more key commands.