When Tab or Enter Is Hit, Change The Cell Color of (previously) Selected Cell

ExcelBean

New Member
Joined
Apr 4, 2014
Messages
36
Is there a way to make the cell color change every time you hit tab or enter?

Thanks in advance!
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
One way to do this is to get a Tab or Enter key press with the Application.OnKey function, send it to some code to color the cell and move the cursor.
The only thing that may be a little different is that with normal Tab/Enter keys, if you tab to the right and hit enter, it acts more as a carriage return, selecting the cell one row down and under the original cell before you hit the tab. The code below only makes the cursor move to the right on enter.

I put this code in the worksheet I want to know about. It could also be put in a Workbook_Activate sub to look at the whole workbook, just make sure to put the corresponding deactivation code in the right place so it resets when you're done.
Code:
Private Sub Worksheet_Activate()
[COLOR=#00ff00]'keypad Enter key[/COLOR]
Application.OnKey "{ENTER}", "EnterKey"
[COLOR=#00ff00]'keyboard Enter key[/COLOR]
Application.OnKey "~", "EnterKey"
Application.OnKey "{TAB}", "TabKey"
End Sub


Private Sub Worksheet_Deactivate()
[COLOR=#00ff00]'resets the key function[/COLOR]
Application.OnKey "{ENTER}"
Application.OnKey "~"
Application.OnKey "{TAB}"
End Sub


Put this in a module:

Code:
Sub EnterKey()
[COLOR=#00ff00]'set the color to green[/COLOR]
ActiveCell.Interior.ColorIndex = 4
[COLOR=#00ff00]'move the cursor down one row[/COLOR]
ActiveCell.Offset(1, 0).Select
End Sub


Sub TabKey()
[COLOR=#00ff00]'set the color to green[/COLOR]
ActiveCell.Interior.ColorIndex = 4
[COLOR=#00ff00]'moves the cursor right one row[/COLOR]
ActiveCell.Offset(0, 1).Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,911
Messages
6,122,198
Members
449,072
Latest member
DW Draft

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