Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Insert code into worksheets programatically??

Hi.

I have the following procedure that runs after a bunch of worksheets are
added to a workbook. When the user selects a particular cell on one of the
added worksheets the program evaluates whether a userform should be displayed
to prompt to some information.

The problem I have is that while this procedure runs fine on my machine,
when I distribute this to other users, the code is not added. Is there
something I am missing? As far as I can see, all the references are correct
(though I am not positive on this).

Sub Test

Set dBook = ActiveWorkbook

On Error Resume Next
Application.EnableEvents = False
Application.DisplayAlerts = False

For Each vbc In dBook.VBProject.VBComponents
If Left(vbc.Name, 5) = "Sheet" Then
If vbc.CodeModule.CountOfLines = 0 Then
vbc.CodeModule.InsertLines 1, "Private Sub
Worksheet_SelectionChange(ByVal Target As Range)"
vbc.CodeModule.InsertLines 2, "Dim rng as Range"
vbc.CodeModule.InsertLines 5, " Set rng = Target"
vbc.CodeModule.InsertLines 6, " Call CheckTarget(rng)
"
vbc.CodeModule.InsertLines 10, "End Sub"
End If
End If
Next vbc

Application.EnableEvents = True
Application.DisplayAlerts = True

On Error GoTo 0

End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Insert code into worksheets programatically??

I have tools|options|editor tab|require variable declaration checked (inside the
VBE).

When your code runs, it looks for 0 lines in those modules. xl2003 seems to add
"option explicit" to the module before your code runs.

And a second guess, any chance that the activeworkbook's project is protected?

Caro-Kann Defence wrote:

Hi.

I have the following procedure that runs after a bunch of worksheets are
added to a workbook. When the user selects a particular cell on one of the
added worksheets the program evaluates whether a userform should be displayed
to prompt to some information.

The problem I have is that while this procedure runs fine on my machine,
when I distribute this to other users, the code is not added. Is there
something I am missing? As far as I can see, all the references are correct
(though I am not positive on this).

Sub Test

Set dBook = ActiveWorkbook

On Error Resume Next
Application.EnableEvents = False
Application.DisplayAlerts = False

For Each vbc In dBook.VBProject.VBComponents
If Left(vbc.Name, 5) = "Sheet" Then
If vbc.CodeModule.CountOfLines = 0 Then
vbc.CodeModule.InsertLines 1, "Private Sub
Worksheet_SelectionChange(ByVal Target As Range)"
vbc.CodeModule.InsertLines 2, "Dim rng as Range"
vbc.CodeModule.InsertLines 5, " Set rng = Target"
vbc.CodeModule.InsertLines 6, " Call CheckTarget(rng)
"
vbc.CodeModule.InsertLines 10, "End Sub"
End If
End If
Next vbc

Application.EnableEvents = True
Application.DisplayAlerts = True

On Error GoTo 0

End Sub


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 12
Default Insert code into worksheets programatically??

Hi Dave.

Thanks for the response. I never considered Excel versions having that kind
of impact. I am using xl2000 and the people I am distributing to are using
xl2003. I need to delve further into that.

I do require variable declaration as well. I Dim'ed the variables outside of
the procedures.

The application is not protected (at least not yet!).

I've tried changing the code first count the number of lines already in the
module and then adding the code below on the next available line.

"Dave Peterson" wrote:

I have tools|options|editor tab|require variable declaration checked (inside the
VBE).

When your code runs, it looks for 0 lines in those modules. xl2003 seems to add
"option explicit" to the module before your code runs.

And a second guess, any chance that the activeworkbook's project is protected?

Caro-Kann Defence wrote:

Hi.

I have the following procedure that runs after a bunch of worksheets are
added to a workbook. When the user selects a particular cell on one of the
added worksheets the program evaluates whether a userform should be displayed
to prompt to some information.

The problem I have is that while this procedure runs fine on my machine,
when I distribute this to other users, the code is not added. Is there
something I am missing? As far as I can see, all the references are correct
(though I am not positive on this).

Sub Test

Set dBook = ActiveWorkbook

On Error Resume Next
Application.EnableEvents = False
Application.DisplayAlerts = False

For Each vbc In dBook.VBProject.VBComponents
If Left(vbc.Name, 5) = "Sheet" Then
If vbc.CodeModule.CountOfLines = 0 Then
vbc.CodeModule.InsertLines 1, "Private Sub
Worksheet_SelectionChange(ByVal Target As Range)"
vbc.CodeModule.InsertLines 2, "Dim rng as Range"
vbc.CodeModule.InsertLines 5, " Set rng = Target"
vbc.CodeModule.InsertLines 6, " Call CheckTarget(rng)
"
vbc.CodeModule.InsertLines 10, "End Sub"
End If
End If
Next vbc

Application.EnableEvents = True
Application.DisplayAlerts = True

On Error GoTo 0

End Sub


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default Insert code into worksheets programatically??

The application is not protected (at least not yet!).
If you lock the code in your project, you won't be inserting code lines in
it.

Perhaps you would need to create a dummy workbook, create the sheets there,
add the code, then copy them into your workbook and delete the dummy - I
don't know if that works with a locked project or not as I haven't tried it.

--
Regards,
Tom Ogilvy



"Caro-Kann Defence" wrote in
message ...
Hi Dave.

Thanks for the response. I never considered Excel versions having that

kind
of impact. I am using xl2000 and the people I am distributing to are using
xl2003. I need to delve further into that.

I do require variable declaration as well. I Dim'ed the variables outside

of
the procedures.

The application is not protected (at least not yet!).

I've tried changing the code first count the number of lines already in

the
module and then adding the code below on the next available line.

"Dave Peterson" wrote:

I have tools|options|editor tab|require variable declaration checked

(inside the
VBE).

When your code runs, it looks for 0 lines in those modules. xl2003

seems to add
"option explicit" to the module before your code runs.

And a second guess, any chance that the activeworkbook's project is

protected?

Caro-Kann Defence wrote:

Hi.

I have the following procedure that runs after a bunch of worksheets

are
added to a workbook. When the user selects a particular cell on one of

the
added worksheets the program evaluates whether a userform should be

displayed
to prompt to some information.

The problem I have is that while this procedure runs fine on my

machine,
when I distribute this to other users, the code is not added. Is there
something I am missing? As far as I can see, all the references are

correct
(though I am not positive on this).

Sub Test

Set dBook = ActiveWorkbook

On Error Resume Next
Application.EnableEvents = False
Application.DisplayAlerts = False

For Each vbc In dBook.VBProject.VBComponents
If Left(vbc.Name, 5) = "Sheet" Then
If vbc.CodeModule.CountOfLines = 0 Then
vbc.CodeModule.InsertLines 1, "Private Sub
Worksheet_SelectionChange(ByVal Target As Range)"
vbc.CodeModule.InsertLines 2, "Dim rng as Range"
vbc.CodeModule.InsertLines 5, " Set rng = Target"
vbc.CodeModule.InsertLines 6, " Call

CheckTarget(rng)
"
vbc.CodeModule.InsertLines 10, "End Sub"
End If
End If
Next vbc

Application.EnableEvents = True
Application.DisplayAlerts = True

On Error GoTo 0

End Sub


--

Dave Peterson



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
Add a Button Programatically plus code jlclyde Excel Discussion (Misc queries) 1 December 9th 08 04:29 PM
Delete Code Modules Programatically blatham Excel Discussion (Misc queries) 1 February 3rd 06 05:38 PM
Programatically saving workbook (or worksheet) without the code Hugh Excel Programming 4 October 22nd 04 12:03 AM
Deleting macro code programatically Terry Lowe[_3_] Excel Programming 1 September 1st 04 02:30 AM
Removing VBA Code (programatically) Marnie R Excel Programming 1 February 13th 04 01:02 AM


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