There are different ways to execute/run a code.
One way is to create a module. Start of the code could be "Sub Name". Now you can manually run the code/macro called Name.
Another way is to put code in worksheet. Start of code could then be "Private Sub Worksheet_Change(ByVal Target As Range)". The code will now run whenever a change is made to the sheet.
Change your posted part of code into:
If Range("A1").Value = 1 Then
Range("B1").Locked = False
Range("B1").Locked = True