Combining two codes in VBA

vinidis

New Member
Joined
Aug 11, 2017
Messages
8
Hi!

I have a problem with two codes in VBA.
Im trying to combine the codes that insert a date in a cell, if i entered a value in the cell behind and a second code, that locks the cells that contains any value. They are work separatly.
I tried the "Call" function, named the two codes, then insert them below in sequence, but nothing happened.
After that, i tried a "Target" function. The difference was, in this method i used two moduls where i inserted the codes. This time i get the "Sub or function not defined!" error messege. It works in excel 2007 but i work in 2013.
Can anyone help me with this?

These are my codes:

Code:
 Private Sub Worksheet_Change(ByVal Target As Range)

On Error GoTo ErrHnd:


If Target.Column = 2 Then
  
    Application.EnableEvents = False
    If Not IsDate(Range("C" & Target.Row).Value) Then
        Range("C" & Target.Row).Value = Format(Date, "yyyy/mm/dd")
    End If
    Application.EnableEvents = True
    
End If

Exit Sub

ErrHnd:
Err.Clear
Application.EnableEvents = True

End Sub

And which locks the cells in range:

Dim xRg As Range
    
    On Error Resume Next
    
    Set xRg = Intersect(Range("A1:P1450"), Target)

    If xRg Is Nothing Then Exit Sub
    Target.Worksheet.Unprotect Password:="Epitomernok86"
    xRg.Locked = True
    Target.Worksheet.Protect Password:="Epitomernok86"

End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
See if this works.

P
Code:
rivate Sub Worksheet_Change(ByVal Target As Range)
Dim xRg As Range
On Error GoTo ErrHnd:
If Target.Column = 2 Then  
    Application.EnableEvents = False
    If Not IsDate(Range("C" & Target.Row).Value) Then
        Range("C" & Target.Row).Value = Format(Date, "yyyy/mm/dd")
    End If
    Application.EnableEvents = True    
End If
 On Error Resume Next    
    Set xRg = Intersect(Range("A1:P1450"), Target)
    If xRg Is Nothing Then Exit Sub
    Target.Worksheet.Unprotect Password:="Epitomernok86"
    xRg.Locked = True
    Target.Worksheet.Protect Password:="Epitomernok86"
ErrHnd:
If Err.Number > 0 Then
    On Error GoTo 0
    Err.Clear
End if   
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,318
Members
452,634
Latest member
cpostell

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top