Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
mark
 
Posts: n/a
Default running code

I have the following code in my workbook, how can I use a
command button to run this.
Private Sub Workbook_Open()
Dim wb As Workbook
'copy all sheets
Worksheets.Copy
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
'delete the sheets you want
wb.Sheets(Array("Suspense", "RCA exc RIM", "Operations
summary", "RCA incl RIM", "First Qtr", "Second
Qtr", "Third Qtr", "Fourth Qtr")).Delete
Application.DisplayAlerts = True

End Sub


any help please

Mark
  #2   Report Post  
Nick Hodge
 
Posts: n/a
Default

Mark

Put it in a standard code module and assign the macro to a command button by
selecting one from the forms toolbar, right clicking and selecting 'Assign
macro'. The way you have it set up currently it is in the 'ThisWorkbook'
class module and will run by the Open event (Each time the workbook is first
opened)

For explanation of the different modules, check here

http://www.nickhodge.co.uk/vba/vbaimplement.htm

--
HTH
Nick Hodge
Microsoft MVP - Excel
Southampton, England
HIS


"mark" wrote in message
...
I have the following code in my workbook, how can I use a
command button to run this.
Private Sub Workbook_Open()
Dim wb As Workbook
'copy all sheets
Worksheets.Copy
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
'delete the sheets you want
wb.Sheets(Array("Suspense", "RCA exc RIM", "Operations
summary", "RCA incl RIM", "First Qtr", "Second
Qtr", "Third Qtr", "Fourth Qtr")).Delete
Application.DisplayAlerts = True

End Sub


any help please

Mark



  #3   Report Post  
JulieD
 
Posts: n/a
Default

Hi

if you have it under Workbook_Open in the ThisWorkbook module the code will
run when you open the workbook - no button needed (ensure that you have your
security settings set to medium - tools / macros / security)

if you want to run it via a button, cut the code from here and keep it in
memory

switch back to the excel worksheet - display the control toolbox toolbar
(view / toolbars), click on the command button, click on your worksheet
where you want it, right mouse click on the command button and choose view
code - now paste your code in there excluding the Private Sub and End Sub
lines

then switch back to the excel worksheet - click on the exit design mode icon
on the control toolbox toolbar and test out your button.

Cheers
JulieD

"mark" wrote in message
...
I have the following code in my workbook, how can I use a
command button to run this.
Private Sub Workbook_Open()
Dim wb As Workbook
'copy all sheets
Worksheets.Copy
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
'delete the sheets you want
wb.Sheets(Array("Suspense", "RCA exc RIM", "Operations
summary", "RCA incl RIM", "First Qtr", "Second
Qtr", "Third Qtr", "Fourth Qtr")).Delete
Application.DisplayAlerts = True

End Sub


any help please

Mark



  #4   Report Post  
mark
 
Posts: n/a
Default

Thanks this works a treat.
soz about the duplicate posting.
-----Original Message-----
Hi

if you have it under Workbook_Open in the ThisWorkbook

module the code will
run when you open the workbook - no button needed

(ensure that you have your
security settings set to medium - tools / macros /

security)

if you want to run it via a button, cut the code from

here and keep it in
memory

switch back to the excel worksheet - display the control

toolbox toolbar
(view / toolbars), click on the command button, click on

your worksheet
where you want it, right mouse click on the command

button and choose view
code - now paste your code in there excluding the

Private Sub and End Sub
lines

then switch back to the excel worksheet - click on the

exit design mode icon
on the control toolbox toolbar and test out your button.

Cheers
JulieD

"mark" wrote in

message
...
I have the following code in my workbook, how can I use

a
command button to run this.
Private Sub Workbook_Open()
Dim wb As Workbook
'copy all sheets
Worksheets.Copy
Set wb = ActiveWorkbook
Application.DisplayAlerts = False
'delete the sheets you want
wb.Sheets(Array("Suspense", "RCA exc RIM", "Operations
summary", "RCA incl RIM", "First Qtr", "Second
Qtr", "Third Qtr", "Fourth Qtr")).Delete
Application.DisplayAlerts = True

End Sub


any help please

Mark



.

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
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 7 March 7th 05 06:29 PM
Opening a file with code without a set file name jenkinspat Excel Discussion (Misc queries) 1 March 4th 05 10:50 AM
Opening a file with code without a set file name jenkinspat Excel Discussion (Misc queries) 1 March 3rd 05 03:40 PM
Error trapped only while stepping through the code - Not triggered when run Jeff Excel Discussion (Misc queries) 0 February 28th 05 06:26 PM
Command Button VBA code Dave Peterson Excel Discussion (Misc queries) 2 January 25th 05 11:28 PM


All times are GMT +1. The time now is 03:12 PM.

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

About Us

"It's about Microsoft Excel"