Podświetlenie aktywnej komórki
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
Udało mi się zmodyfikować kod tak, aby uwzględniał dotychczasowe formatowanie (kolor) komórki:
Public oldrange As Range, oldcolor As Single
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As _ Excel.Range)
On Error Resume Next
oldrange.Interior.ColorIndex = oldcolor
oldcolor = Target.Interior.ColorIndex
Target.Interior.ColorIndex = 6 ' yellow - change as needed
Set oldrange = Target
End Sub
Kolejny wariant kodu, nadesłany przez kolegę Janusza. Format nie jest zmieniany jeżeli komórka ma inny kolor niż "żaden", formatowanie jest usuwane w przypadku zamknięcia zeszytu, lub zmiany arkusza.
Public oldrange As Range, oldcolor As Single
Private Sub Workbook_BeforeClose(Cancel As Boolean)
On Error Resume Next
If ActiveCell.Interior.ColorIndex = 6 Then
ActiveCell.Interior.ColorIndex = xlNone
End If
End Sub
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
On Error Resume Next
If ActiveCell.Interior.ColorIndex = 6 Then
ActiveCell.Interior.ColorIndex = xlNone
End If
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
On Error Resume Next
If Target.Interior.ColorIndex = xlNone Then
oldrange.Interior.ColorIndex = oldcolor
oldcolor = Target.Interior.ColorIndex
Target.Interior.ColorIndex = 6
Set oldrange = Target
End If
End Sub