WaqasTariq
Board Regular
- Joined
- Jun 26, 2012
- Messages
- 58
- Office Version
- 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;
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?
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?