如何检测单元格格式的变化?

我想在Excel工作表中embedded一个过程,当单元格的格式发生变化时,例如从文本到数字。

但我不知道如何获得单元格的格式types。 我尝试使用Worksheet_Change事件处理程序来检查数据types,如下所示:

 Private Sub worksheet_change(ByVal Target As Range) If Target.Address = "a1" Then If VarType(Target) <> 5 Then MsgBox "cell format has been changed" End If End If End Sub 

但是有了这个代码,如果我将单元格A1的数据types从数字更改为文本,则不会触发Worksheet_Change ; 如果我更改单元格的内容,则仅调用事件处理程序。

另外,这个程序可以检测内容是否从一个数字改变为一个字母串,例如从“35.12”到“abcd”,而不是数字型号码到文本型号码; 如果将单元格B1设置为文本,则input“40”,然后将单元格B1的内容粘贴到单元格A1中, vartype()仍然返回“5”,因此不会触发警报。

无论内容types是否更改,如何检测格式是否已更改?

好问题!

如果你只是想触发一个NumberFormat变化的事件(你看起来是不正确地调用这个数据格式, NumberFormat是你想要的属性),下面是一个很好的例子。

我拦截所有select更改事件,并检查是否有任何NumberFormat更改。

 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 

我不确定你在找什么,你必须适当地修改print / msgbox语句。 根据您的使用情况,您可能需要稍微修改一下,但是它适用于我的所有testing示例。

当单元格格式types改变时,似乎没有发生事件。

不过,我从另一个论坛网站获得了这个信息。

要编辑单元格格式而不使用macros,必须select单元格(通过左键单击然后单击图标或右键单击)。 因此,使用工作表SelectionChanged,无论何时select一个单元格,您都可以获取该单元格的格式和地址,以及检查之前单元格的地址和格式,以及之前单元格的格式。 因此,如果前一个单元格的格式与上次捕获的单元格格式不同,则已经更改。

您可以将其与更改事件结合使用,以查看在现在(单元格内容发生更改之后)和单元格被选中之间格式是否已更改。

这里的链接到另一个论坛,因为我不能说这是我自己的发明: http : //www.mrexcel.com/forum/excel-questions/3704-calculate-format-change.html

基于StackOverflow上的这个响应,这里有一些代码可能适用于你,假设更改将由用户生成,选定的范围将在更改后发生变化。

 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