简体   繁体   中英

Using Excel VBA Events to detect merging/unmerging of cells?

I am trying to find a way to detect the immediate use of merging (or unmerging) cells. The change event triggers nor does the selectionchange. I've tried some of the other, but it seems no events trigger upon the merge - which I find odd. My code in the change event currently changes Interior.Color based off the contents of the cell. If the cell is merged and then unmerged, the color stays across the selection. But I would like it to keep the color only in the cell with the text and go back to xlNone on the rest. Anyway to pull this off?

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column < 3 Then Exit Sub
If Target.Row <> 3 Then Exit Sub
Select Case Target.Text
    Case "Blackberry Serrano", "BS"
        Target.Interior.Color = RGB(120, 33, 111)
    [...more of the same...]
    Case ""
        Target.Interior.Color = xlNone
End Select
End Sub

As far as I know, there is no way to detect formatting changes on a cell. Merging is considered a format change and is therefore undetectable via VBA events. However, I did some digging, and found this interesting list:

  • Changing the formatting of a cell doesn't trigger the Change event (as expected). But copying and pasting formatting does trigger the Change event. Choosing the Home=>Editing=>Clear=>Clear Formats command also triggers the event.
  • Merging cells doesn't trigger the Change event, even if the contents of some of the merged cells are deleted in the process.
  • Adding, editing or deleting a cell comment doesn't trigger the Change event.
  • Pressing Delete generates an event even if the cell is empty to start with.
  • Cells that are changed by using Excel commands may or may not trigger the Change event. For example, sorting a range or using Goal Seeker to change a cell does not trigger the event. But using the spell checker does.
  • If your VBA procedure changes the contents of a cell, it does trigger the Change event.

From Excel 2013 Power Programming with VBA by John Walkenbach Source

So basically, if someone is just merging or unmerging cells, there is no way to detect that through a VBA event.

I found my own kind of sort of work around to make the things happen that I wanted. Not sure if there is a more efficient/speedier way to do it, but it does at least work without any errors thus far. It triggers after the selection is changed which is slower than what I wanted but VBA is what it is.

For the worksheet:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Call EnforceFormatting(Target)
End Sub

In a module:

Public oldR As Range
Public newR As Range

Sub EnforceFormatting(ByVal Target As Range)
    Set newR = Target
    If oldR Is Nothing Then
        Set oldR = newR
        Exit Sub
    End If

    If oldR.Column < 3 Then
        Set oldR = newR
        Exit Sub
    End If
    If oldR.Row <> 3 And oldR.Row <> 7 And oldR.Row <> 11 Then
        Set oldR = newR
        Exit Sub
    End If
    If oldR.Rows.Count > 1 Then
        Set oldR = newR
        Exit Sub
    End If
    If oldR.Count > 1 Then
        Application.ScreenUpdating = False
        Dim c As Range
        For Each c In oldR
            c.Value = c.Text
        Next c
        Application.ScreenUpdating = True
    End If

    Set oldR = newR
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM