View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default getting Runtime Error 1004

I'd drop the select's:

Option Explicit

Sub CreateButton()
Dim btnMyButton As Button

Set btnMyButton = ActiveSheet.Buttons.Add(460, 75, 140, 30)

btnMyButton.Caption = "Delete and Update Charts and Lists"
btnMyButton.OnAction = "btnDeleteAndUpdateSeatingChart"
btnMyButton.Name = "btnDeleteAndUpdate"
End Sub

In fact, I'd be more specific with the .onaction portion.

btnMyButton.OnAction _
= "'" & ActiveSheet.Parent.Name & "'!btnDeleteAndUpdateSeatingChart"



ArielZusya wrote:

I've got the following code:

Sub CreateButton()
Dim btnMyButton As Button

Set btnMyButton = ActiveSheet.Buttons.Add(460, 75, 140, 30)

btnMyButton.Caption = "Delete and Update Charts and Lists"
btnMyButton.Select
Selection.OnAction = "btnDeleteAndUpdateSeatingChart"
Selection.Name = "btnDeleteAndUpdate"
End Sub

If I create a new workboox and then create a module with this sub in it and
then run it it works without error. However, when I run it from a second
workbook I get the error. In other words if I use the current workbook to
run vba script that copys the current worksheet to a new workbook and then
while that new workbook is active I run the above code it creates the button
on the new worksheet but when it tries to change the caption on the button
from "Button 1" to "Delete and Update Charts and Lists" I get Runtime Error
1004: Unable to update the Caption Property of the Button Class. Anyone have
any idea why this would be? I've tried saving the new workbook before
running the create button script and it had no impact on the problem. This
is in Excel 2007. Your help is greatly appreciated. Thanks!


--

Dave Peterson