Showing posts with label vba. Show all posts
Showing posts with label vba. Show all posts

Tuesday, June 28, 2016

Learn VB Script Part 7

Using Loop Statements

Do.. Loop Statement

Do...Loop: Repeats a block of statements while a condition is True or until a condition becomes True.

Syntax:

 Do [{While | Until} condition]
   [statements]
   [Exit Do]
   [statements]
Loop

OR

Do
   [statements]
   [Exit Do]
   [statements]

Loop [{While | Until} condition]


Examples 1
Do...Loop

Counter = 1

    Do While Counter < 4

                        Total =Inputbox("Please enter the total marks in numbers")
                        
                                    If   Total < 50 Then
                                    
                                                MsgBox  "Fail"
                                    
                                    ElseIf  Total >=50 and Total <=60   then
                                    
                                                Msgbox "Second Class"
                        
                                    ElseIf  Total >60 and Total <80   then
                                    
                                                Msgbox "First Class"
                        
                                    ElseIf  Total >=80  then
                                    
                                                Msgbox "Distinction"
                        
                                    Else
                        
                                                Msgbox "Invalid Marks"
                                    
                                    End If

        Counter = Counter + 1

        Loop

Example 2

Counter = 1

            Do 

                        Total =Inputbox("Please enter the total marks in numbers")
                        
                                    If   Total < 50 Then
                                    
                                                MsgBox  "Fail"
                                    
                                    ElseIf  Total >=50 and Total <=60   then
                                    
                                                Msgbox "Second Class"
                        
                                    ElseIf  Total >60 and Total <80   then
                                    
                                                Msgbox "First Class"
                        
                                    ElseIf  Total >=80  then
                                    
                                                Msgbox "Distinction"
                        
                                    Else
                        
                                                Msgbox "Invalid Marks"
                                    
                                    End If

        Counter = Counter + 1

        Loop until Counter > 4

Example 3

   'Exit Do While loop

Do While Counter < 10

   Counter=counter + 1

   If  counter = 6 Then

      Exit Do

   End If

   MsgBox Counter

Loop


Example 4
'Exit Do Until loop

Do
    
   Counter=Counter + 1

     If  Counter = 6 Then

        Exit Do

     End If

   MsgBox Counter
   
Loop Until counter >11




Monday, May 30, 2016

Learn VB Script - Part 4

Operators

Operators precedence

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order called operator precedence. You can use parentheses to override the order of precedence and force some parts of an expression to be evaluated before others. Operations within parentheses are always performed before those outside. Within parentheses, however, standard operator precedence is maintained.


When expressions contain operators from more than one category, arithmetic operators are evaluated first, comparison operators are evaluated next, and logical operators are evaluated last. Comparison operators all have equal precedence; that is, they are evaluated in the left-to-right order in which they appear. Arithmetic and logical operators are evaluated in the following order of precedence.




ArithmeticComparisonLogical
DescriptionSymbolDescriptionSymbolDescriptionSymbol
Exponentiation^Equality=Logical negationNot
Unary negation-Inequality<>Logical conjunctionAnd
Multiplication*Less than<Logical disjunctionOr
Division/Greater than>Logical exclusionXor
Integer division\Less than or equal to<=Logical equivalenceEqv
Modulus arithmeticModGreater than or equal to>=Logical implicationImp
Addition+Object equivalenceIs
Subtraction-
String concatenation&






Thursday, May 26, 2016

Learn VB Script Part 3

Data Types

What Are VBScript Data Types?


VBScript has only one data type called a Variant. A Variant is a special kind of data type that can contain different kinds of information, depending on how it's used. Because Variant is the only data type in VBScript, it's also the data type returned by all functions in VBScript.

At its simplest, a Variant can contain either numeric or string information.

       A Variant behaves as a number when you use it in a numeric context and as a string when you use it in a string context. That is, if you are working with data that looks like numbers, VBScript assumes that it is numbers and does what is most appropriate for numbers. Similarly, if you're working with data that can only be string data, VBScript treats it as string data. You can always make numbers behave as strings by enclosing them in quotation marks (" ").

Variant Subtypes

Beyond the simple numeric or string classifications, a Variant can make further distinctions about the specific nature of numeric information. For example, you can have numeric information that represents a date or a time. When used with other date or time data, the result is always expressed as a date or a time.

You can also have a rich variety of numeric information ranging in size from Boolean values to huge floating-point numbers. These different categories of information that can be contained in a Variant are called subtypes. Most of the time, you can just put the kind of data you want in a Variant, and the Variant behaves in a way that is most appropriate for the data it contains.

The following table shows subtypes of data that a Variant can contain. 


SubtypeDescription
BooleanLogical value:
true (numeric value is "nonzero", mostly -1) or
false (numeric value is 0)
See the notes below.
ByteInteger value (1 byte) in the range: 0 to 255.
IntegerInteger value (2 bytes) in the range: -32,768 to 32,767.
LongInteger value (4 bytes) in the range: -2,147,483,648 to 2,147,483,647.
SingleReal value (4 bytes) with the precision 7 digits in the range:
-3.402823E38 to -1.401298E-45 for negative values.
1.401298E-45 to 3.402823E38 for positive values.
Binary implementation format is according to standard IEEE-754 (32-bit)
DoubleReal value (8 bytes) with the precision 15 digits in the range:
-1.79769313486232E308 to -4.94065645841247E-324 for negative values.
4.94065645841247E-324 to 1.79769313486232E308 for positive values.
Binary implementation format is according to standard IEEE-754 (64-bit)
DateValue containing Date and time. See the notes below.
Stringvariable-length string that can be up to approximately 2 billion characters in length in the Unicode character set.
Object"pointer" (reference) to any object.
ArrayArray of values. See How to use array of values in the PROMOTIC system.
EmptyFlag that value is uninitialized.
NullFlag that value intentionally contains no valid data.


Numeric data types Byte, Integer and Long:

Entering the values with hexadecimal constant:

It is possible to enter a number into the variable in decimal figure, for example:
nVal = 36524

or in hexadecimal figure, where the &h prefix is used, for example:
nVal = &h8EAC


(8EAC hexadecimly is 36524 decimaly). But the VBScript evaluates the hexadecimal constants the following way: if the constant is two byte (like we have here), then it creates Integer data type. Because the value 8EAC has the highest bit set, then the constant will be considered as negative numberequal to -29012!


Boolean data type:

Value of the Boolean type can logicaly have just two states (true / false), but it is actually stored asInteger data type and it is supposed that false is zero and true is non-zero.

And this way of understanding the true value can cause trouble, because by default the value is -1, but also any other non-zero value is also true !! That is why we do not recommend comparing the truevalues.

Example:
If value = true Then ...

Date data type:

If the value of the date/time is stored as Date data type, then the values of year/month/day/hour/minute/second can be obtained, for example, by the VBScript methods Year/Month/Day/Hour/Minute/Second. See also VBScript date and time functions and Pm date and time methods.

Date creating from String data type:

The required date (time, date and time) must be written into quotation marks in the format specified in Windows settings "Control Panel / Date/Time / Date and time page". For example, if the date is set in the format d.M.yyyy in the local setting, then the time can be entered as String in the following way:

Dim MyTime, MyDate, MyDateTime
MyTime = CDate("18:59:33")
MyDate = CDate("31.12.2015")
MyDateTime = CDate("31.12.2015 18:59:33")


Date creating by the VBScript constant:
If the date (time, date and time) is entered by this way, it doesn't depend on setting the computer and the date (time, date and time) will always be in the correct format. The date (time, date and time) is entered by the # char in the format #dd/mm/yy# (or #hh:mm:ss# or #dd/mm/yy hh:mm:ss#):

Dim MyTime, MyDate, MyDateTime
MyTime = #18:59:33#
MyDate = #23/10/05#
MyDateTime = #23/10/13 18:59:33#


Date creating from real number:
The date (year, month, day) is represented by the whole part of the real number.
It is a number of days since 30.12.1899. Value 1.0 refers to the date 31.12.1899, value –1.0 refers to the date 29.12.1899.

The time (hours, minutes, seconds) is represented by the decimal part of the real number. The value x.5 means the exact midday (12:00:00) in the day x.

The value from 0.0 to 1.0 is a special case. The value is not considered to be a date but only as time or time range. The value 0.5 represents 12 hours, 1/24/60 represents 1 minute.

The following script assigns the value of the date and the time greater by one day and by one minute into the variable MyDateTime on each execution of the script.

MyDateTime = CDate(MyDateTime + 1 + 1/24/60)

If the variable MyDateTime is initialized by value 1, then after the first execution of the script, the value of the variable equals to "1.1.1900 0:01:00", after the second execution, it equals to "2.1.1900 0:02:00".



Tuesday, May 17, 2016

Learn VB Script Part 2

Variables


What is a variable?


A variable is a convenient placeholder that refers to a computer memory location where you can store program information that may change during the time your script is running. 


Declaring a Variable

Declare variables explicitly in your script using the Dim statement, the Public statement, and the Private statement.

Examples:

Dim length
Dim height

Declare multiple variables by separating each variable name with a comma

Dim radius,area,side


Option Explicit 

We can also declare a variable implicitly by simply using its name in your script. That's not generally a good practice because you could misspell the variable name in one or more places, causing unexpected results when your script is run. For that reason, the Option Explicit statement is available to require explicit declaration of all variables. The Option Explicit statement should be the first statement in your script.




Rules for variable declaration


Variable names follow the standard rules for naming anything in VBScript.
A variable name:
  • Must begin with an alphabetic character.
  • Cannot contain an embedded period.
  • Must not exceed 255 characters.
  • Must be unique in the scope in which it is declared.


Assign values to variables

The variable is on the left side of the expression and the value you want to assign to the variable is on the right

Example

height = 10
length = 20