View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Can You Change A CommandButton Code Using A Macro?

On Chip's page see

Deleting A Procedure From A Module

and

Creating An Event Procedure

An alternative might be to have a public variable that contains the name of
the sheet and you can set it in your code

In the top of the code module for the sheet you will copy, put in the
declaration (outside any procedure)

Public ShName as String

then in the commandbutton click event


Private Sub CommandButton1_Click()
Worksheets(shname).Activate

End Sub

Now when you copy the sheet you can use code like this

Sub CopySheets()
Worksheets("Sheet4").Copy After:=Worksheets _
(Worksheets.Count)
ActiveSheet.Name = "EFGH"
Worksheets("EFGH").shname = "Sheet1"
End Sub

Of course, you would have to initialize ShName in the code module of the
master sheet (possibly in the workbook open event) at some point.


--
Regards,
Tom Ogilvy


wrote in message
ups.com...
Thanks Tom, Your code gave me an idea to shorten/eliminate the need to
change range B to E which is great but I still need to change some of
the CommandButton Code and I am not fully understanding your code. My
code so far is:-

Private Sub ABC_Click()
d = ActiveSheet.Name
Sheets("ABC").Select
Sheets(d).Visible = False
End Sub

Basically I have a button that activates a sheet and hiding the first
one. This code and button is part of a Master Sheet. I have a main
macro that copies this sheet and renames it. I now want to change the
"ABC" to another sheet name. I have this sheet name in a string (Sht)
(from the main macro) and want to use it to change the code of the
CommandButton.

I don't know if your code can help me do this..if so could you explain
how this is possible.

Thanking you again for your help.