Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding macro code to a new worksheet

Hi,

I'm creating a workbook that will has an macro on sheet 1 which
creates a new worksheet with a table for tracking jobs.

What I need to do now is add some code to each new worksheet as part
of the macro on sheet 1.

This is where I have no idea what to do. The code on each worksheet is
basically code to run some batch files when a certain cell is
clicked.

What is the best way to do this?

Thanks

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 22,906
Default Adding macro code to a new worksheet

One method.

Create a single sheet workbook and save as SHEET.xlt template with the code and
table as you like.

Store it in your XLSTART folder and insert it with this line in your Sheet1
macro code.

Sheets.Add(Type:="Worksheet")

Which will use your SHEET.xlt template as the "added" sheet.


Gord Dibben MS Excel MVP


On 4 Mar 2007 17:37:54 -0800, wrote:

Hi,

I'm creating a workbook that will has an macro on sheet 1 which
creates a new worksheet with a table for tracking jobs.

What I need to do now is add some code to each new worksheet as part
of the macro on sheet 1.

This is where I have no idea what to do. The code on each worksheet is
basically code to run some batch files when a certain cell is
clicked.

What is the best way to do this?

Thanks


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 857
Default Adding macro code to a new worksheet

There are ways to access and modify the VBA code module of an object in the
workbook. But to make it simple:

1. Instead of creating a new worksheet each time, do a copy instead. Create
a worksheet in your workbook that will have the macro that you need and hide
that sheet.
2. Assuming that sheet is Sheet2, you can do this to make a copy

Dim sht As Worksheet
With ThisWorkbook
Sheet2.Copy After:=.Sheets(.Sheets.Count)
Set sht = .Sheets(.Sheets.Count)
sht.Name = sht.CodeName
sht.Visible = xlSheetVisible
End With
Set sht = Nothing

3. The copy of Sheet2 will then have all the VBA code as well.

On important thing to keep in mind is the code that you put in Sheet2 will
have to be generic. There should be no references to Sheet2, if you need to
refer to the sheet, use "Me" instead.

One other way to do this might be to put your code in the ThisWorkbook
module instead of the individual sheets.




" wrote:

Hi,

I'm creating a workbook that will has an macro on sheet 1 which
creates a new worksheet with a table for tracking jobs.

What I need to do now is add some code to each new worksheet as part
of the macro on sheet 1.

This is where I have no idea what to do. The code on each worksheet is
basically code to run some batch files when a certain cell is
clicked.

What is the best way to do this?

Thanks


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Adding macro code to a new worksheet

On Mar 5, 12:57 pm, Vergel Adriano
wrote:
There are ways to access and modify the VBA code module of an object in the
workbook. But to make it simple:

1. Instead of creating a new worksheet each time, do a copy instead. Create
a worksheet in your workbook that will have the macro that you need and hide
that sheet.
2. Assuming that sheet is Sheet2, you can do this to make a copy

Dim sht As Worksheet
With ThisWorkbook
Sheet2.Copy After:=.Sheets(.Sheets.Count)
Set sht = .Sheets(.Sheets.Count)
sht.Name = sht.CodeName
sht.Visible = xlSheetVisible
End With
Set sht = Nothing

3. The copy of Sheet2 will then have all the VBA code as well.

On important thing to keep in mind is the code that you put in Sheet2 will
have to be generic. There should be no references to Sheet2, if you need to
refer to the sheet, use "Me" instead.

One other way to do this might be to put your code in the ThisWorkbook
module instead of the individual sheets.

" wrote:
Hi,


I'm creating a workbook that will has an macro on sheet 1 which
creates a new worksheet with a table for tracking jobs.


What I need to do now is add some code to each new worksheet as part
of the macro on sheet 1.


This is where I have no idea what to do. The code on each worksheet is
basically code to run some batch files when a certain cell is
clicked.


What is the best way to do this?


Thanks


Gord & Vergel,

Thanks very much for your suggestions.

I tried both ideas and eventually went for Vergel's idea because I
can't be sure that each machine that this spreadsheet will be used on
will have the template installed.

Thanks again to both of you.

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
Adding code to a macro Excel Helps Excel Worksheet Functions 2 January 23rd 08 06:15 PM
Errors in VB Code since adding macro Susan Excel Discussion (Misc queries) 1 April 11th 06 08:42 PM
Adding VB code to worksheet programatically seegerp Excel Programming 2 March 29th 06 09:52 PM
Adding Worksheet_SelectionChange code by macro Alan Webb[_5_] Excel Programming 2 May 19th 04 10:25 PM
Adding new worksheet from template in code E.J. van Wijngaarden Excel Programming 2 April 9th 04 03:15 AM


All times are GMT +1. The time now is 11:23 AM.

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"