Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying the background color of an active cell 3

Status
Not open for further replies.

W0RKAH0LIC

Technical User
Jun 18, 2003
3
US
How do you modify the background color of an active cell? I want the clicked cell to stand out more than the background.
 
How do I put "{code]
ActiveCell.Interior.ColorIndex = 3"
into the excel program?

 
Sometimes we forget that not everyone is familiar with VBA and how to use it.

If you right-click on the sheet tab, the VBA editor should open and you should see a window that has something that looks like this in it:
Code:
  Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)

  End Sub
If you replace everything in that window with this...
[blue]
Code:
Option Explicit
Dim LastCell As String
Dim LastColor As Integer

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
  If Target.Cells.Count = 1 Then
    'MsgBox Target.Interior.ColorIndex
    If LastCell <> &quot;&quot; Then
      Range(LastCell).Interior.ColorIndex = LastColor
    End If
    LastCell = ActiveCell.Address
    LastColor = ActiveCell.Interior.ColorIndex
    ActiveCell.Interior.ColorIndex = 3
  End If
End Sub
[/color]

... then you can close the VBA editor and you will have the funcionality you requested (assuming you wanted the cell to become RED (ColorIndex=3). If you want some other color, change the number.

This routine also includes the code to turn the cell color back to what it was before it was selected. (Unless you want to have the color changed permanently as a record of which cells you have visited.)

It gets tricky if you want to change the color of a cell manually. The routine sort of takes over and prevents that. The work-around is to select multiple cells and change the color of cells in groups.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top