Excel VBA Variables, Data Types & Constant

Similar documents
Vba Variables Constant and Data types in Excel


d2vbaref.doc Page 1 of 22 05/11/02 14:21

Visual Basic for Applications

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Function: function procedures and sub procedures share the same characteristics, with

Variables in VB. Keeping Track

Language Fundamentals

Unit 4. Lesson 4.1. Managing Data. Data types. Introduction. Data type. Visual Basic 2008 Data types

Fundamentals. Fundamentals. Fundamentals. We build up instructions from three types of materials

Objects and Basic Programming Concepts (See Chapters 5 (skip ED 3), 6 of Albright) (See Chapters 5 (skip ED 4), 6 of Albright)

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Learning Excel VBA. About Variables. ComboProjects. Prepared By Daniel Lamarche

Programming Language 2 (PL2)

Function Exit Function End Function bold End Function Exit Function

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Microsoft Visual Basic 2005: Reloaded

Introduction to Data Entry and Data Types

Outline. Midterm Review. Using Excel. Midterm Review: Excel Basics. Using VBA. Sample Exam Question. Midterm Review April 4, 2014

Civil Engineering Computation

Chapter 1 Getting Started

EXCEL WORKSHOP III INTRODUCTION TO MACROS AND VBA PROGRAMMING

Outline. Data and Operations. Data Types. Integral Types

data_type variable_name = value; Here value is optional because in java, you can declare the variable first and then later assign the value to it.

Variable A variable is a value that can change during the execution of a program.

PROGRAMMING FUNDAMENTALS

variables programming statements

Unit 6 - Software Design and Development LESSON 4 DATA TYPES

Agenda & Reading. VB.NET Programming. Data Types. COMPSCI 280 S1 Applications Programming. Programming Fundamentals

Microsoft Visual Basic 2015: Reloaded

Overview About KBasic

Visual C# Instructor s Manual Table of Contents

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

MATVEC: MATRIX-VECTOR COMPUTATION LANGUAGE REFERENCE MANUAL. John C. Murphy jcm2105 Programming Languages and Translators Professor Stephen Edwards

Chapter-8 DATA TYPES. Introduction. Variable:

Computational Expression

EnableBasic. The Enable Basic language. Modified by Admin on Sep 13, Parent page: Scripting Languages

printf( Please enter another number: ); scanf( %d, &num2);

CSE 123 Introduction to Computing

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

6/14/2010. VBA program units: Subroutines and Functions. Functions: Examples: Examples:

Bit (0, 1) Byte (8 bits: 0-255) Numeral systems. Binary (bin) 0,1 Decimal (dec) 0, 1, 2, 3, Hexadecimal (hex) 0, 1, 2, 3, 1/13/2011

Java Notes. 10th ICSE. Saravanan Ganesh

Good Variable Names: dimensionone, dimension1 Bad Variable Names: dimension One, 1dimension

SKILL AREA 306: DEVELOP AND IMPLEMENT COMPUTER PROGRAMS

Lecture 03 Bits, Bytes and Data Types

MICROSOFT EXCEL 2000 LEVEL 5 VBA PROGRAMMING INTRODUCTION

Unit 3. Constants and Expressions

Software Development Techniques. 26 November Marking Scheme

OpenOffice.org 3.2 BASIC Guide

KEYWORDS DDE GETOBJECT PATHNAME CLASS VB EDITOR WITHEVENTS HMI 1.0 TYPE LIBRARY HMI.TAG

20. VB Programming Fundamentals Variables and Procedures

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

CSc Introduction to Computing

FRAC: Language Reference Manual

IFA/QFN VBA Tutorial Notes prepared by Keith Wong

ACA 1095 Reporting - Editing Multiple Employees. Selecting Employees to Edit

C# and Java. C# and Java are both modern object-oriented languages

Appendix A1 Visual Basics for Applications (VBA)

END-TERM EXAMINATION

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

CS242 COMPUTER PROGRAMMING

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

COS 140: Foundations of Computer Science

Download the files from you will use these files to finish the following exercises.

MICROSOFT EXCEL

<excelunusual.com> Easy Zoom -Chart axis Scaling Using VBA - by George Lungu. < 1. Introduction: Chart naming: by George Lungu

Introduction... 1 Part I: Getting Started with Excel VBA Programming Part II: How VBA Works with Excel... 31

Visual basic tutorial problems, developed by Dr. Clement,

On this class sheet, we can specify the members (properties and methods) that the objects created using this template will have.

Lab # 02. Basic Elements of C++ _ Part1

Excel Programming with VBA (Macro Programming) 24 hours Getting Started

Computer Components. Software{ User Programs. Operating System. Hardware

MS-Access Programming. Assit.Prof. Dr. Anantakul Intarapadung

Values, types and variables


All copyrights reserved - KV NAD, Aluva. Dinesh Kumar Ram PGT(CS) KV NAD Aluva

Input And Output of C++

BASIC ELEMENTS OF A COMPUTER PROGRAM

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

C++ Programming: From Problem Analysis to Program Design, Third Edition

BASIC MACROINSTRUCTIONS (MACROS)

Introduction. Primitive Data Types: Integer. Primitive Data Types. ICOM 4036 Programming Languages

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

An overview about DroidBasic For Android

Standard 11. Lesson 9. Introduction to C++( Up to Operators) 2. List any two benefits of learning C++?(Any two points)

The current topic: Python. Announcements. Python. Python

Fundamental of C programming. - Ompal Singh

NOTES: Variables & Constants (module 10)

COS 140: Foundations of Computer Science

UNIT 2 USING VB TO EXPAND OUR KNOWLEDGE OF PROGRAMMING

LmÉPï C Á npï À ƵÀ ïì itech Analytic Solutions

Chapter 6. Data Types

VISUAL BASIC 6.0 OVERVIEW

Exercise: Using Numbers

Objectives After studying this chapter, you should be able to. Differentiate between numeric and string data.

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming Logic and Design Sixth Edition

HKTA TANG HIN MEMORIAL SECONDARY SCHOOL SECONDARY 3 COMPUTER LITERACY. Name: ( ) Class: Date: Databases and Microsoft Access

Transcription:

Excel VBA Variables, Data Types & Constant Variables are used in almost all computer program and VBA is no different. It's a good practice to declare a variable at the beginning of the procedure. It is not necessary, but it helps to identify the nature of the content (text, data, numbers, etc.) VBA Variables Variables are specific values that are stored in a computer memory or storage system. Later, you can use that value in code and execute. The computer will fetch that value from the system and show in the output. Each variable must be given a name. To name the variable in VBA, you need to follow the following rules. It must be less than 255 characters No spacing is allowed It must not begin with a number Period is not permitted Here are some example for Valid and Invalid names for variables in VBA. Valid Names Invalid Names My_Watch My.Watch

NewCar1 1_NewCar (not begin with number) EmployeeID Employee ID ( Space not allowed) In VBA, we need to declare the variables before using them by assigning names and data type. In VBA, Variables are either declared Implicitly or Explicitly. Implicitly: Below is an example of a variable declared Implicitly. label=guru99 volume=4 Explicitly: Below is an example of variable declared Explicitly. You can use "Dim" keyword in syntax Dim Num As Integer Dim password As String VBA variable is no different than other programming languages. To declare a variable in VBA you use the keyword "Dim." Syntax for VBA Variable, To declare a variable in VBA, type Dim followed by a name: Sub Exercise () Dim <name> End Sub

Before we execute the variables we have to record a macro in Excel. To record a macro do the following - Step 1): Record the Macro 1 Step 2) : Stop Macro 1 Step 3): Open the Macro editor, enter the code for variable in the Macro1 Step 4): Execute the code for Macro 1 Example, for VBA Variable Sub Macro1() Dim Num As Integer Num = 99 MsgBox " Guru " & Num End Sub When you run this code, you will get the following output in your sheet.

n VBA, if the data type is not specified, it will automatically declare the variable as a Variant. Let see an example, on how to declare variables in VBA. In this example, we will declare three types of variables string, joining date and currency. Step 1) Like, in the previous tutorial, we will insert the commandbutton1 in our Excel sheet. Step 2) In next step, right-click on the button and select View code. It will open the code window as shown below. Step 3) In this step, Save your file by clicking on save button Then click on Excel icon in the same window to return the Excel sheet. You can see the design mode is "on" highlighted in green

Step 4) Turn off design mode, before clicking on command button

Step 5) After turning off the design mode, you will click on commandbutton1. It will show the following variable as an output for the range we declared in code. Name Joining Date Income in curreny

Constant in VBA Constant is like a variable, but you cannot modify it. To declare a constant in VBA you use keyword Const. There are two types of constant, Built-in or intrinsic provided by the application. Symbolic or user defined You can either specify the scope as private by default or public. For example, Public Const DaysInYear=365 Private Const Workdays=250 Download Excel containing above code

Excel VBA Data-Types Computer cannot differentiate between the numbers (1,2,3..) and strings (a,b,c,..). To make this differentiation, we use Data Types. VBA data types can be segregated into two types Numeric Data Types Type Storage Range of Values Byte 1 byte 0 to 255 Integer 2 bytes -32,768 to 32,767 Long 4 bytes -2,147,483,648 to 2,147,483,648 Single 4 bytes -3.402823E+38 to -1.401298E-45 for negative values 1.401298E- 45 to 3.402823E+38 for positive values. Double 8 bytes -1.79769313486232e+308 to -4.94065645841247E-324 for negative values 4.94065645841247E-324 to 1.79769313486232e+308 for positive values. Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 Decimal 12 +/- 79,228,162,514,264,337,593,543,950,335 if no decimal is use

bytes +/- 7.9228162514264337593543950335 (28 decimal places) Non-numeric Data Types Data Type Bytes Used Range of Values String (fixed Length) Length of string 1 to 65,400 characters String (Variable Length) Length + 10 bytes 0 to 2 billion characters Boolean 2 bytes True or False Date 8 bytes January 1, 100 to Dece The Boolean Data Type Use the Boolean numeric data type to store logical data that contains only two values: on and off, true and value, yes and no, and so on. The keywords True and False are predefined constants and are interchangeable with the values 1 and 0, respectively. To illustrate these keywords, enter the following statements, one at a time in the VBE's Immediate window, as shown in Figure 3.7:?True = 0?True = -1?False = 0?False = -1

?True = False The Byte Data Type Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data type doesn't include any negative values. If you attempt to assign one, VBA returns an error. The Currency Data Type Use the Currency numeric data type to store monetary values from 922,337,203,477.5808 to 922,337,203,685,477.5807. A Currency data type results in a scaled value with accuracy to 15 digits to the left of the decimal point and 4 digits to the right. Use this data type to avoid rounding errors when precision is of the utmost importance. The Date Data Type The Date data type stores a specially formatted numeric value that represents both the date and time. You don't have to store both the date and time value. The Date data type accepts either the date or the time, or both. Possible values range from January 1, 100 to December 31, 9999. The Decimal Data Type The Decimal data type is a subtype of Variant and not a truly separate data type all its own, accommodating values from 79,228,162,514,264,337,593,543,950,335 to 79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places. The data type maintains precision up to 28 decimal places with

values from 7.9228162514264337593543950335 to 7.9228162514264337593543950335. The Double Data Type Use the Double data type to store precision floating point numbers from 1.79769313486232E308 to 4.94065645841247E-324 or 1.79769313486232E308 to 4.94065645841247E-324. The Integer Data Type This is probably the most common data type in use, besides String. Use this data type to store only whole numbers that range from 32,768 to 32,767. The Long Data Type The Long data type is also an Integer data type storing only whole numbers, but the range is much larger than the traditional Integer data type. Use Long to store values from 2,147,483,648 to 2,147,486,647. The Object Data Type An Object variable is actually a reference to an Access object, such as a form, report, or control. Or, the data type can reference an ActiveX component, or a class object created in a class module. Class modules are covered briefly in "Introducing the VBA Modules," in Chapter 2 and in more depth in "Introducing Objects," in Chapter 8. You'll learn more about Object variables in " in Chapter 8, "Understanding Objects."

The Single Data Type The Single data type stores precision numbers numbers with decimal places or fractional numbers. The data type is similar to Double, but the range is smaller. Use this data type to store values from 3402823E38 to 1.401298E 45 or from 1.401298E 45 to 3.402823E38. The String Data Type String is another very common data type; it stores values or numbers, but treats them as text. There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400 characters. To declare a fixed string, use the Dim statement in the form Dim variablename As String * stringlength In contrast, the variable String data type grows and shrinks as required to fit its stored value. By default, all String variables are of this type. To declare this type, use the Dim statement in the form Dim variablename As String Example 1 Dim password As String Dim yourname As String Dim firstnum As Integer Dim secondnum As Integer Dim total As Integer Dim BirthDay as Date The Variant Data Type

The Variant data type stores numeric and non-numeric values. This data type is the most flexible of the bunch because it stores very large values of almost any type (matches the Double numeric data type). Use it only when you're uncertain of the data's type or when you're accommodating foreign data and you're not sure of the data type's specifications. The Variant data type is VBA's default, so the following code interprets varvalue as a Variant: Dim varvalue Although the Variant data type is flexible, VBA processes these data types a little slower because it must determine the most accurate data type for the assigned value. However, most likely, you'll never notice the performance hit. The biggest disadvantage is the data type's lack of readability. Conclusion Variables are specific values that are stored in a computer memory or storage system. You can use "Dim" keyword in syntax to declare variable explicitly VBA data types can be segregated into two types o o Numeric Data Types Non-numeric Data Types In VBA, if the data type is not specified. It will automatically declare the variable as a Variant

Constant is like a variable, but you cannot modify it. To declare a constant in VBA you use keyword Co