#1   Report Post  
Posted to microsoft.public.excel.misc
John Vickers
 
Posts: n/a
Default Don't open vb editor


I am using a macro to write a macro in the ThisWorkbook Module, but when
it runs it opens the vb editor. I don't want this to happen, so does
anyone know a way to stop this from happening?

Here is my current code:


Code:
--------------------
Sub test()

If ProcedureExists("Workbook_SheetChange", "ThisWorkbook") Then
DeleteProcedure ("Workbook_SheetChange")
End If

Dim StartLine As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkboo k").CodeModule
StartLine = .CreateEventProc("SheetChange", "Workbook") + 1
.InsertLines StartLine + 1, _
"If Target = Sheet1.Range(""A1"") Then" + vbCrLf _
+ "Sheet2.Range(""A1"") = Target.Value" + vbCrLf _
+ "ElseIf Target = Sheet2.Range(""A1"") Then" + vbCrLf _
+ "Sheet1.Range(""A1"") = Target.Value" + vbCrLf + _
"End If" + vbCrLf
End With

End Sub

Function ProcedureExists(ProcedureName As String, _
ModuleName As String) As Boolean
On Error Resume Next
If ModuleExists(ModuleName) = True Then
ProcedureExists = ThisWorkbook.VBProject.VBComponents(ModuleName) _
.CodeModule.ProcStartLine(ProcedureName, vbext_pk_Proc) < 0
End If
End Function

Function ModuleExists(ModuleName As String) As Boolean
On Error Resume Next
ModuleExists = Len(ThisWorkbook.VBProject.VBComponents(ModuleName ).Name) < 0
End Function

Sub DeleteProcedure(ProcedureName As String)

Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("ThisWorkbook" ).CodeModule
With VBCodeMod
StartLine = .ProcStartLine(ProcedureName, vbext_pk_Proc)
HowManyLines = .ProcCountLines(ProcedureName, vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines
End With

End Sub
--------------------



John Vickers


--
John Vickers
------------------------------------------------------------------------
John Vickers's Profile: http://www.excelforum.com/member.php...o&userid=31551
View this thread: http://www.excelforum.com/showthread...hreadid=513276

  #2   Report Post  
Posted to microsoft.public.excel.misc
exceluserforeman
 
Posts: n/a
Default Don't open vb editor

Vvery clever code, I have to spend the next ten years at university to
decipher it before I can respond.

However, you have a dim statement mid way in a sub routine. VB does not
approve of this. Put your Dim statement at the top. Dim StartLine As Long

If you could tell "us" what it is suppose to then maybe "we" could provide
alternative code.


http://www.geocities.com/excelmarksway




"John Vickers" wrote:


I am using a macro to write a macro in the ThisWorkbook Module, but when
it runs it opens the vb editor. I don't want this to happen, so does
anyone know a way to stop this from happening?

Here is my current code:


Code:
--------------------
Sub test()

If ProcedureExists("Workbook_SheetChange", "ThisWorkbook") Then
DeleteProcedure ("Workbook_SheetChange")
End If

Dim StartLine As Long
With ActiveWorkbook.VBProject.VBComponents("ThisWorkboo k").CodeModule
StartLine = .CreateEventProc("SheetChange", "Workbook") + 1
.InsertLines StartLine + 1, _
"If Target = Sheet1.Range(""A1"") Then" + vbCrLf _
+ "Sheet2.Range(""A1"") = Target.Value" + vbCrLf _
+ "ElseIf Target = Sheet2.Range(""A1"") Then" + vbCrLf _
+ "Sheet1.Range(""A1"") = Target.Value" + vbCrLf + _
"End If" + vbCrLf
End With

End Sub

Function ProcedureExists(ProcedureName As String, _
ModuleName As String) As Boolean
On Error Resume Next
If ModuleExists(ModuleName) = True Then
ProcedureExists = ThisWorkbook.VBProject.VBComponents(ModuleName) _
.CodeModule.ProcStartLine(ProcedureName, vbext_pk_Proc) < 0
End If
End Function

Function ModuleExists(ModuleName As String) As Boolean
On Error Resume Next
ModuleExists = Len(ThisWorkbook.VBProject.VBComponents(ModuleName ).Name) < 0
End Function

Sub DeleteProcedure(ProcedureName As String)

Dim VBCodeMod As CodeModule
Dim StartLine As Long
Dim HowManyLines As Long

Set VBCodeMod = ThisWorkbook.VBProject.VBComponents("ThisWorkbook" ).CodeModule
With VBCodeMod
StartLine = .ProcStartLine(ProcedureName, vbext_pk_Proc)
HowManyLines = .ProcCountLines(ProcedureName, vbext_pk_Proc)
.DeleteLines StartLine, HowManyLines
End With

End Sub
--------------------



John Vickers


--
John Vickers
------------------------------------------------------------------------
John Vickers's Profile: http://www.excelforum.com/member.php...o&userid=31551
View this thread: http://www.excelforum.com/showthread...hreadid=513276


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
CSV formatted files open odly in Excel 2000 Janski Excel Discussion (Misc queries) 3 November 24th 05 07:58 PM
i try to open Excel spreadsheets, and they open in wordpad-- mikekeat Excel Discussion (Misc queries) 2 October 1st 05 10:13 PM
cannot open an xl97 file maxzsim Excel Discussion (Misc queries) 1 June 24th 05 12:29 PM
How do I find a file/spreadsheet that Excel says is Already open but I can't find it? nwtrader8 Excel Discussion (Misc queries) 5 June 21st 05 02:16 PM
When I open excel document it always ask if I want to open it in . dperkins Excel Discussion (Misc queries) 0 February 15th 05 08:11 PM


All times are GMT +1. The time now is 08:35 AM.

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

About Us

"It's about Microsoft Excel"