Solved

Change the border color of a cell

Posted on 2011-03-14
7
1,306 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
[X]
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
  • 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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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
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 (Microsoft MVP / EE MVE) 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

630 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