Thread: remove button
View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein \(MVP - VB\)[_2574_] Rick Rothstein \(MVP - VB\)[_2574_] is offline
external usenet poster
 
Posts: 1
Default remove button

Ignore my posting about the button's Name and follow what JLGWhiz has told
you. I could swear when I tested the code I posted, the button got named and
I was able to use that name to delete it later on... I just know it worked
then, but I cannot no longer get to work any more, so obviously I was
mistaken.

However, what I posted about the nested With statements was accurate and you
can follow my instruction on that part of what I posted.

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
You can just name it right after the With Selection statement...

Worksheets("MIRO").Buttons.Add(240, 15, 80, 16).Select
With Selection
.Name = "NewButtonName"
.OnAction = "break"
.Characters.Text = "Break"
With .Characters(Start:=1, Length:=8).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.ColorIndex = xlAutomatic
End With
End With

I posted the whole routine because I wanted to show you another way to
handle your With statements. Instead of ending the first With statement in
order to start your next one, which simply a property of the object you
had in your first With statement, you can just nest the continued With
statement instead. Notice the dot in front of the Characters(...) property
call.... that just tells the second With statement to reference the first
With statement's object while inside the second With statement. When the
second (nested) With statement ends, the first With statement is back in
"control"... meaning and dotted properties you place after the inner With
statement's End With will reference the first With statement's object (in
this case, Selection). Once you get the hang of this, I think you will
find this a more convenient way to handle With statements.

Now, as for deleting the button (which I named NewButtonName in my above
example), just execute this code...

Worksheets("MIRO").Buttons("NewButtonName").Delete

substituting the name you give to the button when you create it.

Rick


"leungkong" wrote in message
...
I have used below code to add a button when workbook open.
I want to remove it when work close.
But the button name is "Button 1".
How can I change the name when I create the button? Thanks.

Worksheets("MIRO").Buttons.Add(240, 15, 80, 16).Select
With Selection
.OnAction = "break"
.Characters.Text = "Break"
End With
With Selection.Characters(Start:=1, Length:=8).Font
.Name = "Arial"
.FontStyle = "Regular"
.Size = 10
.ColorIndex = xlAutomatic
End With