Merge cells doesn't work after locking and unlocking other cell

拥有回忆 提交于 2021-01-29 09:41:43

问题


I've got a sheet in which I'm trying to merge some cells based on CX cell value. CX cell is also dynamically locked/unlocked based on BX cell value. Although locking/unlocking works fine, I get 1004 error when I'm trying to merge cells with line: Range(Cells(Target.Row, i), Cells(Target.Row + Target.Value - 1, i)).Merge

While code is below.

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Not Intersect(Target, Range("B14:B50")) Is Nothing And Sh.Name <> "Dane" Then
        Dim pass As String
        pass = "" 'set the password. Otherwise, protection/unprotection is done without a pass
        If Target.Cells.Count > 1 Then Exit Sub
        ActiveSheet.Unprotect pass
        If Target.Value = "Unlocked" Then
            Target.Offset(0, 1).Locked = False
        Else
            Target.Offset(0, 1).Value = 0
            Target.Offset(0, 1).Locked = True
        End If
        ActiveSheet.Protect pass
    End If
    
    If Not Intersect(Target, Range("C14:C50")) Is Nothing And Sh.Name <> "Dane" Then
        Dim i As Long
                
        Application.DisplayAlerts = False
        For i = 1 To 8 Step 1
            If i <> 6 And i <> 7 And Cells(Target.Row, i).MergeCells Then
                Cells(Target.Row, i).UnMerge
            End If
        Next i
        If Target.Value <> 0 Then
            For i = 1 To 8 Step 1
                If i <> 6 And i <> 7 Then
                    Range(Cells(Target.Row, i), Cells(Target.Row + Target.Value - 1, i)).Merge
                End If
            Next i
        End If
        Application.DisplayAlerts = True
    End If
    
End Sub

回答1:


Okay. So with @FaneDuru comments, I've been able to figure out the issue. Sheet protection was causing the problem, so I had to unprotect and reprotect sheet before for loops in second condition. Also, @FaneDuru advised to set EnableEvents = false there, to prevent any infinite loop issue. Below is the fixed code.

Option Explicit

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Not Intersect(Target, Range("B14:B50")) Is Nothing And Sh.Name <> "Dane" Then
        Dim pass As String
        pass = "" 'set the password. Otherwise, protection/unprotection is done without a pass
        If Target.Cells.Count > 1 Then Exit Sub
        ActiveSheet.Unprotect pass
        If Target.Value = "Unlocked" Then
            Target.Offset(0, 1).Locked = False
        Else
            Target.Offset(0, 1).Value = 0
            Target.Offset(0, 1).Locked = True
        End If
        ActiveSheet.Protect pass
    End If
    
    If Not Intersect(Target, Range("C14:C50")) Is Nothing And Sh.Name <> "Dane" Then
        Dim i As Long
                
        Application.DisplayAlerts = False
        Application.EnableEvents = False
        ActiveSheet.Unprotect pass
        For i = 1 To 8 Step 1
            If i <> 6 And i <> 7 And Cells(Target.Row, i).MergeCells Then
                Cells(Target.Row, i).UnMerge
            End If
        Next i
        If Target.Value <> 0 Then
            For i = 1 To 8 Step 1
                If i <> 6 And i <> 7 Then
                    Range(Cells(Target.Row, i), Cells(Target.Row + Target.Value - 1, i)).Merge
                End If
            Next i
        End If
        ActiveSheet.Protect pass
        Application.EnableEvents = True
        Application.DisplayAlerts = True

    End If
    
End Sub

However, I'm still wondering why I need to unprotect the sheet to edit unlocked range.



来源:https://stackoverflow.com/questions/64751127/merge-cells-doesnt-work-after-locking-and-unlocking-other-cell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!