Vous êtes sur la page 1sur 24

VBA

(VISUAL BASIC APPLICATION)


4
TH
WEEK
Presented By: P.SIVANAGABABU
TOPICS:
SINGLE LOOP
DOUBLE LOOP
TRIPLE LOOP
DO WHILE LOOP
CREATE A PATTERN
DO UNTIL LOOP
LOOP THROUGH DEFINED RANGE
LOOP THROUGH ENTIRE ROW COLUMN
RANDOMLY SHORT DATA
REMOVE DUPLICATES
SORT NUMBERS
STEP KEYWORDS
MACRO ERRORS
DEBUGGING
OBJECTIVE ERROR
ERROR HANDALING
INTERRUPT A MACRO
STRING MANIPULATION
SEPERATE STRING
REVERSE STRING
COVERT TO PROPER CASE

SINGLE LOOP:

Looping is one of the most powerful programming
techniques.
A single loop to loop through a one-dimensional range of
cells.

Code:

Sub
Dim I as integer
For I = 1 to 6
Cells (I,1).value = 100
Next I
End sub

DOUBLE LOOP:

A double loop to loop through a two-dimensional range of
cells.

Code:

Sub
Dim I as integer , j as integer
For I = 1 to 6
For j = 1 to 2
Cells (i , j).value = 100
Next j
Next I
End sub

TRIPLE LOOP:

A triple loop to loop through two-dimensional ranges on
multiple Excel worksheets.

Code:

Sub
Dim c as integer , I as integer , j as integer
For c=1 to 3
For i=1 to 6
For j=1 to 2
Worksheets (c).cells(I , j).value = 100
Next j
Next I
Next c
End sub
DO WHILE LOOP:

Besides the For Next loop, there are other loops in Excel VBA. For
example, the Do While Loop. Code placed between Do While and
Loop will be repeated as long as the part after Do While is true.

Code:

sub
Dim I as integer
I = 1
Do while I < 6
Cells (I ,1).value = 20
I = i+1
Loop
I = 1
Do while cells (I,1)<>
Cell(I,2).value = cells(I,1).value + 10
i=i+1
Loop
End sub

CREATE A PATTERN:

Below we will look at a program in Excel VBA that creates a
pattern.

Code:

Sub
Dim I as integer , j as integer
For I = 1 to 5 step 2
For j = 1 to 5 step 2
Cell (i , j).interior.colorindex = 3
Cell (i,j).offset (1,1).interior.colorindex = 15
Next j
Next I
End sub


DO UNTIL LOOP:

Do Until Loop in Excel VBA. Code placed between Do Until and
Loop will be repeated until the part after Do Until is true.

Code:

Sub
Dim k as integer , j as integer
j = input box (number)
K=1
Do until k>j
Cells (k,1).value = 20
K=k+1
Loop
End sub
LOOP THROUGH DEFINED RANGE:
loops through a defined range. For example, when we want to
square the numbers in Range("A1:A3"). Did you know you can
also loop through a dynamic range.

Code:

Sub
Dim rng as range , cell as range
Set rng = range (a1:a3)
set rng = selection
For each cell in rng
Cell. Value = cell. Value * cell. value
Next cell
End sub
LOOP THROUGH ENTIRE ROW COLUMN:
Look at a program in Excel VBA that loops through the entire first
column and colors all values that are lower than a certain value.

Code:

Sub
Dim I as long
Columns(1).font.color = vb black
For I = 1 to rows. Count
If cells(I,1).value < range (d2).value and not isempty (cells(I,1).value)
then
Cells(I,1).font.color = vb red
End if
Next I
End sub

RANDOMLY SHORT DATA :

Below we will look at a program in Excel VBA that randomly sorts
data (in this example randomly sorts names).

Code:

Sub
Dim tempstring as string, tempinteger as integer, I as integer, j as integer
For I = 1 to 5
Cells(I,2).value = worksheetfunction.randbetween(0,1000)
Next I
For I = 1 to 5
For j = i+1 to 5
If cells (j,2).value < cells (I,2).value then
Tempstring = cells(I,1).value
Cells(j,1).value = cells(j,1).value
Cells (j,1).value= tempinteger
Tempinteger = cells(I,2).value
Cells(I,2).value=cell(j,2).value
Cells(j,2).value= tempinteger
End if
Next j
Next I
End sub

REMOVE DUPLICATES:

Below we will look at a program in Excel VBA that removes
duplicates.
Code:
Sub
Dim toad as Boolean, uniquenumber as integer, I as integer, j as integer
Cells(1,2) .value=cells(1,1).value
Unique numbers = 1
Toad = true
For i=2 to 10
For j=1 to uniquenumber
If cells(I,1).value=cells(j,2).value then
Toad = false
End if
Next j
If to add=true then
Cells (unique numbers +1,2).value=cells(I,1).value
Uniquenumbers = uniquenumbers+1
End if
Toad = true
Next I
End sub

SORT NUMBERS:

Below we will look at a program in Excel VBA that sorts numbers

Code:

Sub
Dim I as integer, j as integer, temp as integer, rng as range
Set rng = range(a1).current region
For i=1 to rng. Count
For j=i+1 to rng. Count
If rng. Cells(j)<rng. Cells(i) then
swap number
Temp = rng. Cells(i)
Rng. Cells(i)=rng. Cells(j)
Ran. Cells(j)=temp
End if
Next j
Next I
End sub

STEP KEYWORDS:
Step keyword in Excel VBA to specify a different increment for the
counter variable of a loop.

Code:

Sub
Dim I as integer, j as integer
For i=1 to 6 step 3
Cells(I,1).value=100
Next I
For j=8 to 3 step -2
Cells(6,j).value = 50
Next j
End sub

MACRO ERRORS:

This chapter teaches how to deal with macro
errors in Excel. First, let's create some errors.

Macro comment:
A macro comment is a piece of text in a macro which will
not be executed by Excel VBA. It is only there to provide
you information about the macro.

Code:

Sub
Range(a1).valu=sree
End sub
DEBUGGING:
This example teaches you how to debug code in Excel
VBA. F8 is the debugging shortcut key. Use to the single
step debug.
Code:
Sub
Dim I as integer, j as integer
For I = 1 to 2
For j = 1 to 5
Cells(I,j).value=worksheet function . Eand between (2,100)
Next j
Next I
End sub

OBJECTIVE ERROR:
When an error in Excel VBA occurs, the properties of the Err object are filled with
information.

Code:

Sub
Dim rng as range, cell as range
set rng = selection
For each cell in rng
On error goto invalid value:
Cell. Value= sqr (cell . value)
Next cell
Exit sub
Invalid value:
Select case err. Number
Case is = 5
Ms box cant calculate square root of negative number at cell & cell. Address
Case is =13
Msg box cant calculate square root of text at cell &cell. Address
End select
Resume next
End sub



ERROR HANDALING:

Two programs in Excel VBA. One program simply ignores errors. The
other program continues execution at a specified line upon hitting an
error.

Code:

Sub
Dim rng as range , cell as range
Set rng = selection
For each cell in rng
On error resume next
Cell. Value = sqr (cell. Value)
Next cell
End sub

INTERRUPT A MACRO:
interrupt a macro in Excel at any time by pressing Esc or Ctrl +
Break.

Code:

Sub
Application.enablecancelkey = xl disabled
Dim x as long
X=5
Do while x>2
X = x+1
Loop
Application.enablecancelkey = xl interrupt
End sub



STRING MANIPULATION

Joint string:
Sub
Dim text1 as string, text2 as string
Text1 = hum
Text2 = tum
Msg box text1 & & text2
End sub

Left:
Sub
Dim text as string
Text = sree ram
Msg box left (text,4)

Right:
Sub
Msg box right (sree krishna)
End sub

SEPERATE STRING:

Code:

Sub
Dim fullname as string, commaposition as integer, I as
integer
For I= 2 to 7
Fullname = cells(I,1).value
Commaposition instr (fullname, 1)
Cells(I,2).value= mid (fullname, commaposition +2)
Cells(I,3).value= left (fullname, commaposition -1)
Next I
End sub



REVERSE STRING:

A program in Excel VBA that can reverse strings.

Code:

Sub
Dim text as string, reversed text as string, length as integer, I as
integer
Text = input box(enter the text you want to reverse)
Length = len (text)
For i=0 to length -1
Reversedtext = reversed text & mid (text,(length i),1)
Next I
Msg box reversedtext
selection.cells.value = reversed text
End sub



COVERT TO PROPER CASE:

A program in Excel VBA that converts text to proper case. That
is, the first letter in each word in uppercase, and all other letters
in lowercase.

Code:
Sub
Dim rng as range, cells as range
Set rng = selection
For each cell in rng
If not cell.hasformula then
Cell. Value = worksheet function.proper(cell. value)
End if
Next cell
End sub
THANK YOU.