+ Reply to Thread
Results 1 to 4 of 4

Using Worksheet_SelectionChange() event macro

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176

    Using Worksheet_SelectionChange() event macro

    I saw following link regarding highlighting rows while in an excel spreadsheet.

    http://www.mcgimpsey.com/excel/highl...iteRetainColor

    It indicates to use a Worksheet_SelectionChange() event macro. Here is the code:
        Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
            Cells.Interior.ColorIndex = xlColorIndexNone
            ActiveCell.EntireRow.Interior.ColorIndex = 36
        End Sub
    I can’t figure out how to use the code and I can’t figure out how to turn the code “on” to use it. Any help is appreciated as I am new to event macros.

    P.S. once I figure out how to turn it “on”, how do I then turn it “off”?

  2. #2
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Right click sheet > Select view code > Paste in code

    So if your in sheet 1 it will hightlight the activecell row only for that sheet

    http://www.contextures.com/xlvba01.html#Worksheet

    VBA Noob
    _________________________________________


    Credo Elvem ipsum etian vivere
    _________________________________________
    A message for cross posters

    Please remember to wrap code.

    Forum Rules

    Please add to your signature if you found this link helpful. Excel links !!!

  3. #3
    Valued Forum Contributor
    Join Date
    11-20-2003
    MS-Off Ver
    2010, 2016
    Posts
    1,176
    Thanks for the link. I was able to turn it "on". Is there a way to turn this feature "off" without having to delete it in VBA?

    One more question. Sorry to be a pain. If I have multiple workbooks open and each has multiple sheets, Can I turn on this feature for all spreadsheets that are open?

  4. #4
    Forum Contributor VBA Noob's Avatar
    Join Date
    04-25-2006
    Location
    London, England
    MS-Off Ver
    xl03 & xl 07(Jan 09)
    Posts
    11,988
    Not ideal but use a cell to toggle. If A1 says off it stops (note you may have to double click cell A1 to remove format) then type on in A1 to start


    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Range("A1").Value = "off" Then
            Cells.Interior.ColorIndex = xlColorIndexNone
            ActiveCell.EntireRow.Interior.ColorIndex = 36
        Else
            ActiveCell.EntireRow.Interior.ColorIndex = xlNone
        End If
    
    End Sub
    Code only works by sheet so all sheets need the code. You could try putting in thisworkbook (see link)

    Which starts when you open

    Private Sub Workbook_Open()
    VBA Noob
    Last edited by VBA Noob; 09-27-2007 at 05:46 PM.

+ 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