ms excel – macros using vba by :- amit dhingra. csc proprietary 06/09/2004 page: 2 agenda macro...
TRANSCRIPT
MS Excel – Macros using VBA
By :- Amit Dhingra
CSC Proprietary 06/09/2004 Page: 2
Agenda
• Macro Security
• Recording a Macro
• Running a Macro
• Introduction to VBA
• Writing and debugging code in VBA
• Looping using VBA
CSC Proprietary 06/09/2004 Page: 3
Macro Security
• 4 types• Low
• Medium
• High
• Very High
• Option to choose which macro to run available under the Medium security level
CSC Proprietary 06/09/2004 Page: 4
Recording a Macro
CSC Proprietary 06/09/2004 Page: 5
Running a Macro
• Press “ALT + F8” for the list of Macros
• Press the Shortcut directly for the Macro
CSC Proprietary 06/09/2004 Page: 6
Exercise
S.No Name Marks
1 Amit 56
2 Kamal 67
3 Namrata 68
4 Deepti 59
5 Yogesh 78
A B C
1. Cell D1=Average Marks
2. Cell E1=Total Students
3. Cell F1=Count of Students who have scored above 65
4. Cell D2 to D5 should have either “ABOVE” or “BELOW” based upon the value in cells C2 to C5 as compared to the average in D1.
Record a macro that gives the following output
CSC Proprietary 06/09/2004 Page: 7
Some more Macros
• Working with multiple sheets
• Working with multiple files
CSC Proprietary 06/09/2004 Page: 8
• Create a Macro that gives• The first 5 characters of “Asset Location” (Column F) in
Facility (Column G)
• Fetches the City (Column H) based upon Facility (Column G) from the Facility Code sheet.
e.g.
Exercise
CSC Proprietary 06/09/2004 Page: 9
Introduction to VBA
• Right Click on sheet name - > view code
• Press “ALT”+”F11”
• Write or Edit the code for macro in the modules under project explorer
CSC Proprietary 06/09/2004 Page: 10
Writing Code in VBA
CSC Proprietary 06/09/2004 Page: 11
Conditional Statements
• If…. Then…..End if
• Select Case….. End Select
CSC Proprietary 06/09/2004 Page: 12
Exercise
• Display the Full Name as • Last Name , First Name - If Last name is not blank
• First Name - If Last name is blank
For the following data:SNo FirstName LastName Full Name1 Amit Gupta ?2 Deepak ?3 Ajay Sharma ?4 Payal Mehta ?5 Deepak ?
CSC Proprietary 06/09/2004 Page: 13
Looping
• While……Wend
• Do……….Loop While
• For………Next
CSC Proprietary 06/09/2004 Page: 14
Exercise
• Display the sum of all the data of Column A in the cell F1.
– Do not use the Sum Formula…. Use the While / Do or For loop.
– The number of cells which have the data in Column A is not fixed.
CSC Proprietary 06/09/2004 Page: 15
Thanks
Contact DetailsAmit DhingraSH-103, A-91, Sector-2, Noida0120-3064000 ext. 3648Nortel Ext. 3648