SendKeys: {enter} and {return} are not the same!

Ruddles

Well-known Member
Joined
Aug 24, 2010
Messages
5,851
Office Version
  1. 365
Platform
  1. 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:-

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?
 

Attachments

  • sendkeys.png
    sendkeys.png
    28.2 KB · Views: 11

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Before the days of electric typewriters we had 2 functions - return and line feed. Return simply moved the carriage to the beginning of the line you were on, although I don't recall any way of doing that except for manually pushing the carriage back. There may have been a backspace key that you'd have to press a bunch of times to replicate that, but not many typists would do that. Linefeed rolled the paper up one or more lines depending on whether or not you had set the feed to be two lines. If you used the mechanical lever for these operations you created line feeds and carriage returns in one go.

Along came electric typewriters with their buttons. IIRC, the key that caused line feeds and returns was labelled "Return". Along came computers, which of course, interact differently in that you don't roll screens like you roll up paper in a typewriter. Now we have Enter, the main distinction being that it behaves according to the application its used in. In some, it will give you a new paragraph, maybe even double spaced. In others, it may produce a new line, indented equal to the line above it (VBE). In Excel, cursor moves to a new row and maybe even chooses the first empty column from the left in that row rather than just the leftmost. In an Access db form, Enter typically moves the cursor to the next field in the tab order, but so does tab key. It usually does not create a LF/CR in the same form control and in many cases will commit a record to the table as well.

Clear as mud?
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top