+ Reply to Thread
Results 1 to 4 of 4

Macro to write code to a sheet

  1. #1
    Registered User
    Join Date
    06-28-2021
    Location
    Peterborough, England
    MS-Off Ver
    Office 365
    Posts
    2

    Macro to write code to a sheet

    Hello,

    I am self taught with VBA - been using it for a couple of years now, mainly focusing on writing macros into modules. I haven't really touched on writing code into individual sheets before

    At the moment I am trying to set up macros to format my worksheets, and part of that is code that will update the formatting whenever the sheet is selected. I have been able to write the sub that I need within individual worksheets (i.e. private sub worksheet_activate()), but ideally I would like to be able to have this same code automatically inserted when I use the first macro which creates / formats the worksheet in the first place. Is there a way to make this happen?

    Up till now the best solution I can think of is to have a blank 'sheet 1' with the code in it and have my macros start by copying that sheet and going from there, but it seems a little awkward. Any suggestions will be gratefully accepted.

    My current process (in case it helps to clarify what I want):
    - create new Excel workbook
    - use a macro which inserts the basic worksheets I will use on all workbooks, as well as a sheet which includes basic information needed for most/all worksheets
    - use a further macros for the sheets which I will need in this specific workbook as and when I need them

    So ideally I would like code to be inserted at the sheet level automatically, how can this happen?

    Thanks

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to write code to a sheet

    Have you considered creating a Workbook Template .xltm that contains all your sheets/formatting/macros and saving it in your default Template folder.

    When you open a template, add stuff and then choose to save it, it defaults to a .xlsm file and means your template will not be changed and ready to open again.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Registered User
    Join Date
    06-28-2021
    Location
    Peterborough, England
    MS-Off Ver
    Office 365
    Posts
    2

    Re: Macro to write code to a sheet

    Thanks for the idea - in this scenario would it be a case of writing the code into the first worksheet and then copying that worksheet every time I need a new worksheet?

    By the way, I understand that worksheet_activate() only works when it is in the specific worksheet it refers to (when I've tested it elsewhere it doesn't seem to do anything) - is that correct, or is it possible to have a macro that would apply to all worksheets on activation?

    Thanks again

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Macro to write code to a sheet

    If I understand your requirement, which is to have code attached to a sheet replicated in subsequent sheets then typically you'd have a line of macro code at Module level to create a copy of the base sheet. e.g.

    Sheet1.Copy after:=Sheets(Sheets.Count)

    Note Sheet1 here is the VBA sheet code name NOT the sheet tab name which might als happen to be "Sheet1". ALway use VBA code names in preference to tab names which are too easily changed by the user.

    If you wanted to add several sheets you could use a loop and prompt with an INPUT box asking the user how many and use the Input box value as the loop counter.

    Just as an aside, it's rarely necessary to use Sheet Activate syntax, or indeed Range Select stuff to work with the objects. Just address the objects directly. Activating or Selecting just slows things down since it requires VBA to jump back to the Excel App which carries a time penalty.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Add a match and write to another work sheet form this code
    By mmikem in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-26-2018, 05:30 PM
  2. [SOLVED] trying to to write a second code in the same sheet but it brings an error
    By pavlos_x in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 03-18-2015, 02:10 PM
  3. [SOLVED] VBA Code to write data to sheet
    By sara101 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-04-2014, 06:00 AM
  4. Code to write to a cell on another sheet
    By neil40 in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 08-23-2013, 04:31 AM
  5. Replies: 6
    Last Post: 07-18-2013, 02:34 AM
  6. How to write macro for copy data from multiple sheet to a single sheet.
    By Santoshmoni in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2011, 04:39 AM
  7. Macro creating new worksheet. Is it possible to write running code for sheet
    By Kernal87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-30-2008, 06:17 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1