LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default why does userform close after programming worksheet event with CreateEventProc or AddFromString

Thanks to everyone who replied here and sent emails. I finally got it
all figured out and just wanted to post the result to help others who
might have a similar problem. First the code, then the explanation.

THE CODE:
Public Sub DoStuff()
Dim VBProj As VBProject
Dim VBComp As VBComponent
Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim testCodeString As String

Application.ScreenUpdating = False

''add a new (temporary) workbook
Workbooks.Add

''rename the activesheet per the user's textbox input value
ActiveSheet.Name = UserForm1.TextBox1.Value

''write event for the new worksheet
Set VBProj = ActiveWorkbook.VBProject

For Each VBComp In VBProj.VBComponents
If VBComp.Type = vbext_ct_Document Then
If (VBComp.Properties("Name").Value = ActiveSheet.Name)
Then
Set VBCodeMod = VBComp.CodeModule


''--------------------------------------------------------
'' METHOD 2: Use .AddFromString

''--------------------------------------------------------
testCodeString = _
"Public Sub Worksheet_Calculate()" & vbCr & _
" Dim prompt As String" & vbCr & vbCr & _
" ''test line - comment 1" & vbCr & _
" prompt = ""RUNNING: Worksheet_Calculate""" &
vbCr & _
" MsgBox prompt" & vbCr & _
"End Sub"

VBCodeMod.AddFromString testCodeString
''--END METHOD 2
CODE-------------------------------------

End If
End If
Next

''move the new sheet with event code to the other workbook
Sheets(1).Move Befo=Workbooks("TestWorkbook.xls").Sheets(1)

''activate and close/delete the previously added (temporary)
workbook
Workbooks(Workbooks.Count).Activate
ActiveWorkbook.Close (False)

Application.ScreenUpdating = True
End Sub

THE EXPLANATION:
I got this idea from another post where the post-er mentioned being
able to write code to a worksheet in a different workbook, but not the
workbook running the userform. So, I decided to try creating a
temporary workbook, writing the Worksheet event code in a sheet in the
new workbook and then just moving the worksheet to the old workbook.
This worked...Now, the original workbook contains a new sheet (named by
the user), the new sheet contains the appropriate event code, and the
userform stays open - ready for another user input (worksheet name).

If anyone would like to chat about this further, please email me since
i'll probably stop watching this thread.

Happy programming...
Troy


 
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
Close UserForm despite Exit Event Carim[_3_] Excel Programming 4 May 17th 05 05:39 AM
Worksheet close event++? has ex Excel Programming 1 April 13th 05 12:05 AM
Worksheet close event? has ex Excel Programming 4 April 12th 05 11:43 PM
Event procedures for controls added with CreateEventProc John Austin[_6_] Excel Programming 7 March 24th 05 03:21 PM
Event procedures for controls added with CreateEventProc John Austin[_5_] Excel Programming 0 March 12th 05 09:33 PM


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