Results 1 to 3 of 3

Thread: [RESOLVED] Highlight active cell in Excel

  1. #1

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Resolved [RESOLVED] Highlight active cell in Excel

    I use this code to highlight the sheet's active cell:
    VB Code:
    1. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    2.     Static OldRange As Range
    3.     On Error Resume Next
    4.     Target.Interior.ColorIndex = 6 ' yellow - change as needed
    5.     OldRange.Interior.ColorIndex = xlColorIndexNone
    6.     Set OldRange = Target
    7. End Sub
    that I found here.

    All the cells in my sheet are white (default) except for a few in various other colours. Anyone knows how to modify the code so that the old colour is restablished after the cell is no longer actove?
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Highlight active cell in Excel

    Save the range to a module level range variable object. Then update it in the event. Set your range variable interior color back to default, set the new range to the color 6, then update the range variable object for the next event so you will know which range to reset.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    vbuggy krtxmrtz's Avatar
    Join Date
    May 2002
    Location
    In a probability cloud
    Posts
    5,573

    Re: Highlight active cell in Excel

    Quote Originally Posted by RobDog888
    Save the range to a module level range variable object. Then update it in the event. Set your range variable interior color back to default, set the new range to the color 6, then update the range variable object for the next event so you will know which range to reset.
    It works as you say, indeed. Thank you.
    VB Code:
    1. 'In a module
    2. Global oldrng As Range
    3. Global oldcol As Long
    4. 'In ThisWorkbook
    5. Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
    6.     On Error Resume Next
    7.     oldrng.Interior.ColorIndex = oldcol
    8.     oldcol = Target.Interior.ColorIndex
    9.     Target.Interior.ColorIndex = 6 ' yellow or whatever
    10.     Set oldrng = Target
    11. End Sub
    Lottery is a tax on people who are bad at maths
    If only mosquitoes sucked fat instead of blood...
    To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)

Posting Permissions

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



Click Here to Expand Forum to Full Width