Solved

Change the border color of a cell

Posted on 2011-03-14
7
1,298 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
Comment Utility
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
Comment Utility
active cell is the same as focus
0
 
LVL 50

Expert Comment

by:teylyn
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Frank Freese
Comment Utility
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:teylyn
Comment Utility
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:
teylyn earned 500 total points
Comment Utility
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
Comment Utility
thanks - that gave me enough to play with. good job and thank you
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

A2 = A1 That kind of cell reference is relative.  If you copy it from A2 to B2, then B2 will get this: B2 = B1 That's all fine and good, but if you then insert a new row above row 2, you'll find: A3 = A1 B3 = B1 This is intentional. …
Convert between Excel file formats (.XLS, .XLSX, .XLSM) with/without macro option David Miller (dlmille) Intro Over this past Fall, I've had the opportunity to see several similar requests and have developed a couple related solutions associate…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…

728 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now