To track style changes in Excel 2007/2010 using VBA

前端 未结 1 1651
甜味超标
甜味超标 2021-01-28 07:23

I need to track the cell style changes in some sheets. I cannot use the buid-in tracking in Excel 2007/2010 because I need to customize something. I tried to track the style cha

相关标签:
1条回答
  • 2021-01-28 08:02

    There is no event triggered on format change.

    The best workaround is to monitor the Worksheet_SelectionChange event. When the user clicks a cell, you have to store a reference to the cell, and all the format information you want to monitor for. Next time the event fires, you have to look back at the last cell they clicked, compare it's current format to your saved format information, and that will allow you to detect changes.

    The downside is you can only detect the change after they have clicked away from the cell they formatted.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
        Static LastRange As Range 'The last range selected'
        'For example, monitor the background color or the cell'
        Static LastColorIndex As Integer
    
        If LastRange.Cells(1).Interior.ColorIndex <> LastColorIndex Then
            'Do what you do'
        End If
    
        Set LastRange = Target
        LastColorIndex = Target.Interior.ColorIndex
    End Sub
    

    This is the simplest possible case. Things get more complicated if they modify an entire range of cells at once.

    0 讨论(0)
提交回复
热议问题