Error Message When running code but not stepping through it.

G

Guest

I'm attempting to use VBA to add a Command Button to a Worksheet. When I step
through the code (using F8) it works fine. However, when I simply try to run
it, Excel crashes and I get the error:

The instruction at '0x650e7624' referenced memory at '0x053c831c'. The
memory could not be read.

Any idea what is going on?
 
R

Ron de Bruin

Hi Jared

If you are using Excel 97 then try this

Try to Change the takefocusonclick property to false of the button
This is a bug in Excel 97
 
R

Ron de Bruin

I don't see your code but this is working for me for Sheet1

Sub test()
Dim WS As Worksheet
Dim Btn As OLEObject
Set WS = ThisWorkbook.Worksheets("Sheet1")
With WS
Set Btn = .OLEObjects.Add(ClassType:="Forms.CommandButton.1", _
Left:=.Range("C3").Left, Top:=.Range("C3").Top, _
Width:=100, Height:=30)
End With
Btn.Object.Caption = "Click Me"
Btn.Name = "YourButton"
With ThisWorkbook.VBProject.VBComponents(WS.CodeName).CodeModule
.InsertLines .CreateEventProc("Click", Btn.Name) + 1, _
vbTab & "If Range(""A1"").Value > 0 Then " & vbCrLf & _
vbTab & vbTab & "Msgbox ""Hi""" & vbCrLf & _
vbTab & "End If"
End With
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top