Excel VBA Programming For Dummies

The Visual Basic for Applications (VBA) programming language lets you automate many aspects of Excel by writing Sub and Function procedures (also known as macros). A good knowledge of VBA can save time and make you a much more productive Excel user.






>


>


Common VBA Statements for Excel VBA Programming


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 Excel's Help system.



















































































































































































































































































































VBA StatementWhat It Does
AppActivateActivates an application window
BeepSounds a tone via the computer's speaker
CallTransfers control to another procedure
ChDirChanges the current directory
ChDriveChanges the current drive
CloseCloses a text file
ConstDeclares a constant value
DateSets the current system date
DeclareDeclares a reference to an external procedure in a Dynamic Link
Library (DLL)
DeleteSettingDeletes a section or key setting from an application's entry in
the Windows Registry
DimDeclares variables and (optionally) their data types
Do-LoopLoops through a set of instructions
EndUsed by itself, exits the program; also used to end a block of
statements that begin with If, With, Sub, Function, Property, Type,
or Select
EraseRe-initializes an array
ErrorSimulates a specific error condition
Exit DoExits a block of Do-Loop code
Exit ForExits a block of For-Next code
Exit FunctionExits a Function procedure
Exit PropertyExits a property procedure
Exit SubExits a subroutine procedure
FileCopyCopies a file
For Each-NextLoops through a set of instructions for each member of a
series
For-NextLoops through a set of instructions a specific number of
times
FunctionDeclares the name and arguments for a Function procedure
GetReads data from a text file
GoSub...ReturnBranches to and returns from a procedure
GoToBranches to a specified statement within a procedure
If-Then-ElseProcesses statements conditionally
Input #Reads data from a sequential text file
KillDeletes a file from a disk
LetAssigns the value of an expression to a variable or
property
Line Input #Reads a line of data from a sequential text file
LoadLoads an object but doesn't show it
Lock...UnlockControls access to a text file
MidReplaces characters in a string with other characters
MkDirCreates a new directory
NameRenames a file or directory
On ErrorGives specific instructions for what to do in the case of an
error
On...GoSubBranches, based on a condition
On...GoToBranches, based on a condition
OpenOpens a text file
Option BaseChanges the default lower limit for arrays
Option CompareDeclares the default comparison mode when comparing
strings
Option ExplicitForces declaration of all variables in a module
Option PrivateIndicates that an entire module is Private
Print #Writes data to a sequential file
PrivateDeclares a local array or variable
Property GetDeclares the name and arguments of a Property Get
procedure
Property LetDeclares the name and arguments of a Property Let
procedure
Property SetDeclares the name and arguments of a Property Set
procedure
PublicDeclares a public array or variable
PutWrites a variable to a text file
RaiseEventFires a user-defined event
RandomizeInitializes the random number generator
ReDimChanges the dimensions of an array
RemSpecifies a line of comments (same as an apostrophe ['])
ResetCloses all open text files
ResumeResumes execution when an error-handling routine finishes
RmDirRemoves an empty directory
SaveSettingSaves or creates an application entry in the Windows
Registry
SeekSets the position for the next access in a text file
Select CaseProcesses statements conditionally
SendKeysSends keystrokes to the active window
SetAssigns an object reference to a variable or property
SetAttrChanges attribute information for a file
StaticDeclares variables at the procedure level so that the variables
retain their values as long as the code is running
StopPauses the program
SubDeclares the name and arguments of a Sub procedure
TimeSets the system time
TypeDefines a custom data type
UnloadRemoves an object from memory
While...WendLoops through a set of instructions as long as a certain
condition remains true
Width #Sets the output line width of a text file
WithSets a series of properties for an object
Write #Writes data to a sequential text file




>



>


>


VBA Functions for Excel VBA Programming


The VBA language contains a number of functions that you can use to build code in Excel. The following table provides descriptions of the most useful functions. When writing code, type the word VBA followed by a period, and you'll see a drop-drop list of these functions. See Excel's Help system for more details.























































































































































































































































































































































































































































































VBA FunctionWhat It Does
AbsReturns the absolute value of a number
ArrayReturns a variant that contains an array
AscConverts the first character of string to its ASCII value
AtnReturns the arctangent of a number
CBoolConverts an expression to boolean
CByteConverts an expression to byte data type
CCurConverts an expression to currency data type
CDateConverts an expression to date data type
CDblConverts an expression to double data type
CDecConverts an expression to decimal data type
ChooseSelects and returns a value from a list of arguments
ChrConverts an ANSI value to a string
CIntConverts an expression to integer data type
CLngConverts an expression to long data type
CosReturns the cosine of a number
CreateObjectCreates an OLE Automation object
CSngConverts an expression to single data type
CStrConverts an expression to string data type
CurDirReturns the current path
CVarConverts an expression to variant data type
CVDateConverts an expression to date data type
CVErrReturns a user-defined error number
DateReturns the current system date
DateAddReturns a date with a specific date interval added to it
DateDiffReturns a date with a specific date interval subtracted from
it
DatePartReturns an integer containing a specific part of a date
DateSerialConverts a date to a serial number
DateValueConverts a string to date
DayReturns the day of the month of a date
DirReturns the name of a file or directory that matches a
pattern
DoEventsYields execution so the operating system can process other
events
EOFReturns True if the end of a text file has been reached
ErrorReturns the error message that corresponds to an error
number
ExpReturns the base of the natural logarithms (e) raised to
a power
FileAttrReturns the file mode for a text file
FileDateTimeReturns the date and time when a file was last modified
FileLenReturns the number of bytes in a file
FixReturns the integer portion of a number
FormatDisplays an expression in a particular format
Format CurrencyReturns a number as a string, formatted as currency
FormatDateTimeReturns a number as a string, formatted as a date and/or
time
Format NumberReturns a number as a formatted string
Format PercentReturns a number as a string, formatted as a percentage
FreeFileReturns the next file number available for use by the Open
statement
GetAllReturns a list of key settings and their values (originally
created with SaveSetting) from an application’s entry in the
Windows registry
GetAttrReturns a code representing a file attribute
GetObjectRetrieves an OLE Automation object from a file
GetSettingReturns a key setting value from an application’s entry
in the Windows registry
HexConverts from decimal to hexadecimal
HourReturns the hour of a time
IIfReturns one of two parts, depending on the evaluation of an
expression
InputReturns a specific number of characters from an open text
file
InputBoxDisplays a box to prompt a user for input
InStrReturns the position of a string within another string
InStrRevReturns the position of a string within another string,
beginning at the back end of the string
IntReturns the integer portion of a number
IsArrayReturns True if a variable is an array
IsDateReturns True if a variable is a date
IsEmptyReturns True if a variable has been initialized
IsErrorReturns True if an expression is an error value
IsMissingReturns True if an optional argument was not passed to a
Procedure
IsNullReturns True if an expression contains no valid data
IsNumericReturns True if an expression can be evaluated as a number
IsObjectReturns True if an expression references an OLE Automation
object
JoinReturns a string created by joining a number of substrings
contained in an array
LBoundReturns the lower bound of an array
LCaseReturns a string converted to lowercase
LeftReturns a specified number of characters from the left of a
string
LenReturns the length of a string, in characters
LocReturns the current read or write position of a text file
LOFReturns the number of bytes in an open text file
LogReturns the natural logarithm of a number
LTrimReturns a copy of a string with no leading spaces
MidReturns a specified number of characters from a string
MidBReturns a specified number of bytes from a string
MinuteReturns the minute of a time
MonthReturns the month of a date
MonthNameReturns a string indicating the specified month
MsgBoxDisplays a modal message box
NowReturns the current system date and time
OctConverts from decimal to octal
ReplaceReturns a string in which one substring is replaced with
another
RGBReturns a number representing an RGB color value
SpaceReturns a string with a specified number of spaces
SplitReturns an array consisting of a number of substrings
SqrReturns the square root of a number
StrReturns a string representation of a number
RightReturns a specified number of characters from the right of a
string
RndReturns a random number between 0 and 1
RoundRounds a number to a specific number of decimal places
RTrimReturns a copy of a string with no trailing spaces
SecondReturns the second of a time
SeekReturns the current position in a text file
SgnReturns an integer that indicates the sign of a number
ShellRuns an executable program
SinReturns the sin of a number
StrCompReturns a value indicating the result of a string
comparison
StrConvReturns a string variant converted as specified
StringReturns a repeating character or string
StrReverseReverses the character order of a string
SwitchEvaluates a list of expressions and returns a value associated
with the first expression in the list that is True
TabPositions output in an output stream
TanReturns the tangent of a number
TimeReturns the current system time
TimerReturns the number of seconds since midnight
TimeSerialReturns the time for a specified hour, minute, and second
TimeValueConverts a string to a time serial number
TrimReturns a string containing a copy of a specified string
without leading spaces and trailing spaces
TypeNameReturns a string that describes the data type of a
variable
UBoundReturns the upper bound of an array
UCaseConverts a string to uppercase
ValReturns the numbers contained in a string
VarTypeReturns a value indicating the subtype of a variable
WeekdayReturns a number representing a day of the week
Weekday NameReturns a string indicating the specified weekday
YearReturns the year of a date




>






>
dummies


Source:http://www.dummies.com/how-to/content/excel-vba-programming-for-dummies-cheat-sheet.html

No comments:

Post a Comment