View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Bob Phillips[_6_] Bob Phillips[_6_] is offline
external usenet poster
 
Posts: 11,272
Default programmatically create a macro

Here is some code that creates some Change event code

Dim StartLine As Long

With ActiveWorkbook.VBProject.VBComponents("Sheet1").Co deModule
StartLine = .CreateEventProc("Change", "Worksheet") + 1
.InsertLines StartLine, _
"Dim ans" & vbCrLf & _
" ans = Msgbox( ""All OK"",vbOYesNo)" & vbCrLf & _
" If ans = vbNo Then Cancel = True"
End With

It creates this code

Private Sub Worksheet_Change(ByVal Target As Range)
Dim ans
ans = MsgBox("All OK", vbOYesNo)
If ans = vbNo Then Cancel = True

End Sub

--

HTH

RP
(remove nothere from the email address if mailing direct)


"Jim Warren" wrote in message
...
I am dispersing large amounts of monthly data by creating and emailing
workbooks in a macro. In the largest of these workbooks I would like to
create a macro that would be triggered by clicking the mouse in various

cells
of one sheet. The macro itself is no problem, it's programmatically

getting
the macro and the event code into the new workbook that is the problem.

So
far I have not found a way to do this. Does anyone have any suggestions?