A procedure with a Private scope can be referred to only by other procedures in the same module, whereas procedures with a Public scope can be referred to by any procedure in any module. Unless otherwise stated, every procedure is a Public procedure. If you're writing a set of macros you don't want to be seen (or run) by anyone who knows how to open the Macro dialog box, you can put an Option Private Module statement in the declarations section at the top of the code module to make very procedure, even those that use the Public keyword, private.
You can run the procedure by linking the macro to a trigger, but you can also run the macro by calling it from within another macro. To call a Sub procedure from another procedure, type the name of the procedure and include values for any required arguments. The Call statement is not required, but if you use it, you must enclose any arguments in parentheses.
[Public] or [Private] Sub ShowTheTime
MsgBox (NOW ())
End Sub
The second optional element in a procedure declaration is whether or not to make the procedure retain the values generated by previous executions of the procedure. When you add the Static keyword to the Sub statement, Excel knows to create a durable storage space for each variable and to maintain that space and its contents until you close the workbook.
Defining Function Procedure
A Function procedure is similar to a Sub procedure, but a function can also return a value. A Function procedure can take arguments, such as constants, variables, or expressions that are passed to it by a calling procedure. As with Sub procedures, you can make your Function procedures available to procedures in every other workbook by putting the Public keyword in front of the declaration.
Running Function Procedure
If you want to run a Function procedure, you can do so using one of the following methods:
1. Use the Function procedure in a formula.
2. Call the Function procedure from within another procedure.
3. Call the Function procedure from a cell on a worksheet.
Your Function procedures don't appear in the Macros dialog box. The third way to run a Function procedure is to call it from a cell on a worksheet. To do so, you can call it the same way you would call any other function (for example, =Amortize (ActiveCell.Value)).
There might be times where you want to operate on a value that's stored in a variable in a procedure, not in a worksheet cell. That's when you need to tell the procedure the values on which you want it to operate; those values are called arguments. The element in the parentheses, curItemPrice as Currency, is the name and data type of the variable that's being passed to the function. Where is the value calculated by the procedure stored? It's stored in a variable with the same name as the Function procedure.
Function MarkupPrice (curItemPrice as Currency) as Currency
MarkupPrice = curItemPrice * 1.8
End Function
You can consider using the ByVal keyword to have the procedure use a copy of the data and not the original cell value
Worksheet In Excel Vba
Most people's first knowledge of Excel VBA (or Visual Basic for Applications) is recording a macro. As a user performs a series of operations (e.g. opening a workbook activating a particular worksheet, copying some data, etc) Excel will faithfully record each step and create the necessary VBA code. Each time the user runs the macro, these steps are replayed exactly as they were originally recorded.
Recording macros is great for really simple tasks such as producing a strait-forward report. However, there are distinct limitations to this approach. Firstly, because Excel plays back the steps just as they were originally performed, recorded macros are notoriously slow.
Secondly, recorded macros will only run properly under the conditions in which they were originally recorded. For example, if a particular worksheet needs to be active at a certain point and it is not active when the macro is played back, an error will occur.
This means, effectively, that recorded macros can only really be used by the person who recorded them. They cannot be distributed to one's work colleagues.
One of the first things we do on the Excel VBA training courses that we run at our London training centre is to wean users off the recorder. We give them a good grasp of the Excel object model, a way of programmatically representing each of the elements within the Excel environment such as workbooks, worksheets and cells.
Naturally, there is a bit of a steep learning curve for any users who have done little or no programming. For this reason, we run a five day crash-course in Excel VBA for fledgling macro programmers. We have found that this provides everyone with a chance to gain some degree of confidence with the challenging VBA environment.
After weaning users off the macro recorder, we explain to them the syntax and structure of VBA. They learn how to use variables to store both data and references to Excel objects, how to code logical and iterative structures and how to allow the person running a macro to choose between different options.
Despite its initial complexity, attending an Excel VBA training course is a good investment. It can greatly enhance the productivity of an Excel user. Monthly operations and reports which used to take hours can suddenly be streamlined and accomplished with astounding rapidity.
Getting trained on Excel VBA is a great way to wean oneself off the macro recorder. However, the recorder will still always have its uses. For example, when you are working with an Excel object or a series of steps with some degree of programmatic complexity, recording a few steps then looking at the code generated is a great way to learn new syntax.
Both Shailendra Singh & Andrew Whiteman are contributors for EditorialToday. The above articles have been edited for relevancy and timeliness. All write-ups, reviews, tips and guides published by EditorialToday.com and its partners or affiliates are for informational purposes only. They should not be used for any legal or any other type of advice. We do not endorse any author, contributor, writer or article posted by our team.
Shailendra Singh has sinced written about articles on various topics from Software, Search Engines and Software. We are the fastest growing human edited web directory in the world. Listing of new website in 48 hours on static html pages with static text links.We. Shailendra Singh's top article generates over 12100 views. to your Favourites.
Articles On Life Insurance Changing any negative mindset you have concerning life insurance can help your family a great deal if and when you are no longer there for them physically