Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Change the border color of a cell

Posted on 2011-03-14
7
1,300 Views
Last Modified: 2012-05-11
Experts,
I'd like to be able to change to default color of a cell border to red everytime that cell get's a focus.
By focus, I mean if move move to a cell via a macro or click on it cursor the cell border would be red.
0
Comment
Question by:Frank Freese
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:Mdragga
ID: 35132558
It sounds like you want the border of the active cell to be a different color? I'm not sure if I'm understanding what you mean by "Focus" but with the active cell as my understanding

http://cpearson.com/excel/rowliner.htm

That website has a small macro Excel file that operates as an Add-In. It is pretty old but it seems to function with some customization options

OR

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Dim v As Variant
Dim r As Range
Dim i As Long

v = Array(xlEdgeBottom, xlEdgeTop, xlEdgeRight, xlEdgeLeft)
For Each r In Sh.UsedRange.Cells
With r
For i = 0 To 3
..Borders(v(i)).LineStyle = xlNone
Next
End With
Next

For i = 0 To 3
With Target(1).Borders(v(i))
..LineStyle = xlContinuous
..Weight = xlThick
..ColorIndex = 3
End With
Next
End Sub

I believe that macro should function for single cells as well as a group of cells but since I don't have Excel 2010 on this machine I'm unable to test it.
0
 

Author Comment

by:Frank Freese
ID: 35132595
active cell is the same as focus
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 35132662
Hello,

the code suggested above does work in Excel 2010, but I suggest you change the colorIndex to 8. The highlighted cell will have its border in inverse colors, so it will appear turquoise rather than red, although it is set to red. The above code is also rather slow and will remove any existing cell borders.

If you don't want to destroy existing borders, you will need to store the existing cell border definitions before applying the red border, then, upon changing to another cell you will need to apply the original settings again. This can get rather complex if the code is meant to work for a range of cells as well as a single cell. Each side of the cell may have a different setting for line style, thickness and color.

As an alternative you could change the cell background color to highlight the cell. This code, found on http://www.ozgrid.com/forum/showthread.php?t=50240&page=1, does the trick nicely.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static rngPrev As Range, PrevColor As Integer
    Dim TempColor As Integer
    TempColor = Target.Cells(1, 1).Interior.ColorIndex
     
    If Not rngPrev Is Nothing Then rngPrev.Interior.ColorIndex = PrevColor
    PrevColor = TempColor
     
    With Target.Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
    Set rngPrev = Target
End Sub

Open in new window


Right-click the Sheet tab, select "View Code" and paste into the code window.

cheers,
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Frank Freese
ID: 35132897
teylyn
close real close - I'd like restrict the color change to R6 in column A thru R80 column A
0
 
LVL 50

Expert Comment

by:Ingeborg Hawighorst
ID: 35133022
No problem.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range("R6:R80")) Is Nothing Then
    
    Static rngPrev As Range, PrevColor As Integer
    Dim TempColor As Integer
    
    TempColor = Target.Cells(1, 1).Interior.ColorIndex
     
    If Not rngPrev Is Nothing Then rngPrev.Interior.ColorIndex = PrevColor
    PrevColor = TempColor
     
    With Target.Interior
        .ColorIndex = 3
        .Pattern = xlSolid
    End With
    Set rngPrev = Target
End If
End Sub

Open in new window


Whichever cell was last selected in R6:R80 will remain highlighted until you select another cell in R6:R80. If you want the color removed when you click a cell outside that range, then try

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
  
    Static rngPrev As Range, PrevColor As Integer
    Dim TempColor As Integer
    
    TempColor = Target.Cells(1, 1).Interior.ColorIndex
     
    If Not rngPrev Is Nothing Then rngPrev.Interior.ColorIndex = PrevColor
    PrevColor = TempColor
    If Not Intersect(Target, Range("R6:R80")) Is Nothing Then
        With Target.Interior
            .ColorIndex = 3
            .Pattern = xlSolid
        End With
        Set rngPrev = Target
    End If
End Sub

Open in new window


cheers, teylyn
0
 
LVL 50

Accepted Solution

by:
Ingeborg Hawighorst earned 500 total points
ID: 35133039
Hang on, I think I mis-interpreted your directions. What is "R6 in column A"?

Do you mean "row 6 in column A", i.e. A6 through to A80? If so, change

If Not Intersect(Target, Range("R6:R80")) Is Nothing Then

to

If Not Intersect(Target, Range("A6:A80")) Is Nothing Then

cheers, teylyn
0
 

Author Closing Comment

by:Frank Freese
ID: 35133515
thanks - that gave me enough to play with. good job and thank you
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question