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

后端 未结 2 1739
遇见更好的自我
遇见更好的自我 2021-01-27 04:47

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

2条回答
  •  既然无缘
    2021-01-27 05:26

    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
    

提交回复
热议问题