VBA - disable {ENTER} key but enable carriage return in cell...

RobbieC

Active Member
Joined
Dec 14, 2016
Messages
376
Office Version
  1. 2010
Platform
  1. Windows
Hi there, I have a workbook which is purely for user input ie. the user is just inputting data.

Many of the cells are designed to hold large amounts of text (which are formatted to wrap text). However when many people type, they like to hit the {return} key to invoke a carriage return. Excel always goes to the next cell below when {return} is hit...

Is there a way to invoke a carriage return in a cell rather than go to the next cell?

If you can point me in the right direction, I'd be most grateful. Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Maybe something like this :
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

Do not forget to unhook the keys when not needed.
 
Upvote 0
This is almost working Jaafar, when you hit {enter} the carriage returns works, but the cursor stops flashing in the cell, therefore when you type something else (a second line) it just writes over what has already been inputted...

How can I keep the cursor i the cell after the carriage return?
 
Upvote 0
The code I posted worked for me fine and I didn't experience the problem you described.
Try removing the the DoEvents statement and the second call to SendKeys from the carriage_return sub and see what you get .
 
Upvote 0
Brilliant Jaafar. I saved the workbook, closed it and re-opened it and it worked! Not sure why it wasn't working before...

Thanks very much for your help
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,120
Members
451,399
Latest member
alchavar

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