Ruddles
Well-known Member
- Joined
- Aug 24, 2010
- Messages
- 5,851
- Office Version
- 365
- Platform
- Windows
Just out of idle curiosity, I've just written a bit of code to capture the color samples from Excel's cell background color picker. After two days of profanity, I finally realised that the SendKeys method's {enter} and {return} strings are not the same. Believing them to be identical, I always use {enter} rather than {return} (less typing, obviously!) but when I changed all the {enter}s to {return}s, my misbehaving code suddenly worked perfectly.
It appears that {return} sends an extra line feed which {enter} doesn't, although The Google doesn't seem to know this (unless I'm searching for the wrong thing!) and I don't know any way of examining the contents of {enter} and {return} directly, but I can demonstrate this by using the VBE's Immediate window (see attached screen grab).
I can also demonstrate this with the following piece of code. If you place your cursor between the apostrophes and run the code, Test1 generates blank lines feed whereas Test2 doesn't:-
Have I been missing something all these years, did I not RTFM properly (or at all), did I not ask The Google politely enough or have I discovered Something New?
I'm quite happy to have my ignorance laid bare in a public forum – it wouldn't be the first time.
Comments, anyone?
It appears that {return} sends an extra line feed which {enter} doesn't, although The Google doesn't seem to know this (unless I'm searching for the wrong thing!) and I don't know any way of examining the contents of {enter} and {return} directly, but I can demonstrate this by using the VBE's Immediate window (see attached screen grab).
I can also demonstrate this with the following piece of code. If you place your cursor between the apostrophes and run the code, Test1 generates blank lines feed whereas Test2 doesn't:-
VBA Code:
Sub Test1()
Application.SendKeys "{return}"
Application.SendKeys "{return}"
Application.SendKeys "{return}"
Application.SendKeys "{return}"
Application.SendKeys "{return}"
' '
End Sub
Sub Test2()
Application.SendKeys "{enter}"
Application.SendKeys "{enter}"
Application.SendKeys "{enter}"
Application.SendKeys "{enter}"
Application.SendKeys "{enter}"
' '
End Sub
Have I been missing something all these years, did I not RTFM properly (or at all), did I not ask The Google politely enough or have I discovered Something New?
I'm quite happy to have my ignorance laid bare in a public forum – it wouldn't be the first time.
Comments, anyone?