Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Join or Combine Two Distinct WorkSheet VBA Codes as One

0

Hello all,

I need help with combining two worksheet VBA codes together as one.

The first code creates a timestamp and excel username when data is entered into the worksheet or updated.

The second code locks the cells immediately after data is entered in a cell in the same worksheet. I need those two codes two work as one.

I have attached a sample workbook with the new code supplied by a member here called Variatus who has been really helpful. 

Thanks as always.

Answer
Discuss

Answers

0

I'm afraid you are choosing the wrong approach here. This is Microsoft's fault, I guess, because they try to make everything ever easier until it's too complicated to grasp. The worksheet's Protect method in fact creates a Protection object. You can access the Protection object with Activesheet.Protection

That object has many properties which you can set. Their effect is to limit the scope of the protection. You can, for example, allow filtering or sorting, even deleting - there are more than ten properties. VBA's Intellisense will  show them all when you type "Activesheet.Protect". Google for "Protection object MSDN" for explanations.

The property you are interested in is called UserInterfaceOnly. It accepts the values True and False and has the effect of invalidating the protection for VBA, meaning VBA can do what you make it do while the user is bound by the Protection object's rules.

Excel wouldn't be a Microsoft product if it were that easy, however. There is a twist. The UserInterfaceOnly property is valid only for the session in which it is set, meaning, you can set the property, save your workbook, and next time you open it it's gone. That's irritating but considering that you propose to remove and reinstate the protection after each cell change it isn't so bad.

There is a little piece of Microsoft logic I actually like. It says that you don't need protection while the workbook is closed. Protecting a worksheet and then saving it makes no sense. That, in combination with the twist given to the UserInterfaceOnly property leads you to this code which must be installed in the ThisWorkbook code module.

Private Sub Workbook_Open()

    Dim Ws As Worksheet

    For Each Ws In ThisWorkbook.Worksheets
        With Ws
            ' if the sheet is included in the list then protect it
            If InStr("Sheet1/Sheet2/Sheet3", .Name) Then
                .Protect Password:="123", Contents:=True, UserInterfaceOnly:=True
            End If
        End With
    Next Ws
End Sub

You can use the IF statement with a list of sheets you want to protect or of those you wish to exclude (in that case Instr should = 0). The list might comprise a single sheet. You might tweak the Protection object's properties further. The effect is that your workbook can't be opened without the right kind of protection, and that invalidates your question to which I refer here.

Since your code now has full access to the project you don't need to lift and reinstate the protection to make a change. Just add one line of code to your other procedure.

Target.Locked = CBool(Len(Target))

I suggest to add this line near the bottom, just before Application.EnableEvents = True.

Discuss

Discussion

Thanks Variatus.

I did everything as you instructed. I added the code you supplied the "ThisWorkbook" code window. I also inserted 
Target.Locked = CBool(Len(Target))
to the worksheet code in sheet2 as instructed.

But it just didnt work like i wanted. i am able to record timestamp as excel user making changes to the worksheet (sheet2), but the cells dont lock after data is entered into them.
Kabasa007 (rep: 2) Mar 2, '20 at 5:53 am
Please post your workbook with the latest version of the code in it. To do so, edit your question and append a second workbook to it.
Variatus (rep: 4889) Mar 2, '20 at 6:41 am
I can't replicate your error. On my Excel 2013 all cells are locked after the code runs. That is the cell in which the change was made and both cells in which date and name were recorded. Moreover, the lock is effective, meaning I can't change what I entered. I tested cell Q3.
Variatus (rep: 4889) Mar 9, '20 at 9:49 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login