|
-
Jun 22nd, 2006, 04:53 PM
#1
[RESOLVED] Highlight active cell in Excel
I use this code to highlight the sheet's active cell:
VB Code:
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
Static OldRange As Range
On Error Resume Next
Target.Interior.ColorIndex = 6 ' yellow - change as needed
OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target
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)
-
Jun 22nd, 2006, 06:40 PM
#2
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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Jun 23rd, 2006, 03:50 AM
#3
Re: Highlight active cell in Excel
 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:
'In a module
Global oldrng As Range
Global oldcol As Long
'In ThisWorkbook
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Excel.Range)
On Error Resume Next
oldrng.Interior.ColorIndex = oldcol
oldcol = Target.Interior.ColorIndex
Target.Interior.ColorIndex = 6 ' yellow or whatever
Set oldrng = Target
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|