Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8490729
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T22:12:38+00:00 2026-06-10T22:12:38+00:00

I am creating a macro and part of the macros function is to make

  • 0

I am creating a macro and part of the macros function is to make VBA create a new spreadsheet. Because of the nature of distribution the name will change. I need to add code to this spreadsheet. Is there anyway I can do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T22:12:40+00:00Added an answer on June 10, 2026 at 10:12 pm

    Jook has already explained how it works. I will take it a step further.

    The syntax of adding a worksheet is

    expression.Add(Before, After, Count, Type)
    

    If you check inbuilt Excel’s help then you can see what Before, After, Count, Type stands for

    FROM EXCEL”S HELP

    Parameters (All 4 parameters are Optional)

    1. Before – An object that specifies the sheet before which the new sheet is added.
    2. After – An object that specifies the sheet after which the new sheet is added.
    3. Count – The number of sheets to be added. The default value is one.
    4. Type – Specifies the sheet type. Can be one of the following XlSheetType constants: xlWorksheet, xlChart, xlExcel4MacroSheet, or xlExcel4IntlMacroSheet. If you are inserting a sheet based on an existing template, specify the path to the template. The default value is xlWorksheet.

    Once the sheet is created then you need to use .insertlines to create the relevant procedure and to also embed the code that you want to run.

    NOTE – IMP: If you want the code to embed code in the VBA project, you need to ensure that you have “Trust Access to the VBA Project Object Model” selected. See snapshot.

    enter image description here

    Here is an example where I am creating a sheet and then embedding a Worksheet_SelectionChange Code which will display a message “Hello World”

    CODE – TRIED AND TESTED

    Option Explicit
    
    Sub Sample()
        Dim ws As Worksheet
        Dim nLines As Long
        Dim VBP As Object, VBC As Object, CM As Object
        Dim strProcName As String
    
        Set ws = Worksheets.Add
    
        Set VBP = ThisWorkbook.VBProject
        Set VBC = VBP.VBComponents(ws.Name)
        Set CM = VBC.CodeModule
    
        strProcName = "Worksheet_SelectionChange"
    
        With ThisWorkbook.VBProject.VBComponents( _
        ThisWorkbook.Worksheets(ws.Name).CodeName).CodeModule
            .InsertLines Line:=.CreateEventProc("SelectionChange", "Worksheet") + 1, _
            String:=vbCrLf & _
            "    Msgbox ""Hello World!"""
        End With
    End Sub
    

    This is how the new sheet code area looks once you run the above code.

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a spreadsheet to train my numerical skills. Now, I use VBA macros
I'm creating a button to execute a VBA function/macro which is written by myself.
I'm creating a convenience macro. Part of the convenience is that a regular expression
Long ago writing VBA macros for MSFT Office was part of my job. Now
im creating a macro and need to check some pixels to make sure the
To clarify. I have 1 spreadsheet I am creating the VBA in. When the
I am creating a Macro with VBA in Excel 2010 to move rows from
I'm creating a macro in C++ that declares a variable and assigns some value
I have the following couple of C pre-processor macros for creating test functions: //
Creating a class at runtime is done as follows: klass = Class.new superclass, &block

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.