I actually found my solution. It is as follows:
quote = chr(34)
text = "Range(" & quote & "B5" & quote & ").select"
call execute_code(text)
Sub execute_code(code_string)
'Create a new module in the current workbook, enter the code, run and remove the new module
On Error Resume Next
'Delete module in case it already exists
Dim VBComp As VBComponent
Set VBComp = ThisWorkbook.VBProject.VBComponents("NewModule")
ThisWorkbook.VBProject.VBComponents.Remove VBComp
On Error GoTo 0
Dim LineNum As Long
Set VBComp = ThisWorkbook.VBProject.VBComponents.Add(vbext_ct_StdModule)
VBComp.Name = "NewModule"
'Add the code lines
Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("NewModule").CodeModule
With VBCodeMod
LineNum = .CountOfLines + 1
.InsertLines LineNum, _
"Sub MyNewProcedure()" & Chr(13) & _
code_string & Chr(13) & _
"End Sub"
End With
'Run the new module
Application.Run "MyNewProcedure"
'Remove the new module
ThisWorkbook.VBProject.VBComponents.Remove VBComp
End Sub
Nora, thank you so much for your quick answers.
Bookmarks