Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default programmatically create a macro

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?
  #2   Report Post  
Posted to microsoft.public.excel.programming
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?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default programmatically create a macro

Bob: Thanks for the help. Although, it didn't work. After the first crash,
I single stepped through the code and it works until it comes to the
..InsertLines line. I got a runtime error "The object invoked has
disconnected from its client." The only thing I could find at Microsoft was
an article that said all globals are reset when using .CreateEventProc. I
guess that would reset StartLine, but I don't know. If you could give me some
more ideas I would appreciate it. By the way, could you recommend a good
reference source for some of the more obscure commands in VBA.

"Bob Phillips" wrote:

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?




Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Create a two-line header programmatically Jeanne Conroy Excel Programming 1 November 24th 04 03:37 PM
Programmatically create combo boxes Richard[_31_] Excel Programming 1 August 12th 04 03:10 AM
how to create a user form programmatically Alberto Piazza Excel Programming 0 November 13th 03 01:20 AM
help with create spreadsheet programmatically Divyesh Raithatha Excel Programming 1 October 4th 03 08:02 AM
How to create picklists programmatically Harald Staff[_4_] Excel Programming 0 July 16th 03 09:59 AM


All times are GMT +1. The time now is 02:40 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"