+ Reply to Thread
Results 1 to 11 of 11

a way to color the CURRENT row or cell when selected

  1. #1
    Registered User
    Join Date
    05-19-2005
    Posts
    3

    Exclamation a way to color the CURRENT row or cell when selected

    I have a table with many data and I would like to have the CURRENT cell or row colored for an easier view.
    The below macro is working well but it deactivates both the borders and colors.


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Cells.Borders().LineStyle = xlNone
    Cells.Interior.ColorIndex = 0
    ActiveCell.EntireRow.BorderAround xlDash, ColorIndex:=5
    ActiveCell.EntireRow.Interior.ColorIndex = 36
    End Sub

    any ideas how to keep the borders and colors? are there any other methods?
    Thanks!!!

  2. #2
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    change it to:

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Cells.Interior.ColorIndex = 0
    ActiveCell.Interior.ColorIndex = 36
    End Sub

    Note: If you have any cells colored previously, then they disappear on using this macro.

    Do you want to retain other colors...?

    - Mangesh

  3. #3
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Use the following to retain your older colors:


    Dim myAddress
    Dim myColor

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)


    If Not IsEmpty(myAddress) Then
    Range(myAddress).Interior.ColorIndex = myColor
    End If

    myAddress = ActiveCell.Address
    myColor = ActiveCell.Interior.ColorIndex
    ActiveCell.Interior.ColorIndex = 36

    End Sub



    - Mangesh

  4. #4
    Registered User
    Join Date
    05-19-2005
    Posts
    3
    Thanks! You solved the problem with the cell coloring.
    Is it possible to keep the sheet colors and yet to color the whole row when I select it?

  5. #5
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    That would become quite complicated as the program has to remember the colors in all cells in that row

    Mangesh

  6. #6
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Try this:

    Dim n
    Dim myRow
    Dim myColors(1 To 20)

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    n = 20 ' nos of cells in row whose formatting is to be remembered


    If Not IsEmpty(myRow) Then
    For j = 1 To n
    Cells(myRow, j).Interior.ColorIndex = myColors(j)
    Next j
    End If

    myRow = ActiveCell.Row
    For i = 1 To n
    myColors(i) = Cells(myRow, i).Interior.ColorIndex
    Next i

    ActiveCell.EntireRow.Interior.ColorIndex = 36

    End Sub



    This remembers the colors in first 20 cells of the row. Replace 20 in code to suit your requirement

    Mangesh

  7. #7
    Registered User
    Join Date
    05-19-2005
    Posts
    3
    Congratulation! It works really well. I replaced 20 with 256 which is the maximum and still works fine.

    Well done!!!!!!!!

    I just hope the people to whom I'm going to send the file won't save the file with the colored line after closing it.

  8. #8
    Forum Contributor
    Join Date
    06-10-2004
    Location
    India
    Posts
    1,066
    Good it works. My only fear for using 256 was the time factor. Thanks for the feedback

    Mangesh

  9. #9
    Registered User
    Join Date
    06-06-2005
    Posts
    1

    deactivation of function "Sub Worksheet_SelectionChange"

    How can deactivate the function Sub Worksheet_SelectionChange?

  10. #10
    Nick Hebb
    Guest

    Re: a way to color the CURRENT row or cell when selected

    If you want the currently selected range/cell to be colored and the
    color cleared from the previously selected range or cell, then you will
    need to keep track of that last range selected.

    Declare a private variable at the module level, and set its range upon
    activation. Then when SelectionChange is called, clear the last range,
    color the target range and set the last range variable to the current
    target, as follows

    Private lastRange As Range ' module level declaration

    Private Sub Worksheet_Activate()
    Set lastRange = ActiveCell
    End Sub

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    lastRange.Interior.ColorIndex = xlColorIndexNone
    Target.Interior.Color = RGB(255, 255, 0) ' yellow
    Set lastRange = Target
    End Sub


  11. #11
    Registered User Ivan F Moala's Avatar
    Join Date
    10-25-2003
    Location
    Auckland, New Zealand
    Posts
    71
    This maybe useful for you ??

    http://office.microsoft.com/en-us/as...366231033.aspx

    If conditionall formating is a problem ... then here is an alternative.

    http://www.xcelfiles.com/Excel02.html#anchor_67
    Kind Regards,
    Ivan F Moala From the City of Sails
    \1

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1