Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Programatically adding code to a worksheet

I've written a macro to add some controls (i.e. buttons) to a
worksheet (not form).

2 questions

1. How do I add the code for the controls, like 'Private Sub
CommandButton1_Click()', to the worksheet that contains the control
programatically.

2. does this code have to be attached to the sheet that contains the
controls?

Thanks



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Programatically adding code to a worksheet

1.

Dim StartLine As Long

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

2. Yes

--
---
HTH

Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Flash in the Pan" wrote in message
...
I've written a macro to add some controls (i.e. buttons) to a
worksheet (not form).

2 questions

1. How do I add the code for the controls, like 'Private Sub
CommandButton1_Click()', to the worksheet that contains the control
programatically.

2. does this code have to be attached to the sheet that contains the
controls?

Thanks





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default Programatically adding code to a worksheet

Bob...

Tried what you wrote and changed "Sheet1" to a valid sheet in my
workbook.

When I ran it, received the following error.

Run-time error 57017
Event handler is invalid

Debug showed line containing Startline highlighted.


Any ideas?

Thanks



On May 19, 1:40*am, "Bob Phillips" wrote:
1.

Dim StartLine As Long

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

2. Yes

--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Flashin thePan" wrote in ...



I've written a macro to add some controls (i.e. buttons) to a
worksheet (not form).


2 questions


1. How do I add the code for the controls, like 'Private Sub
CommandButton1_Click()', to the worksheet that contains the control
programatically.


2. does this code have to be attached to the sheet that contains the
controls?


Thanks- Hide quoted text -


- Show quoted text -


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Programatically adding code to a worksheet




Hello Flash,

I had a similar issue. I needed to add code behind the sheet, but
using a macro.. Sort of using a macro to add another macro to the
excel workbook.
I used the following syntax...

With ActiveWorkbook.VBProject.VBComponents("Sheet1").Co deModule
.InsertLines .CountOfLines + 1, _
"Private Sub Worksheet_Calculate()" & vbCrLf & _

Continue to add in all your lines of code, but enter them between " "
and terminate the line using & vbCrLf & _
Please note that VBA will note take in more than 25 lines of code. If
you happened to exceed the specified limit, VBA will pop an error !!
If you need to add in more code, start again using the .InsertLines
syntax.

Hope this helps !!!
Sagar






On Jun 2, 4:58*pm, Flash in the Pan wrote:
Bob...

Tried what you wrote and changed "Sheet1" to a valid sheet in my
workbook.

When I ran it, received the following error.

Run-time error 57017
Event handler is invalid

Debug showed line containing Startline highlighted.

Any ideas?

Thanks

On May 19, 1:40*am, "Bob Phillips" wrote:



1.


Dim StartLine As Long


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


2. Yes


--
---
HTH


Bob


(there's no email, no snail mail, but somewhere should be gmail in my addy)


"Flashin thePan" wrote in ...


I've written a macro to add some controls (i.e. buttons) to a
worksheet (not form).


2 questions


1. How do I add the code for the controls, like 'Private Sub
CommandButton1_Click()', to the worksheet that contains the control
programatically.


2. does this code have to be attached to the sheet that contains the
controls?


Thanks- Hide quoted text -


- Show quoted text -- Hide quoted text -


- Show quoted text -


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
Adding VB code to worksheet programatically seegerp Excel Programming 2 March 29th 06 09:52 PM
Add code to a worksheet programatically Dave B[_9_] Excel Programming 2 December 9th 05 09:08 PM
Adding a Control programatically Richard Buttrey Excel Programming 9 July 5th 05 08:34 AM
Programatically saving workbook (or worksheet) without the code Hugh Excel Programming 4 October 22nd 04 12:03 AM
Excel crashing when (programatically) adding past the 360th worksheet to a workbook Scott Lyon Excel Programming 2 August 14th 03 12:34 PM


All times are GMT +1. The time now is 06:11 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"