Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change the border color of a cell

Posted on 2011-03-14
7
Medium Priority
?
1,311 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

721 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