Date post: | 22-Jul-2016 |
Category: |
Documents |
Upload: | ajay-kumar-tungala |
View: | 5 times |
Download: | 0 times |
VBA(VISUAL BASIC APPLICATION)
4TH 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 LoopEnd 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 IEnd 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 LoopEnd 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 rangeSet rng = range (“a1:a3”)‘set rng = selectionFor each cell in rngCell. Value = cell. Value * cell. valueNext cellEnd 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 integerFor I = 1 to 5Cells(I,2).value = worksheetfunction.randbetween(0,1000)Next I For I = 1 to 5 For j = i+1 to 5If cells (j,2).value < cells (I,2).value thenTempstring = cells(I,1).valueCells(j,1).value = cells(j,1).valueCells (j,1).value= tempintegerTempinteger = cells(I,2).valueCells(I,2).value=cell(j,2).valueCells(j,2).value= tempintegerEnd ifNext jNext IEnd 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 integerCells(1,2) .value=cells(1,1).valueUnique numbers = 1Toad = trueFor i=2 to 10For j=1 to uniquenumberIf cells(I,1).value=cells(j,2).value thenToad = falseEnd ifNext jIf to add=true thenCells (unique numbers +1,2).value=cells(I,1).valueUniquenumbers = uniquenumbers+1End if Toad = trueNext IEnd sub
SORT NUMBERS:
• Below we will look at a program in Excel VBA that sorts numbers
• Code:
SubDim I as integer, j as integer, temp as integer, rng as rangeSet rng = range(“a1”).current regionFor i=1 to rng. CountFor j=i+1 to rng. CountIf rng. Cells(j)<rng. Cells(i) then‘swap numberTemp = rng. Cells(i)Rng. Cells(i)=rng. Cells(j)Ran. Cells(j)=tempEnd ifNext jNext IEnd 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 integerFor i=1 to 6 step 3 Cells(I,1).value=100Next IFor j=8 to 3 step -2Cells(6,j).value = 50Next jEnd 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:
SubRange(“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:SubDim I as integer, j as integerFor I = 1 to 2 For j = 1 to 5Cells(I,j).value=worksheet function . Eand between (2,100)Next jNext 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 rangeset rng = selectionFor each cell in rngOn error goto invalid value:Cell. Value= sqr (cell . value)Next cellExit subInvalid value:Select case err. NumberCase is = 5Ms box “can’t calculate square root of negative number at cell” & cell. AddressCase is =13Msg box “can’t calculate square root of text at cell ”&cell. AddressEnd selectResume nextEnd 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 rangeSet rng = selectionFor each cell in rngOn error resume nextCell. Value = sqr (cell. Value)Next cellEnd sub
INTERRUPT A MACRO:
• interrupt a macro in Excel at any time by pressing Esc or Ctrl + Break.
• Code:
Sub ‘Application.enablecancelkey = xl disabledDim x as longX=5Do while x>2X = x+1Loop‘Application.enablecancelkey = xl interruptEnd sub
STRING MANIPULATION
• Joint string:Sub Dim text1 as string, text2 as stringText1 = “hum”Text2 = “tum”Msg box text1 & “ “ & text2End sub
• Left:SubDim text as stringText = “sree ram”Msg box left (text,4)
• Right:SubMsg box right (“sree krishna”)End sub
SEPERATE STRING:
• Code:
SubDim fullname as string, commaposition as integer, I as integerFor I= 2 to 7Fullname = cells(I,1).valueCommaposition instr (fullname, “1”)Cells(I,2).value= mid (fullname, commaposition +2)Cells(I,3).value= left (fullname, commaposition -1)Next IEnd sub
REVERSE STRING:
• A program in Excel VBA that can reverse strings.
• Code:
SubDim text as string, reversed text as string, length as integer, I as integerText = input box(“enter the text you want to reverse”)Length = len (text)For i=0 to length -1Reversedtext = reversed text & mid (text,(length –i),1)Next IMsg box reversedtext‘selection.cells.value = reversed textEnd 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:SubDim rng as range, cells as rangeSet rng = selectionFor each cell in rngIf not cell.hasformula thenCell. Value = worksheet function.proper(cell. value)End ifNext cellEnd sub
THANK YOU.