Solved

Change the border color of a cell

Posted on 2011-03-14
7
1,301 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

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
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.

 

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

726 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