简体   繁体   中英

Private Sub Worksheet_Change combine two codes

I am trying to combine these two codes, the first one is to change the name of my worksheet when I change the value of the cell m3, and the second code is to block the cells after modifying the cells. I am new in VBA so I don't know how to combine them.

CODE 1

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$M$3" Then RenameSheet
End Sub

CODE 2

Private Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range

   On Error Resume Next
    Set xRg = Intersect(Range("F6"), Target)
    If xRg Is Nothing Then Exit Sub

     Target.Worksheet.Unprotect Password:="1234"

     xRg.MergeArea.Locked = True

     Target.Worksheet.Protect Password:="1234"

End Sub

Also sub renamesheet code is:

Sub RenameSheet()

    Dim rs As Worksheet
    For Each rs In Sheets
    If rs.Name <> "MENU" And rs.Name <> "CAJA_CONTABILIDAD" Then
        rs.Name = "Vale " & rs.Range("M3")
    End If
    Next rs

    If Target.Address = "$M$3" Then RenameSheet
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
    If Target.Address = "$M$3" Then
        RenameSheet
        Exit Sub
    End If

    Dim xRg As Range
    Set xRg = Intersect(Range("F6"), Target)
    On Error Resume Next
    If Not xRg Is Nothing Then
        Target.Worksheet.Unprotect Password:="1234"
        xRg.MergeArea.Locked = True
        Target.Worksheet.Protect Password:="1234"
    End If

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