View Code - VBcolor

DPChristman

Board Regular
Joined
Sep 4, 2012
Messages
171
Office Version
  1. 365
Platform
  1. Windows
I have a spreadsheet (form,really) that contains a lot of automatically updated fields from other spreadsheets.

Because of all the activity in this document, I don't want to lock any cells.

However, to give my users the ability to identify the current active cell.

I found an used this Worksheet_SelectionChange event by right clicking the tab, selecting view code, and pasting this

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.ScreenUpdating = False
Cells.Interior.ColorIndex = 0
Target.Interior.Color = vbCyan
Application.ScreenUpdating = True
End Sub

It works as advertised, but for some reason, it leaves all the other previously shaded rows un-shaded.

Is there a way to just highlight only the active cell?

The above script is supposed to do it, but apparently not
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
The code you posted removes ALL color from ALL cells, then highlights the active cell by filling it with cyan. What sort of "highlight" do you want?
 
Upvote 0
Essentially, just the active cell. This way, the user can identify what cell they will be typing in.

For example:
Store #: Location:
00000LOCATION - CITY, STATE


<colgroup><col span="5"></colgroup><tbody>
</tbody>
They would type in only the store number, and everything else would be auto-filled.

i don't want to have to protect the entire document, and un-protect certain cells, because the document requires the user to be able to edit headings were needed.

So the best option I though of would be to at least allow the document to highlight the cell they are currently in.
 
Upvote 0
Essentially, just the active cell. This way, the user can identify what cell they will be typing in.

For example:
Store #: Location:
00000LOCATION - CITY, STATE

<tbody>
</tbody>
They would type in only the store number, and everything else would be auto-filled.

i don't want to have to protect the entire document, and un-protect certain cells, because the document requires the user to be able to edit headings were needed.

So the best option I though of would be to at least allow the document to highlight the cell they are currently in.
That's what the code you posted does.
 
Upvote 0
Well, no. Because according to your initial response, 'The code you posted removes ALL color from ALL cells'. Because my document is a form for the users to fill out, the headers are shaded depending on the section of the form, etc. I don't want the color removed from any cells. I just want when the click into a cell they are going to type in, it highlights.
 
Upvote 0
Well, no. Because according to your initial response, 'The code you posted removes ALL color from ALL cells'. Because my document is a form for the users to fill out, the headers are shaded depending on the section of the form, etc. I don't want the color removed from any cells. I just want when the click into a cell they are going to type in, it highlights.
In that case, just remove this line:

Cells.Interior.ColorIndex = 0
 
Upvote 0
I actually thought of, and tried that.

When you remove Cells.Interior.ColorIndex = 0, it still highlights the active cells, but they stay highlighted after you go on to the next cells, not just the active cell
 
Upvote 0
I actually thought of, and tried that.

When you remove Cells.Interior.ColorIndex = 0, it still highlights the active cells, but they stay highlighted after you go on to the next cells, not just the active cell
Will the user always make a change to the active cell? If yes, you can highlight the cell when it is chosen and remove the highlight after the change is made.

Alternatively, if the users are restricted to a range outside the range that has your color-filled cells, you can change the existing code to remove all color just from that range before highlighting the active cell.
 
Upvote 0
Maybe this event code will work the way you want... it will only color selected cells (that are not already colored) with the vbCyan (actually, one more than that color value so that it does not interfere with normally cyan colored cells) and leaves all colored cells "untouched".
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
  Application.FindFormat.Interior.Color = vbCyan + 1 'one more than vbCyan's value
  Application.ReplaceFormat.Interior.Color = xlNone
  Cells.Replace "", "", SearchFormat:=True, ReplaceFormat:=True
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
  Application.FindFormat.Interior.Color = xlNone
  Application.ReplaceFormat.Interior.Color = vbCyan + 1 'one more than vbCyan's value
  Target.Replace "", "", SearchFormat:=True, ReplaceFormat:=True
  Application.FindFormat.Clear
  Application.ReplaceFormat.Clear
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,631
Messages
6,120,640
Members
448,974
Latest member
DumbFinanceBro

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