Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Programatically Delete command button and code.

I have a command button on a worksheet which has code associated with the
worksheet. How to delete the control and code from another macro? The reason
is this control is on a worksheet that is copied to another workbook. Once
the copy is done, I want to delete the control and code so the target
worksheet won't display the Security Warning about macros when it is opened.

I've seen some hints at it in other posts on how to delete code and
controls, but can't quite translate them to my situation.

Any help is appreciated.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 772
Default Programatically Delete command button and code.

Put all of your code in the ThisWorkbook area and delete control like this,
when you copy the sheet the code placed in ThisWorkbook won't go with

Sheets("sheet1").Shapes("CommandButton1").Select
Selection.Delete
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"DocBrown" wrote:

I have a command button on a worksheet which has code associated with the
worksheet. How to delete the control and code from another macro? The reason
is this control is on a worksheet that is copied to another workbook. Once
the copy is done, I want to delete the control and code so the target
worksheet won't display the Security Warning about macros when it is opened.

I've seen some hints at it in other posts on how to delete code and
controls, but can't quite translate them to my situation.

Any help is appreciated.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Programatically Delete command button and code.

Thanks for the quick reply. :-)

This works great to delete the control on the worksheet, but I still need to
delete the VBA code in the Sheet module.

Thanks,
John S.

"John Bundy" wrote:

Put all of your code in the ThisWorkbook area and delete control like this,
when you copy the sheet the code placed in ThisWorkbook won't go with

Sheets("sheet1").Shapes("CommandButton1").Select
Selection.Delete
--
-John Northwest11
Please rate when your question is answered to help us and others know what
is helpful.


"DocBrown" wrote:

I have a command button on a worksheet which has code associated with the
worksheet. How to delete the control and code from another macro? The reason
is this control is on a worksheet that is copied to another workbook. Once
the copy is done, I want to delete the control and code so the target
worksheet won't display the Security Warning about macros when it is opened.

I've seen some hints at it in other posts on how to delete code and
controls, but can't quite translate them to my situation.

Any help is appreciated.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7,247
Default Programatically Delete command button and code.

Try the following code. You MUST have "Trust access to the Visual Basic
Project" checked in the Trusted Source tab of the Macro Security dialog.

Dim CommandButtonName As String
Dim StartLine As Long
Dim CountLines As Long
Dim CodeMod As Object
Dim WS As Worksheet
CommandButtonName = "CommandButton1" '<<< CHANGE AS REQUIRED
Set WS = Worksheets("Sheet1") '<<< CHANGE AS REQUIRED
With WS
.OLEObjects(CommandButtonName).Delete
Set CodeMod = ThisWorkbook.VBProject.VBComponents(.CodeName).Cod eModule
End With
'<<<<<
With CodeMod
StartLine = .ProcStartLine(procname:=CommandButtonName & "_Click",
prockind:=0)
CountLines = .ProcCountLines(procname:=CommandButtonName & "_Click",
prockind:=0)
.DeleteLines StartLine:=StartLine, Count:=CountLines
End With
'<<<<<<

If you want to delete ALL the code in the module, not just the button's
Click event, replace the lines above between the <<<< markers with the
following:

With CodeMod
.DeleteLines StartLine:=1, Count:=.CountOfLines
End With


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
(email address is on the web site)



"DocBrown" wrote in message
...
I have a command button on a worksheet which has code associated with the
worksheet. How to delete the control and code from another macro? The
reason
is this control is on a worksheet that is copied to another workbook. Once
the copy is done, I want to delete the control and code so the target
worksheet won't display the Security Warning about macros when it is
opened.

I've seen some hints at it in other posts on how to delete code and
controls, but can't quite translate them to my situation.

Any help is appreciated.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Add a Button Programatically plus code jlclyde Excel Discussion (Misc queries) 1 December 9th 08 04:29 PM
Delete Code Modules Programatically blatham Excel Discussion (Misc queries) 1 February 3rd 06 05:38 PM
Delete command button ? SpookiePower Excel Programming 3 January 23rd 06 02:46 PM
Delete with command button? John[_100_] Excel Programming 3 March 17th 05 01:38 PM
Delete command button BrianB Excel Programming 0 October 18th 04 10:28 AM


All times are GMT +1. The time now is 08:54 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"