Sunday, April 2, 2017

User Defined Function

'
'Function Statement()
'
'Declares the name, arguments, and code that form the body of a Function procedure.
'
'Syntax
'
'[Public | Private | Friend] [Static] Function name [(arglist)] [As type]
'[statements]
'[name = expression]
'[Exit Function]
'[statements]
'[name = expression]
'
'End Function
'
'The Function statement syntax has these parts:
'
'Part Description
'Public Optional. Indicates that the Function procedure is accessible to all other procedures in all modules. If used in a module that contains an Option Private, the procedure is not available outside the project.
'Private Optional. Indicates that the Function procedure is accessible only to other procedures in the module where it is declared.
'Friend Optional. Used only in a class module. Indicates that the Function procedure is visible throughout the project, but not visible to a controller of an instance of an object.
'Static Optional. Indicates that the Function procedure's local variables are preserved between calls. The Static attribute doesn't affect variables that are declared outside the Function, even if they are used in the procedure.
'name Required. Name of the Function; follows standard variable naming conventions.
'arglist Optional. List of variables representing arguments that are passed to the Function procedure when it is called. Multiple variables are separated by commas.
'type Optional. Data type of the value returned by the Function procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported), Date, String, or (except fixed length), Object, Variant, or any user-defined type.
'statements Optional. Any group of statements to be executed within the Function procedure.
'expression Optional. Return value of the Function.
'
'The arglist argument has the following syntax and parts:
'
'[Optional] [ByVal | ByRef] [ParamArray] varname[( )] [As type] [= defaultvalue]
'
'Part Description
'Optional Optional. Indicates that an argument is not required. If used, all subsequent arguments in arglist must also be optional and declared using the Optional keyword. Optional can't be used for any argument if ParamArray is used.
'ByVal Optional. Indicates that the argument is passed by value.
'ByRef Optional. Indicates that the argument is passed by reference.ByRef is the default in Visual Basic.
'ParamArray Optional. Used only as the last argument in arglist to indicate that the final argument is an Optional array of Variant elements. The ParamArray keyword allows you to provide an arbitrary number of arguments. It may not be used with ByVal, ByRef, or Optional.
'varname Required. Name of the variable representing the argument; follows standard variable naming conventions.
'type Optional. Data type of the argument passed to the procedure; may be Byte, Boolean, Integer, Long, Currency, Single, Double, Decimal (not currently supported) Date, String (variable length only), Object, Variant, or a specific object type. If the parameter is not Optional, a user-defined type may also be specified.
'defaultvalue Optional. Any constant or constant expression. Valid for Optional parameters only. If the type is an Object, an explicit default value can only be Nothing.
'
'Remarks
'
'If not explicitly specified using Public, Private, or Friend, Function procedures are public by default. If Static isn't used, the value of local variables is not preserved between calls. The Friend keyword can only be used in class modules. However, Friend procedures can be accessed by procedures in any module of a project. A Friend procedure does't appear in the type library of its parent class, nor can a Friend procedure be late bound.
'
'
'  Caution
'Function procedures can be recursive; that is, they can call themselves to perform a given task. However, recursion can lead to stack overflow. The Static keyword usually isn't used with recursive Function procedures.
'
'
'All executable code must be in procedures. You can't define a Function procedure inside another Function, Sub, or Property procedure.
'
'The Exit Function statement causes an immediate exit from a Function procedure. Program execution continues with the statement following the statement that called the Function procedure. Any number of Exit Function statements can appear anywhere in a Function procedure.
'
'Like a Sub procedure, a Function procedure is a separate procedure that can take arguments, perform a series of statements, and change the values of its arguments. However, unlike a Sub procedure, you can use a Function procedure on the right side of an expression in the same way you use any intrinsic function, such as Sqr, Cos, or Chr, when you want to use the value returned by the function.
'
'You call a Function procedure using the function name, followed by the argument list in parentheses, in an expression. See the Call statement for specific information on how to call Function procedures.
'
'To return a value from a function, assign the value to the function name. Any number of such assignments can appear anywhere within the procedure. If no value is assigned to name, the procedure returns a default value: a numeric function returns 0, a string function returns a zero-length string (""), and a Variant function returns Empty. A function that returns an object reference returns Nothing if no object reference is assigned to name (using Set) within the Function.

Option Explicit

Function dateformat() As String
dateformat = Now()
End Function

Function dateformaparameter1(dte As Date) As String
dateformaparameter1 = Format(dte, "dd-mmm-yyyy")
End Function

Function dateformaparameter2(dte As Date, tme As Boolean) As String
dateformaparameter2 = Format(dte, "dd:mmm:yyyy:hh:mm:ss")
End Function

Function dateformaparameter3(dte As Date, tme As Boolean) As String

If tme = True Then
    dateformaparameter3 = Format(dte, "dd:mmm:yyyy:hh:mm:ss")
Else
    dateformaparameter3 = Format(dte, "dd:mmm:yyyy")
End If
End Function

Function dateformaparameter4(dte As Date, Optional tme As Boolean = False) As String

If tme = True Then
    dateformaparameter4 = Format(dte, "dddd, mmm yyyy hh:mm:ss")
Else
    dateformaparameter4 = Format(dte, "dd:mmm:yyyy")
End If
End Function

Function salarystatus(salaryrange As Long) As String
    If salaryrange < 25000 Then
        salarystatus = "Low"
    ElseIf salaryrange < 30000 Then
        salarystatus = "Medium"
    ElseIf salaryrange < 35000 Then
        salarystatus = "High"
    ElseIf salaryrange < 40000 Then
        salarystatus = "Excellent"
    End If


End Function

Sub readingfromtable()
Sheet3.Select
Range("A3").Select
    Do
        ActiveCell.Offset(0, 5).value = _
        salarystatus(ActiveCell.Offset(0, 4).value)
        ActiveCell.Offset(1, 0).Select
    Loop Until ActiveCell.value = ""
End Sub

No comments:

Post a Comment

*INTERVIEW QUESTIONS

* Ques 01. What is the difference between ByVal and ByRef and which is default ? Ans-  ByRef : If you pass an argument by reference when...