Sei sulla pagina 1di 4

Common VBA Statements for Excel VBA Programming

By John Walkenbach Part of the Excel VBA Programming For Dummies Cheat Sheet The following table provides a list of commonly used VBA statements that you might use when creating macros for Excel. For more information on a particular statement, consult Excels Help system.

VBA Statement AppActivate Beep Call ChDir ChDrive Close Const Date Declare

What It Does Activates an application window Sounds a tone via the computer's speaker Transfers control to another procedure Changes the current directory Changes the current drive Closes a text file Declares a constant value Sets the current system date Declares a reference to an external procedure in a Dynamic Link Library (DLL) Deletes a section or key setting from an application's entry in the Windows Registry

DeleteSetting

Dim Do-Loop End

Declares variables and (optionally) their data types Loops through a set of instructions Used by itself, exits the program; also used to end a block of statements that begin with If, With, Sub, Function, Property, Type, or Select Re-initializes an array Simulates a specific error condition Exits a block of Do-Loop code Exits a block of For-Next code Exits a Function procedure Exits a property procedure Exits a subroutine procedure

Erase Error Exit Do Exit For Exit Function Exit Property Exit Sub

FileCopy For Each-Next For-Next Function Get GoSub...Return GoTo If-Then-Else Input # Kill Let Line Input # Load Lock...Unlock Mid MkDir Name On Error On...GoSub On...GoTo Open Option Base Option Compare Option Explicit Option Private

Copies a file Loops through a set of instructions for each member of a collection Loops through a set of instructions a specific number of times Declares the name and arguments for a Function procedure Reads data from a text file Branches to and returns from a procedure Branches to a specified statement within a procedure Processes statements conditionally (the Else part is optional) Reads data from a sequential text file Deletes a file Assigns the value of an expression to a variable or property Reads a line of data from a sequential text file Loads an object but doesn't show it Controls access to a text file Replaces characters in a string with other characters Creates a new directory Renames a file or directory Gives specific instructions for what to do in the case of an error Branches, based on a condition Branches, based on a condition Opens a text file Changes the default lower limit for arrays Declares the default comparison mode when comparing strings

Forces declaration of all variables in a module Indicates that an entire module is Private

Print # Private Property Get Property Let Property Set Public Put RaiseEvent Randomize ReDim Rem Reset Resume RmDir SaveSetting Seek Select Case SendKeys Set SetAttr Static

Writes data to a sequential file Declares a local array or variable Declares the name and arguments of a Property Get procedure Declares the name and arguments of a Property Let procedure Declares the name and arguments of a Property Set procedure Declares a public array or variable Writes a variable to a text file Fires a user-defined event Initializes the random number generator Changes the dimensions of an array Specifies a line of comments (same as an apostrophe [']) Closes all open text files Resumes execution when an error-handling routine finishes Removes an empty directory Saves or creates an application entry in the Windows Registry Sets the position for the next access in a text file Processes statements conditionally Sends keystrokes to the active window Assigns an object reference to a variable or property Changes attribute information for a file Declares variables at the procedure level so that the variables retain their values as long as the code is running and the project hasn't been reset. Pauses the program Declares the name and arguments of a Sub procedure Sets the system time Defines a custom data type

Stop Sub Time Type

Unload While...Wend

Removes an object from memory Loops through a set of instructions as long as a certain condition remains true Sets the output line width of a text file Allows a shorthand way of accessing multiple properties for an object Writes data to a sequential text file

Width # With Write #

Potrebbero piacerti anche