VBA and Macro Creation (Using Excel) : Submitted By: Rohit Bhardwaj Cse 4 Yr. Roll No:173/11 Univ. Reg.:1282807
VBA and Macro Creation (Using Excel) : Submitted By: Rohit Bhardwaj Cse 4 Yr. Roll No:173/11 Univ. Reg.:1282807
(using Excel)
Submitted By:
Rohit Bhardwaj
CSE 4th yr.
Roll No:173/11
Univ.
Object-Oriented Programming
Creating Macros with VBA
OBJECT ORIENTED
PROGRAMMING:
MS-VBA
class Sharon
Attributes: height, 3Dbodyshape, faceimage, currentlocation,
currentlove, pastexperienceslog
Methods: imitateHuman, fallInLove(person),
doSecretMission(password)
You then declare a new object and can subsequently assign its
attribute value and invoke its methods.
Standard Coding
Macro Recording
The Macro approach records a series of mouse-clicks and keyboard
strokes.
Problem Denition
We have data spanning 3 columns and 13 rows (C6:F18).
Data should be stored in the given range in order to use it for other application.
Unfortunately, there are some errors in the data entry process, so some of the rows are shifted
to the right by one column.
Problem Denition
To see the VBA code generated by your actions, go into VBA (Alt+F11) and
then near the upper left of the window expand the Modules folder and
double-click on Module1.
Subroutines
Notice that the code starts with Sub command and ends with End Sub command.
Execution of Subroutines
(The first three green lines following each are simply comments/documentation.)
The real rst line is Range("D8:F8").Select, and it selects the cells we want to select.
Finally, the last line, ActiveSheet.Paste, pastes the cut values to the selected cell of the
worksheet.
CODE BLOCK
End Sub
Data Type
Description
Booleanpart declares
As Integer
data
type ofvalue.
our input variable.
Holds eitherthe
TRUE
or FALSE
Integer
Integers between -32000 and 32000
Common Data Types:
Double precision numbers, can hold fractional numbers (The
Double
range of double number is very large)
A set of characters (like sentences)
String
End Sub
Shifting Repeatedly
CODE BLOCK
Next varName
Shifting Repeatedly
Sub ShifterLoop
End Sub
Variable Declaration
The subroutine ShiftOneColumn requires an integer input.
We decided to introduce something called RowNum which stores
a row number.
We want the data type for RowNum to be Integer. The VBA interpreter will attempt to
guess (scary!) the data type if you forget to declare your variables.
Shifting Repeatedly
So, we add a line before the loop where we declare our variable
RowNum.
Sub ShifterLoop
Dim RowNum As Integer
For RowNum=6 To 18 Step 1
Call ShiftOneColumn(RowNum)
Next RowNum
Lets go back to our task: Creating a function to check the rst column.
We name our function as CheckColOne.
What is our input argument? Row Number.
Function to check the rst column
Function CheckColOne (RowNum as Integer)
If Cells(RowNum,3).Value="" Then
CheckColOne=1
Else
CheckColOne=0
Sub ShifterLoop ()
Dim RowNum As Integer
For RowNum=6 To 18 Step 1
If CheckColOne(RowNum) Then
Call ShiftOneColumn(RowNum)
End If
Next RowNum
End Sub