Include worksheet change event automatically - excel

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?

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.

Apply filter buttons to columns when creating a new workbook through VBA

So I have a data set that I scan and VBA creates a new workbook with the desired data in it.
This works fine, all I'm trying to do is that when the new workbook is created, VBA also loads/applies the filter buttons to each header cell in the new workbook.
When I send the new workbook out to the intended party I want it to already have the filter buttons in the header cells so that they don't have to add them themselves.
Any ideas?
Thank you!
Use Range.AutoFilter with no parameters; from the docs:
If you omit all the arguments, this method simply toggles the display of the AutoFilter drop-down arrows in the specified range.
Workbooks("YourWorkbook").Worksheets("YourSheet").Range("A1:G100").AutoFilter

Paste some VBA code via VBA to each newly genereated sheet

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

I want to run macro based on value of cell in sheet after it is opened

I am assigning value to some cell in sheet1 from MS Access. Then based on that cell value I want to pull data from MS Access Table. If I put select statements in workbook_open event it says that cell value is null. How can this be done. I want to run Select after workbook is loaded with the data. Please help me with this. Thanks in advance
You can add your select statements in a macro in the excel workbook and add a line at the end of your VBA to run that macro after the key is updated in cell A1. Here is how you can run excel macros from Access.
Try the following:
verify from Access that the value is in the correct cell of the correct worksheet.
save the workbook
manually open the workbook
manually verify the value is in the correct cell of the correct worksheet
verify the workbook open event macro is looking in the proper place.

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.

Resources