VBA to simulate ALT {ENTER} in cell

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a bit of code to make the ENTER key add a carriage return whilst entering text into cell:

Code:
Sub HookEnterKey()
    Application.OnKey "{RETURN}", "carriage_return"
    Application.OnKey "{ENTER}", "carriage_return"
End Sub

Sub carriage_return()
    ActiveCell = ActiveCell & Chr(10)
    SendKeys "{F2}"
    DoEvents
    SendKeys "{F2}"
End Sub
but this will only put a new line at the end of the existing content in a cell.

Is there a way to do this but to simulate ALT + {ENTER} ?.. So that it inserts the linebreak at the point of the cursor and not at the end. I've looked everywhere, but I can't find anything...

If you can point me in the right direction, I'd be very grateful

Thanks
 
Last edited:

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
You can't run code in edit mode in a cell, so I'm afraid not. Why can't you just use Alt+Enter?
 
Upvote 0
But my code above using sendkeys on the RETURN key does work whilst in edit mode on a cell. It is running the sub carriage_return... which puts a new line at the end of the current text

If it could be replaced with something along the lines of SendKeys "%{ENTER}" (ALT ENTER)

or the code could sit on a button with a macro to insert the linebreak at the point of the cursor
 
Upvote 0
But my code above using sendkeys on the RETURN key does work whilst in edit mode on a cell.

It doesn't actually. It exits edit mode and then runs the code, so the cursor isn't anywhere in the formula bar at the point the code runs.
 
Upvote 0
ah, I see. Thanks Rory.

I shall put a note on the document for the users to use ALT + ENTER to add a new line
 
Upvote 0
depending on your users it may be helpfull to use an input box where users will insert the new text

then you add it through the code to the cell
 
Upvote 0

Forum statistics

Threads
1,214,655
Messages
6,120,760
Members
448,991
Latest member
Hanakoro

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