Lock / Unlock Project using VBA

WaqasTariq

Board Regular
Joined
Jun 26, 2012
Messages
58
Office Version
  1. 365
I have a macro that creates activex buttons based on the selection of the user. I then add VBAA to the buttons so they can perform specific actions. Here is part of the code that does that;
Code:
' Adding buttons to the capture sheet
    ' Data collections for adding buttons
    Dim ButtonNumber1 As Integer
    ButtonName1 = Range(ColNameProcessSteps & (RowsUsedAtTop + 1)).Value
    ButtonLeft = 50
    ButtonTop = 100
    ButtonLine = 1
    ButtonNumber1 = 1
    
    For ForRange1 = (RowsUsedAtTop + 1) To ButtonIdLastRow1
        Sheets(DesignWorkSheet).Select
        ButtonIdentifier = Range(ColNameButtonIdentifier & ForRange1).Value
        Sheets(CaptureWorkSheet).Select
    ' Creating buttons
        Set obj = ActiveSheet.OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
        Link:=False, DisplayAsIcon:=False, Left:=ButtonLeft, Top:=ButtonTop, Width:=100, Height:=35)
        obj.Name = "InputButton" & ButtonNumber1
        ActiveSheet.OLEObjects("InputButton" & ButtonNumber1).Object.Caption = ButtonIdentifier & " " & ButtonName1 & " -(" & ButtonNumber1 & ")"
        ActiveSheet.OLEObjects("InputButton" & ButtonNumber1).Object.BackColor = DefaultColor1
        ActiveSheet.OLEObjects("InputButton" & ButtonNumber1).Object.WordWrap = True
        ButtonLeft = ButtonLeft + 125
        
        ' making sure only 5 end up on a single row
        If ButtonLeft = 675 Then
            If ButtonLine = 1 Then
                ButtonTop = 150
                ButtonLine = ButtonLine + 1
            ElseIf ButtonLine = 2 Then
                ButtonTop = 200
                ButtonLine = ButtonLine + 1
            ElseIf ButtonLine = 3 Then
                ButtonTop = 250
                ButtonLine = ButtonLine + 1
            ElseIf ButtonLine = 4 Then
                ButtonTop = 300
                ButtonLine = ButtonLine + 1
            End If
            ButtonLeft = 50
        End If
        
    ' Macro text
    ' Macro text addition code starts here
        Code = "Sub " & "InputButton" & ButtonNumber1 & "_Click()" & vbCrLf
        Code = Code & "   Application.ScreenUpdating = False" & vbCrLf
        Code = Code & "   MainWorkSheet = ActiveSheet.Name" & vbCrLf
        Code = Code & "   Sheets(""temp1"").Visible = True" & vbCrLf
        Code = Code & "   Sheets(""temp1"").Select" & vbCrLf
        Code = Code & "   ActiveSheet.Range(""B1"").Value = " & ButtonNumber1 & "' The Value Changes with button #" & vbCrLf
        Code = Code & "   Sheets(MainWorkSheet).Select" & vbCrLf
        Code = Code & "   Application.Run ""ActivateMainMacro""" & vbCrLf
        Code = Code & "End Sub"
        
    'add macro at the end of the sheet module
        With ActiveWorkbook.VBProject.VBComponents(Worksheets(CaptureWorkSheet).CodeName).CodeModule
            .insertlines .CountOfLines + 1, Code
        End With
        
        ButtonNumber1 = ButtonNumber1 + 1
    Next ForRange1

Now I want to protect the macro. If I add protection to the macro the 'Macro text addition code' above does not get added to the relevant sheet.

Is there a way to protect/unprotect project using VBA or a different way I can achieve this and keep my macro protected?
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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