LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Problems with VBE - Please Help

Hello,
I have the procedure below in my program which adds a
button with click event code to a specified sheet. This
code works perfectly when I call it once for a sheet or
back-to-back for different sheets. However, if I call it
twice for the same sheet (try to put two buttons with code
on the same sheet), it fails with this error:

EXCEL.EXE Application Error
The instruction at "0x..." referenced memory at "0x...".
The memory could not be "read".

I believe the problem is that the code module in the VBE
stays active (or something to that effect) and the second
button's event code cannot be written there. I have no
idea how to "close" or "deactivate" this so I can write to
the same code module. Can anyone please help me?? If I
take out the code that -er- writes the code, it works fine
even with two buttons on the same page. Any help would be
GREATLY appreciated. This is driving me nuts!

TIA,
Joe (please reply to group)

Sub CreateButton(strSheetName As String, strButtonName As
String, strCaption As String, lngWidth As Long, lngRow As
Long, lngCol As Long)

'variable declarations
Dim lngLineNum As Long
Dim cmd As OLEObject

'create the button with required characteristics
Set cmd = ActiveWorkbook.Sheet(strSheetName).OLEObjects.Add
(ClassType:="Forms.CommandButton.1", _
Link:=False, DisplayAsIcon:=False, Left:=241.5, Top:=37.5,
Width:=lngWidth, Height:=22.5)

With cmd
.Name = strButtonName
.Left = ActiveWorkbook.Sheets(strSheetName).Cells
(lngRow, lngCol).Left
.Top = ActiveWorkbook.Sheets(strSheetName).Cells
(lngRow, lngCol).Top
End With

With cmd.Object
.Caption = strCaption
.Font.Bold = True
.Font.Size = 8
End With

With ActiveWorkbook.VBProject.VBComponents
(ActiveWorkbook.Sheets(strSheetName).CodeName).Cod eModule
lngLineNum = .CreateEventProc("Click", strButtonName) +
1
.InsertLines lngLineNum, _
"MsgBox " & Chr(34) & "ok" & Chr(34)
End With

End Sub
 
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
aauugghhh...#div/o problems & various average formula problems acbel40 Excel Worksheet Functions 5 October 19th 09 05:00 PM
Still having VBA problems Preschool Mike Excel Discussion (Misc queries) 1 August 23rd 09 03:06 PM
Fix Your Problems!!! Derrick Excel Discussion (Misc queries) 4 July 31st 09 08:06 PM
OLE Problems martins Excel Worksheet Functions 0 August 6th 06 03:56 PM
Tab Key problems ESL in Tyler Excel Discussion (Misc queries) 2 March 21st 05 11:01 PM


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