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
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
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.