![]() |
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 |
All times are GMT +1. The time now is 10:12 PM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com