VBA to put code programmatically in sheet-level macro - excel

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.

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?

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

Having a sheet that contains buttons for macros that run off of selections

Is it possible to use Selection to define data used in a macro button that is in another sheet.
I'm trying to make a template workbook that's first sheet is a bunch of macro buttons. I want to be able to select data in another sheet, click my button sheet, and click my button to run a macro on the data I have selected in the other sheet.
The problem that I'm running into is that sheets seem to have independent selections at the same time. So my macro always runs on the selection from the button sheet instead of the sheet I was on. Any thoughts on how I can make this work? The selection of the data needs to be dynamic, so I can't just say .Cells(1,1).value because it might not be the data I need.
Say there are two types of sheets:
a set of data-sheets
a single button-sheet which controls processing via macros tied to buttons
We code a single global range variable in a standard module. We code selection change event macros in all the data-sheets. Then:
We click on a data-sheet
We select cells on the data-sheet
the event macro on that data-sheet records the selection in the global variable
We click to get on the button-sheet
We click on a button
the button macro retrieves the global range
the button macro determines the sheet associated with the stored range (from the Parent Property) and also the cells on that sheet which we selected
the button macro processes the data
EDIT#1:
To learn more about Event Macros (worksheet code), see:
http://www.mvps.org/dmcritchie/excel/event.htm
Also just Google: Excel VBA Event Macro
(there are many examples in this forum, for example)
https://msdn.microsoft.com/en-us/VBA/Excel-VBA/articles/workbook-sheetselectionchange-event-excel
In this link is where I need to start. Every time that I change my selection it runs an event where I could maybe grab the cell values and store them in an array that will stay static when I change sheets. If I get it right I'll post it here. 5/31/2018
To Accomplish this I made rng a public variable and stored my selection from sheet2 in it. Then I used a click event in my buttons to to call the macros that I wanted to run on my range. Here are some screenshots of the codes in my modules and in my sheets. This answer is for anyone that finds this in the future.
https://imgur.com/a/DJDaQiM
The Biggest thing to take away from this is that Public Variables in vba only have to be declared once in any of your modules. NOT IN YOUR SHEET CODE. This was the biggest thing that held me back from getting this done. Good luck and contact me with any questions.

Resources