Paste some VBA code via VBA to each newly genereated sheet - excel

I have a simple code that creates a new sheet in template form after running it.
By doing so I get a new sheet with all what I need in my template.
I need to copy aswell a short VBA script Worksheet_Change into that newly created sheets.
For ex. code to get the windows user id and date from system after making change in cells in column B.
How can I put that Worksheet_Change script into my vba code for each new sheet created via VBA?

Like Andreas said use the workbook sheetchange, and in that you can write a function that adds the user ID and date to sheetname.cells(r,c) - wherever you want - as 'sheetname' is passed when workbook_sheetchange event is called

Related

How to use same macro to read all worksheets

I have a excel workbook which already has a macro say "covert". It has two worksheets. First includes configurations (variables) required to run macro and second includes rows (data) on which macro is applied. On first sheet there's one button on-click of this rows are converted into JSON. I checked configuration of this button, 'convert' macro is assigned to it.
Now, I want to make a copy of second sheet which will have similar data and I want to use same macro to read this newly created sheet with slight change in macro.
As soon as I copy sheet with data, I can see macro is also duplicated.
To make macro to decide which sheet should be read, I've added a row in first sheet and then I am adding below code to fetch config.
Dim configSheet As Worksheet
Set configSheet = ThisWorkbook.Worksheets("Configuration")
With configSheet
VAR_SHEET = .Range("B8").value
Then selecting particular sheet using below code.
With ThisWorkbook.Worksheets(VAR_SHEET)
Now, the problem is even after making changes in macro, it is always reading first sheet instead of considering variable.
When you copy a sheet with code like that, any buttons on the sheet do not "auto-adjust" to call code in the copy: they still call the same subs as they did originally (assuming non-activeX button).
Any buttons on the sheet which call code in the sheet module will need to be re-linked to the code in the copy.

Include worksheet change event automatically

If I create a new sheet in a workbook via macro, is there a way to automatically also include a code for worksheet_change event on that sheet.?
I am creating sheet with name "Group A" in a workbook via macro.
There a cells that have to change to uppercase once the user enters values into it.
How can i achieve this?

How to make VBA code to loop via multiple sheets and copy selected (6) cells

How to make VBA code to loop via multiple sheets and copy selected (6) cells, and pasted into summary excel sheet with heading format cells made via the VBA code, including sheets numbers, the summary sheet is created via the code ..
Surely you can use the record button to record the macro doing exactly that, after which look at code and you can neaten if needed. There should be no problem doing this at all!
Please review below link.
http://office.microsoft.com/en-gb/excel-help/record-and-use-excel-macros-HA001054837.aspx

VBA to put code programmatically in sheet-level macro

I was trying to use Workbook_SheetFollowHyperlink() to trigger a macro if the user clicks the hyperlink.
However, this Workbook_SheetFollowHyperlink macro is to be inserted on a sheet-level, not on the module level.
Is there any way that I can programmatically add this Workbook_SheetFollowHyperlink macro to every sheet? The reason is, I create these sheets on the fly using VBA, and the number of sheets and their names are not known in advance beforehand.
Workbook_SheetFollowHyperlink is defined in the ThisWorkbook class module. (At Worksheet level, it's Worksheet_FollowHyperlink)
So, you already have what you need: an Event that responds to following a Hyperlink on any sheet in the workbook.

Excel Copy a sheet with its code to a new xls file

Please, could you tell me if the following is possible in VBA:
I need to write code that copies a sheet from my workbook to a new freshly created xls file. BUT, the sheet I wanna copy has a Macro behind (it's actually a WorkSheet_Change event handler) that I also want to copy to the new file.
Many thanks
Kind regards,
Miloud B
You can Export the code from the original and then Import it into the copy.
Here's an article that shows some code for doing this: Macro to Add a Macro to New Workbooks

Resources