1. Trang chủ
  2. » Công Nghệ Thông Tin

Visual basic and excel

36 478 2

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 36
Dung lượng 273,5 KB

Nội dung

AE6382 VBA - Excel  VBA is Visual Basic for Applications  The goal is to demonstrate how VBA can be used to leverage the power of Excel  VBA syntax and usage  the Excel VB programming environment  the Excel object model  an application AE6382 VBA - Excel  What advantage is there in using VBA  extend Excel – new functions  makes it possible to use the Execl GUI environment  makes it possible to respond to events (mouse, …)  makes Windows programming API accessible  Excel can be used to control Automation servers (other software components that expose an API through COM)  by understanding how to use the Excel object model with VBA it is a small step to using Excel as an Automation server (controlled by other program) AE6382 VBA - Excel  In order to run VBA code your security settings must be properly set  Tools | Macro | Security…  At least Medium security must be set – each macro will require user verification to run  Signed code can be run in all cases AE6382 VBA – The Basics  Data types  Integer 2 byte integer  Long 4 byte integer  Single 4 byte floating point  Double 8 byte floating point  Currency 8 byte real  String upto 64K characters  Byte 1 byte  Boolean 2 byte true or false  Date 8 bytes  Object 4 bytes – an object reference  Variant 16 bytes + 1 byte / character AE6382 VBA – The Basics  The variant data type is special – a variant can hold any type of data  A variable declared as variant (the default) can hold anything  The actual type of the data is kept in the data  It adds flexibility but at a cost – it requires more processing at compute time to determine what it is and how to handle it AE6382 VBA – The Basics  Variables  must start with a letter  can contain _ and numbers  cannot exceed 255 characters in length  Within a procedure declare a variable using  If a variable is not declared it will be created when used, the type will be Variant  Use Option Explicit in the declarations section to require declaration of variables  VBA variables have scope restrictions  variables declared in a procedure are local to that procedure  variables declared in a module can be public or private Dim variable Dim variable As type AE6382 VBA – The Basics  String variables  The first form is variable length  The second form is limited to 50 characters  the variable will be space filled if string is < 50 characters  the string will be truncated if the contents are > 50 characters  the Trim and RTrim functions are useful for working with fixed length strings  Boolean variables contain either True or False Dim variable As String Dim variable As String * 50 AE6382 VBA – The Basics  The Object type is used to store the address (a reference) of an object  this form can be used for any object  this is referred to as late-binding, the object types are checked at runtime (slower)  The declaration of a specific object is  this form will only store Excel Worksheet objects, an attempt to put anything else into it will result in an error  this is referred to as early-binding, the object types are checked at compile time (faster) Dim variable As Object Dim variable As Worksheet AE6382 VBA – The Basics  Arrays are declared using  Arrays can be multidimensional  The lower bound starts at zero  can explicitly specify lower bound  can use Option Base command to reset to something other than 0  The last form above is a dynamic array – it must be dimensioned using ReDim before it can be used  Use ReDim Preserve to retain any existing entries in array - only the upper bound of array can be changed Dim A (1 To 10) As Double Dim B (1 To 10, 1 To 10) As Double Dim C (4,4,4) As Integer Dim D () As Double Option Base 1 AE6382 VBA – The Basics  Constants are declared using  Constants have the same scope limitations as variables Const pi = 3.14159 Const pi As Double = 3.14159 [...]... Programming Environment Excel (all Office components) have a VBA programming environment, VBIDE It consists of the Visual Basic Editor an Object Browser debugging support These are accessed from the Excel menu There is also a set of CHM files that document the components and object models of Office (for version 10) the Excel file is named VBAXL10.CHM XLMAIN10.CHM is the main help file for Excel and contains VBAXL10.CHM... http://www.ae.gatech.edu/classes/ae6382/documents/MS_Scripting/Office10/ AE6382 VBA – Programming Environment To start the Visual Basic Editor from Excel select Tools | Macro | Visual Basic Editor AE6382 VBA – Programming Environment Once in the VB Editor – to start the Object Browser select View | Object Browser AE6382 VBA – Programming Environment In the Object Browser to select only the Excel objects use the pull down menu at the upper left AE6382 VBA... AE6382 VBA – The Basics Logical statements The If Then Else statement is the basic logic test If a>10 Then … End If If a>10 Then … Else … End If If a>10 Then … ElseIf a #1/1/2006# Then … End If AE6382 VBA – The Basics Watch out for Dim a, b, c As Integer it is equivalent to Dim a As Variant Dim b As Variant Dim c As Integer AE6382 VBA – The Basics Objects VBA can use pre-defined objects – such as intrinsic Excel objects VBA can create user-defined objects – Class Modules Declaring a variable to contain an... for library-like procedures class modules for defining classes AE6382 VBA – Excel Objects Excel exposes all of its functionality through objects These are directly accessible to the VBA programmer They are also visible to other applications through COM Automation matlab scripting languages other Office components AE6382 VBA – Excel Objects The top level object is the Workbook A workbook corresponds to... methods for use with Workbooks are Save and SaveAs AE6382 VBA – Excel Objects The Worksheet corresponds to a Sheet Each Worksheet object is kept in the Worksheets collection To add a single new Worksheet Dim ws = Worksheets.Add(count:=1) ws.Visible = False will hide the sheet ws.Delete will delete the sheet Worksheets(“Sheet1”).Delete will delete a sheet AE6382 VBA – Excel Objects The Range object (collection)... pull down menu at the upper left AE6382 VBA – Programming Environment To view information on the Worksheet object and its Name property AE6382 VBA – Programming Environment The usual way to create code is to double-click on one of the Worksheet or Workbook objects in the Project Explorer and enter the code AE6382 VBA – Programming Environment By using the menu item Insert it is possible to add other... objects and collections It is the mechanism by which the object hierarchy is defined By convention, collection names are usually plural Workbooks – list of Workbook objects Worksheets – list of Worksheet objects Range – list of objects that represent cells, columns, rows The following example iterates through Workbooks collection For Each ws In Worksheets Debug.Print ws.Name Next AE6382 VBA – The Basics . AE6382 VBA - Excel  VBA is Visual Basic for Applications  The goal is to demonstrate how VBA can be used to leverage the power of Excel  VBA syntax and usage  the Excel VB programming. processing at compute time to determine what it is and how to handle it AE6382 VBA – The Basics  Variables  must start with a letter  can contain _ and numbers  cannot exceed 255 characters in. programming API accessible  Excel can be used to control Automation servers (other software components that expose an API through COM)  by understanding how to use the Excel object model with VBA

Ngày đăng: 24/10/2014, 10:05

TỪ KHÓA LIÊN QUAN