A
Aldo
Hi guys,
I am trying to use the following block of code to insert a function in a
worksheet.
If the VBA Project is not protected, the macro runs just fine, but when
locking the project for viewing, I get the following error:
"Run-time Error 50289 – Can't perform operation since the project is
protected"
How can I work it around?
Thanks in advance,
Aldo.
Function CreateProcedureForIndex(Optional wksName As String, Optional
eventName As String, Optional objectName As String)
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" 'one " character
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents(Sheets(wksName).CodeName)
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = LineNum + 1: .InsertLines LineNum, "Option Explicit"
LineNum = .CreateEventProc(eventName, objectName) ' + 1
LineNum = LineNum + 1: .InsertLines LineNum, " If
Cells(Target.row, Target.Column + 1).Value = ""Chart Sheet"" Then"
LineNum = LineNum + 1: .InsertLines LineNum, "
Charts(Target.Value).Activate"
LineNum = LineNum + 1: .InsertLines LineNum, " End if"
End With
End Function
I am trying to use the following block of code to insert a function in a
worksheet.
If the VBA Project is not protected, the macro runs just fine, but when
locking the project for viewing, I get the following error:
"Run-time Error 50289 – Can't perform operation since the project is
protected"
How can I work it around?
Thanks in advance,
Aldo.
Function CreateProcedureForIndex(Optional wksName As String, Optional
eventName As String, Optional objectName As String)
Dim VBProj As VBIDE.VBProject
Dim VBComp As VBIDE.VBComponent
Dim CodeMod As VBIDE.CodeModule
Dim LineNum As Long
Const DQUOTE = """" 'one " character
Set VBProj = ActiveWorkbook.VBProject
Set VBComp = VBProj.VBComponents(Sheets(wksName).CodeName)
Set CodeMod = VBComp.CodeModule
With CodeMod
LineNum = LineNum + 1: .InsertLines LineNum, "Option Explicit"
LineNum = .CreateEventProc(eventName, objectName) ' + 1
LineNum = LineNum + 1: .InsertLines LineNum, " If
Cells(Target.row, Target.Column + 1).Value = ""Chart Sheet"" Then"
LineNum = LineNum + 1: .InsertLines LineNum, "
Charts(Target.Value).Activate"
LineNum = LineNum + 1: .InsertLines LineNum, " End if"
End With
End Function