Adding code to new excel sheet dynamically - vba

I need some help Regarding VBA.
In My code I add an excel sheet and rename it and add a validation list to it.I need to run some code on changing value in that validation list.And that must run only on change of that particular cell.
If I am not clear please let me know.Please Help me solving this.

Instead of trying to create the individual code for each new worksheet with the Visual Basic Extensibility (see this link for further reading), simply use the Workbook wide event Workbook_SheetChange (you need to place it in the ThisWorkbookmodule).
In this event code first check, if the worksheet which caused the event is one of the newly created worksheets. This can be done most easily, be checking the .Name of the worksheet.

you can use SelectionChange Event and Change event or it is also possible to use the event Thisworkbook module. SheetChange or SheetSelectionChange.

Related

Excel vba: Auto run the routines

I have various routines in two different modules. Some clears the report, some populates it, fill the blank spaces with zeroes. I am currently running them using F5. But I want when the user opens the sheet, he gets all populated data without having to run that particular sub routine. Is it possible to run the routines in various modules automatically when the excel is opened?
This is pretty easy. Instead of putting code in the module, you will put it in ThisWorkbook and use the Workbook_Open event. You don't necessarily have to move your code. You can just call the existing macros.

Usages for Worksheet_SelectionChange and Workbook_SheetSelectionChange events

As far as I understand it they do the same thing, but while
Worksheet_SelectionChange
is triggered every time selection is changed on a single worksheet,
Workbook_SheetSelectionChange
is triggered every time selection is changed in any worksheet in the workbook.
Are there any other differences between these events?
The single parameter supplied by the Worksheet_SelectionChange event macro is the cell or range of cells that have just been selected, e.g. Target. The Workbook_SheetSelectionChange supplies that but adds the worksheet where the selection was made with Sh.
If you want the same thing to happen on multiple worksheets (not necessarily all of them), use Workbook_SheetSelectionChange and deal with the Sh parameter. This also centralizes your code so that modifications do not have to be repeated in several places. If you are only planning on reacting to selection changes on a single worksheet, use that worksheet's Worksheet_SelectionChange event macro on its own sheet code page.
It seems you answered your own question.
If you want Change events to happen specific to ONE sheet, then put Worksheet_SelectionChange into the Sheet's module.
If you want Change events to happen on every sheet in the book, then put Workbook_SheetSelectionChange into the Book's module.
Note, if you only want say 3 of 5 sheets triggering, you could run a check at the beginning of the Workbook_Change event to check for Sheet.Name

Excel Automatic Calculation Setting

I have a complex project in `Microsoft Office Excel 2007' which utilises a large number of UDFs. Through VBA in a Workbook_Open Event, I set Excel Automatic calculation to OFF and a strategically placed Calculate method to manually calculate the cells whenever I need it so that the UDF doesn't perform recalculation unintentionally.
If the workbook is the ONLY one opened (or the first to open) in an Excel instance, everything works perfect. Only when it's opened AFTER another workbook (within the same instance), my project will inherit the Automatic calculation setting from the FIRST workbook and perform endless calculation on my UDFs. The disable code placed in the Workbook_Open event isn't executed until the UDF finishes the calculation (which can take forever). This only happens if my project is NOT the one opened first.
Through http://www.decisionmodels.com/calcsecretse.htm, I discover that it is the nature of Excel to perform the calculation process BEFORE the Workbook_Open event is executed.
So the question I have obviously relates to the project being opened AFTER another workbook is opened with automatic calculation turned ON:
How do I force my project to disable automatic calculation
without it performing recalculation first (remember, problem only
occurs when the project not the first one to be opened since it will
follow settings from previously opened workbook) OR...
How do I get the project to open in ANOTHER INSTANCE (when double clicked) to avoid
inheriting automatic calculation setting from the previous workbook.
Either way, the answer I'm seeking is for the project to open without performing the calculation first.
Thanks
One way is to use a different workbook (Opener.xls) to initiate opening the UDF workbook (udf.xls)
in Opener.xls the Workbook_Open code
- sets calculation to manual
- opens udf.xls
In your question I don't recognize the way you use to change and inherit that option to your workbooks, But I answer it as a solution:
Use VBA and running VBA macros to change that option for just your active sheet as soon as you need to calculate; by using it like this:
With ActiveSheet
.EnableCalculation = False
.EnableCalculation = True
.Calculate
End With
In another ways that may you need, you can read this part of MSDN article.

Excel VBA - Run macro on Application open

I've got this Design Template I want to apply to the application by default whenever it's opened. But a document needs to be open in order to apply a Design template. So I have to target some handlers whenever a new or existing workbook is opened/created.
So which approach can I take here?
I think you can use the Workbook_open event. It sounded like that would be sufficient for the behavior that you want, but since you want your macros to be global, you might need to make them into an add in for them.
The add-in approach is best for applying to existing workbooks.
If you're looking to have a standard design template applied to each new workbook, though, I suggest you create a template.
To do this:
Create a new empty workbook
Apply the design template
Save the file as Book.xlt (for Excel 2003; Book.xltx for versions 2007, 2010) in the following folder: %appdata%\Microsoft\Excel\XLSTART\
Each time you create a new workbook (by opening Excel or hitting the "New" button), it'll be generated from this template file.

Passing parameter to VBA Macro

I am trying to run a macro when I double click on Visio shapes.
I want to pass the shape object to the VBA macro so that I could do something in the macro depending on the id of the Shape object.
Is it possible to pass the parameter to VBA macro. If yes, please let me know how?
Thanks.
You can put your macro as the EventDblClick event on the shapes you want to watch. To do this you would use the CallThis function (http://msdn.microsoft.com/en-us/library/aa212649(office.11).aspx) to have your macro called every time a shape is double-clicked.
This would require though, that you modify the shapesheet for every shape you want to watch. If you're providing the master(s) for the shapes you want to watch it shouldn't be a big deal, but otherwise you'd have to write code that will add the event to every shape you want to watch (though you could use the Document_ShapeAdded event on a document to add your event to the shapesheet whenever a shape is added)
When you click on an object, the Selection changes. You can use the Selection object in your macro to refer to the currently selected object.
I'm not sure if a macro can be run on double click, but my VBA experience does not come from Visio. As bit of googling turns up that this somehow seems to be possible. If you put it as a Button on a CommandBar, with a little more effort even in the context menu, you would be restricted to a Sub procedure without parameters in any case.