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?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
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.
Jook has already explained how it works. I will take it a step further.
The syntax of adding a worksheet is
If you check inbuilt Excel’s help then you can see what
Before,After,Count,Typestands forFROM EXCEL”S HELP
Parameters (All 4 parameters are Optional)
Before– An object that specifies the sheet before which the new sheet is added.After– An object that specifies the sheet after which the new sheet is added.Count– The number of sheets to be added. The default value is one.Type– Specifies the sheet type. Can be one of the following XlSheetType constants:xlWorksheet,xlChart,xlExcel4MacroSheet, orxlExcel4IntlMacroSheet. 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
.insertlinesto 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.
Here is an example where I am creating a sheet and then embedding a
Worksheet_SelectionChangeCode which will display a message “Hello World”CODE – TRIED AND TESTED
This is how the new sheet code area looks once you run the above code.