How to detect changes in cell format?

后端 未结 3 1428
南方客
南方客 2020-12-11 04:55

I want to embed a procedure in an Excel sheet that will detect when a cell\'s format changes, e.g. from Text to Number.

But I can\'t figure out how to get the cell\'

相关标签:
3条回答
  • 2020-12-11 05:02

    Based on this response on StackOverflow here is some code that might work for you, assuming the change will be user generated and the selected range will change after they make the change...

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        Static LastRange As Range
        Static LastNumberFormat As String
    
        If Not LastRange Is Nothing Then
            If LastRange.Cells(1).NumberFormat <> LastNumberFormat Then
                'Your action or message box notification goes here
            End If
        End If
    
        Set LastRange = Target
        LastNumberFormat = Target.NumberFormat
    
    End Sub
    
    0 讨论(0)
  • 2020-12-11 05:18

    Great question!

    If you are only looking to trigger an event on the NumberFormat change (you appear to be calling this incorrectly the data format, NumberFormat is the attribute you want), the following is a good example.

    I'm intercepting all selection change events and checking if any NumberFormat changed.

    Option Explicit
    
    'keep track of the previous
    Public m_numberFormatDictionary As New dictionary
    Public m_previousRange As Range
    
    Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    
        'requires reference to Microsoft Scripting Runtime
    
        Dim c As Variant
        Dim wasChange As Boolean
    
    
        Debug.Print "***********************"
    
        'make sure you had a previous selection and it was initialized
        If m_numberFormatDictionary.Count > 0 And Not m_previousRange Is Nothing Then
    
            'Iterate through all your previous formattings and see if they are the same
            For Each c In m_previousRange
                Debug.Print "Found " & c.NumberFormat & " in " & c.Address
                Debug.Print "Stored value is " & m_numberFormatDictionary(c.Address) & " in " & c.Address
    
                'print out when they are different
                If c.NumberFormat <> m_numberFormatDictionary(c.Address) Then
                    Debug.Print "~~~~~~ Different ~~~~~~"
                    wasChange = True
                End If
    
            Next c
        End If
    
        'clear previous values
        m_numberFormatDictionary.RemoveAll
    
        'Make sure you don't error out Excel by checking a million things
        If Target.Cells.Count < 1000 Then
    
            'Add each cell format back into the previous formatting
            For Each c In Target
                Debug.Print "Adding " & c.NumberFormat & " to " & c.Address
                m_numberFormatDictionary.Add c.Address, c.NumberFormat
            Next c
    
            'reset the range to what you just selected
            Set m_previousRange = Target
        End If
    
        'simple prompt now, not sure what your use case is
        If wasChange Then
            MsgBox "There was at least one change!"
        End If
    
    End Sub
    

    I'm not exactly sure what you are looking for, you'll have to modify the print/msgbox statements appropriately. Depending on your use case you may have to modify this slightly but it works in all my test examples.

    0 讨论(0)
  • 2020-12-11 05:29

    there does not appear to be an event that fires when a cells format type changes.

    however, I got this info from another forum site.

    To edit a cells format without the use of a macro the cells must be selected (via left click then an icon, or right click). so, using the worksheets SelectionChanged, whenever a cell is selected you grab the format and address of that cell as well as check the address and format of the previous cell VS what the format of the previous cell is now. thus, if the format of the previous cell is different now than it was when last captured, it has been changed.

    You can use this in conjuction with the change event, to see if the format has been changed between now (after the cells contents have changed) and when the cell was selected.

    here's the link to the other forum as I cant claim this as my own invention: http://www.mrexcel.com/forum/excel-questions/3704-calculate-format-change.html

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