SlideShare ist ein Scribd-Unternehmen logo
1 von 15
What we learned in                             1 st    Session
1. VBA is EVENT DRIVEN LANGUAGE
2. OBJECT-BASED LANGUAGE
3. What is IDE ( Integrated Development Environmetn)
4. How to go to VBA IDE
5. Various Features of IDE
    - VBE Menu
    - Project Window
    - Code Window
    - Properties Window
    - Immediate Window
6. Option Explicit
    - Why Option Explicit
    - How to set Auto for Option Explicit
7. Code Modules
    - General Purpose Code Modules
    - Work Book Code Modules
    - Work Sheet Code Modules



                                            Ameetz.com
How to Create Modules
To create a new general purpose
module you can use the Insert -
>Module option from the VBE menu
toolbar:

After inserting the first new general purpose module, you’ll
have a new entry in the VBAProject window. Now you have
a new collection called Modules and the new module you
just created will be listed as one of the members of the
collection.



Any more modules you add will be listed as new members of the collection. You can
double-click on any of them and view its contents in the Code Window. The one
property that a module has is its Name. You can give more meaningful names than
just “Module1” or “Module2” by changing the name in the properties window while
the module is the current object of affection active object.


                                                    Ameetz.com
How to Create Modules..contd…

There’s no rule for naming modules except that they must start with an alpha
character and can't contain certain special characters, I like to give mine names
that start with “atz” (for ameetz) followed by some description of the use of the
code within them. Examples might be names like:

atzUtilities , atzDeclarations , atzOps_Operations

There is no practical limit to the number of modules. The maximum size of any
individual module is 64K. However we can insert lot of coding into one module
hence there will be memory limit issues.




                                                     Ameetz.com
How to Create Modules..contd…



                                                       Workbook Events
                                                       Just what are the workbook
                                                       events? You can get a
                                                       complete list of them from
                                                       the code window while the
There is one and only one code module per workbook     Workbook Code module
that is associated with Workbook Event handling. At    content is displayed: You
the technical level, this module, along with the       can display that content by
worksheet event handling modules are Class Modules.    double-clicking         the
That need not concern you. Just be aware that if you   ThisWorkbook object in the
want to do any coding that deals with events that      VBAProject window. You’ll
occur at the workbook level, you do it in this         get a display similar to
module.                                                above picture



                                               Ameetz.com
How to Create Modules..contd…
If you use the left pull-down of the workbook’s code module you’ll see that there is
a specific Workbook entry. If you choose that item, the VBE will automatically insert
a stub (just the beginning declaration and end statement for the procedure) for the
Workbook_Open() event.                     You can delete that entry if you don’t need
                                          code to deal with something you want to
                                          happen when the workbook is opened.

                                          With your cursor placed inside of any
                                          Workbook related procedure, even just a
                                          stub, you can then use the pull-down on
                                          the right to find a list of all the available
                                          event handlers for the workbook, as shown
                                          below :

                                                        NOTE: If the cursor is not in a
                                                        workbook       event       handling
                                                        procedure, the list on the right will
                                                        show you a list of non-workbook
                                                        event procedure names in the
                                                        module.

                                                  Ameetz.com
How to Create Modules..contd…
WORKSHEET CODE MODULES
There is one and only one code module per worksheet that is associated with Worksheet
Event handling. However, each sheet has its very own code module that is separate and
distinct from all of the others even though they may all have event for a given event for
those worksheets. At the technical level, this module, just like the event handling module for
the workbook are Class Modules. Remember that if you want to do any coding that deals
with events that occur at the worksheet level, you do it in these modules.

Worksheet Events
Just what are the worksheet events? You can get a complete list of them from the code
window while any Worksheet Code module content is displayed: You can display that
content by double-clicking any worksheet object in the VBAProject window. The code
module for that sheet will be displayed as given below:




                                                        Ameetz.com
For worksheets, when you choose the Worksheet item in the left pulldown list, the
default event is the Worksheet_SelectionChange(ByVal Target As Range) event.
This even triggers any time you make a new selection on the sheet – such as
simply moving to another cell. The new cell becomes the selection, and thus you’ve
had a selection change.
As with the Workbook events, you can now get a complete list of Worksheet Events
available to be programmed against by using the right-side pull-down (indicated as
“(Declarations)”). This list is much shorter than the Workbook’s list, as there are 9
events (from Excel 2003) provide considerable versatility in dealing with
worksheets. Out of the list, the Change() event is probably the one that most often
has code associated with it. A Change() occurs when a user alters the contents
(value) of one or more cells on the sheet. Worksheet formula recalculations don’t
trigger this event, but they do trigger the Calculate() event.




                                                   Ameetz.com
The ‘Target’ and ‘Cancel’ Objects

Often in worksheet event stubs provided by the VBE you will see reference to two
special objects (sometimes more or others also): Cancel and/or Target. Target
represents the Range [which is an object that represents a single cell, a group of
cells, one or more rows and/or one or more columns] that is active at the time the
event took place. Think of Target as the actual object itself. Anything you do to
Target is done to the actual Range that it represents. Changes made to Target will
appear on the sheet itself.
The Cancel object is a Boolean type object. A Boolean object can only have one
of two conditions assigned to it: TRUE or FALSE. By default a Boolean object is
FALSE (and has a numeric value of zero). If your code sets Cancel = TRUE then
the underlying event action is cancelled: the DoubleClick never takes place or the
RightClick never gets completed. These are handy events to use to take very
special actions with – you can have someone double-click in a cell (and set
Cancel = True) to begin a series of events unique to that cell. A real world example
of this type of thing in one application I developed is that in a data area matrix that
has dates in the top row, a double-click on a date causes all rows with an empty
cell in that column to become hidden: a kind of auto filter based on empty cells for
that one column.


                                                     Ameetz.com
Procedures: Function and Sub

Code modules contain code, and that code is placed into procedures, and
procedures fall into two categories: Sub (or subroutines) and Function(s).

FUNCTIONS
The difference between a Sub and a Function is simply that a function can return
a value to the procedure that called it. That procedure can be another
Function, a Sub or even to a worksheet cell. When it is done using a worksheet
formula, the Function is known as a User Defined Function, or UDF. Potentially
all Functions are UDFs.
One other distinction between Functions and Subs is that (generally) Functions
can only affect a single cell in a workbook, while Subs can do their work and
affect almost any aspect of a workbook or worksheet. When it is used as a
UDF, it can only affect the cell that it is called from; it cannot alter the contents of
other cells.




                                                     Ameetz.com
SUBS
Sub procedures are just like Functions, except that they do not return a
value in the same way that a Function does. They can accept
arguments, or not, just like a Function does.
                 VBA Sub Procedure Example




                                             Ameetz.com
VBA Function Procedure Example




The word Macro is slang for the word VBA procedure.
• A procedure is defined as a named group of statements that are run as a unit.
  A statement is simply 1 complete line of code.
• VBA procedures are used to perform tasks such as controlling Excel’s
  environment, communicating with databases, calculating equations, analyzing data etc.




                                                        Ameetz.com
What is a VBA Procedure? Contd..

• A VBA procedure unit or block consists of a procedure statement (Sub or Function) and an
  ending statement with statements in between.

• A VBA procedure are constructed from three types of statements: executable, declaration and
  assignment statements. The statements between a procedure’s declaration and ending statement
  (i.e. Sub and End Sub) perform the procedure's task; what you are trying do.
      •For example, the procedure to the right commands Excel's Sort feature on the active
      worksheet when it is run.

• Procedures are typed and stored in a Module.

• Procedures are executed or run (same meaning) in order to apply their statements. When a
  procedure is run, its statements (i.e. lines) are executed in a top-down line by line fashion
  performing operations. Think of reading a book page. Note that typing a procedure in a module
  does not run it. You must do this after typing it by variety of different methods. Until you run it, it is
  just basically text sitting in a document.

• VBA has two types of procedures: Sub procedures and Function procedures.




                                                                 Ameetz.com
Sub Procedures
Sub procedures are written when you want to command Excel like creating a
chart, analyzing data, coloring cells, copying and pasting data.

Function Procedures
Function procedures are created when you want to make your own custom
worksheet functions or perform a calculation that will be used over and over
again. Note that Sub procedures can also do calculations.

What to Write
So if you want to do a task in Excel, you write a Sub procedure, if you want to
write a custom worksheet function, you write a Function procedure. Are their
grey areas between the two, yes, but do not worry about them when first starting
out




                                               Ameetz.com
In this example, you create an Excel macro
that converts selected formulas to their
current values. Sure, you can do this
without a macro, but it’s a multistep
procedure.
To convert a range of formulas to values,
you normally complete the following steps:
1. Select the range that contains the
formulas to be converted.
2. Copy the range to the Clipboard.
3. Choose Edit Paste Special.
4. Click the Values option button in the
Paste Special dialog box, which
is shown in Figure 2-1.
5. Click OK.
6. Press Esc.
This clears the cut-copy mode indicator (the
moving border) in the worksheet.




                                               Ameetz.com
Here comes the hands-on part. Follow these instructions carefully:
1. Select the range of cells that contains your formulas.
The selection can include both values and formulas. In my case, I chose
the range A1:D10.
2. Choose Tools Macro Record New Macro.
The Record Macro dialog box appears, as shown in Figure 2-3.
3. Enter a name for the macro.
Excel provides a default name, but it’s better to use a more descriptive name.
ConvertFormulas is a good name for this one.


                                1




   No Spaces




                                                  Ameetz.com

Weitere ähnliche Inhalte

Was ist angesagt?

E learning excel vba programming lesson 4
E learning excel vba programming  lesson 4E learning excel vba programming  lesson 4
E learning excel vba programming lesson 4Vijay Perepa
 
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzalHow to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal1995786
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basicsHang Dong
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosNick Weisenberger
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programmingiveytechnologyclub
 
Transforming Power Point Show with VBA
Transforming Power Point Show with VBATransforming Power Point Show with VBA
Transforming Power Point Show with VBADCPS
 
VBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course BrochureVBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course Brochurer1c1
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in Indiaibinstitute0
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2Dan D'Urso
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA NotesYang Ye
 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application Raghu nath
 
Advance MS Excel
Advance MS ExcelAdvance MS Excel
Advance MS Excelacute23
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1guest38bf
 
Introduction To Excel 2007 Macros
Introduction To Excel 2007 MacrosIntroduction To Excel 2007 Macros
Introduction To Excel 2007 MacrosExcel
 

Was ist angesagt? (20)

Excel vba
Excel vbaExcel vba
Excel vba
 
Vba Class Level 3
Vba Class Level 3Vba Class Level 3
Vba Class Level 3
 
E learning excel vba programming lesson 4
E learning excel vba programming  lesson 4E learning excel vba programming  lesson 4
E learning excel vba programming lesson 4
 
How to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzalHow to apply a formula and macro in excel......by irfan afzal
How to apply a formula and macro in excel......by irfan afzal
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
 
Vba part 1
Vba part 1Vba part 1
Vba part 1
 
MACROS excel
MACROS excelMACROS excel
MACROS excel
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programming
 
Transforming Power Point Show with VBA
Transforming Power Point Show with VBATransforming Power Point Show with VBA
Transforming Power Point Show with VBA
 
VBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course BrochureVBA Classes from Chandoo.org - Course Brochure
VBA Classes from Chandoo.org - Course Brochure
 
Online Advance Excel & VBA Training in India
 Online Advance Excel & VBA Training in India Online Advance Excel & VBA Training in India
Online Advance Excel & VBA Training in India
 
AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2AVB201.2 Microsoft Access VBA Module 2
AVB201.2 Microsoft Access VBA Module 2
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA Notes
 
Visual Basics for Application
Visual Basics for Application Visual Basics for Application
Visual Basics for Application
 
Learn Excel Macro
Learn Excel Macro  Learn Excel Macro
Learn Excel Macro
 
Advance MS Excel
Advance MS ExcelAdvance MS Excel
Advance MS Excel
 
AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1AVB201.1 MS Access VBA Module 1
AVB201.1 MS Access VBA Module 1
 
Introduction To Excel 2007 Macros
Introduction To Excel 2007 MacrosIntroduction To Excel 2007 Macros
Introduction To Excel 2007 Macros
 

Ähnlich wie E learning excel vba programming lesson 2

Tutorials on Macro
Tutorials on MacroTutorials on Macro
Tutorials on MacroAnurag Deb
 
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET
Unit IV-Checkboxes    and   Radio Buttons in VB.Net in VB.NET Unit IV-Checkboxes    and   Radio Buttons in VB.Net in VB.NET
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET Ujwala Junghare
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013Raghu nath
 
Automation Of Reporting And Alerting
Automation Of Reporting And AlertingAutomation Of Reporting And Alerting
Automation Of Reporting And AlertingSean Durocher
 
Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vbarjg_vijay
 
Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkAkhil Mittal
 
Financial modeling sameh aljabli lecture 6
Financial modeling   sameh aljabli   lecture 6Financial modeling   sameh aljabli   lecture 6
Financial modeling sameh aljabli lecture 6Sameh Algabli
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsRuth Marvin
 
Visual basic concepts
Visual basic conceptsVisual basic concepts
Visual basic conceptsmelody77776
 
AVB201.1 Microsoft Access VBA Module 1
AVB201.1 Microsoft Access VBA Module 1AVB201.1 Microsoft Access VBA Module 1
AVB201.1 Microsoft Access VBA Module 1Dan D'Urso
 

Ähnlich wie E learning excel vba programming lesson 2 (20)

Tutorials on Macro
Tutorials on MacroTutorials on Macro
Tutorials on Macro
 
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET
Unit IV-Checkboxes    and   Radio Buttons in VB.Net in VB.NET Unit IV-Checkboxes    and   Radio Buttons in VB.Net in VB.NET
Unit IV-Checkboxes and Radio Buttons in VB.Net in VB.NET
 
VBA
VBAVBA
VBA
 
Adv excel® 2013
Adv excel® 2013Adv excel® 2013
Adv excel® 2013
 
Excel® 2013
Excel® 2013Excel® 2013
Excel® 2013
 
Excel 2013
Excel 2013Excel 2013
Excel 2013
 
Automation Of Reporting And Alerting
Automation Of Reporting And AlertingAutomation Of Reporting And Alerting
Automation Of Reporting And Alerting
 
iOS Development (Part 2)
iOS Development (Part 2)iOS Development (Part 2)
iOS Development (Part 2)
 
Autocad excel vba
Autocad excel vbaAutocad excel vba
Autocad excel vba
 
Generic Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity FrameworkGeneric Repository Pattern in MVC3 Application with Entity Framework
Generic Repository Pattern in MVC3 Application with Entity Framework
 
Class 1 blog
Class 1 blogClass 1 blog
Class 1 blog
 
Advexcellp
AdvexcellpAdvexcellp
Advexcellp
 
E views tutorial
E views tutorialE views tutorial
E views tutorial
 
Financial modeling sameh aljabli lecture 6
Financial modeling   sameh aljabli   lecture 6Financial modeling   sameh aljabli   lecture 6
Financial modeling sameh aljabli lecture 6
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
Form part1
Form part1Form part1
Form part1
 
008.module
008.module008.module
008.module
 
Visual basic concepts
Visual basic conceptsVisual basic concepts
Visual basic concepts
 
AVB201.1 Microsoft Access VBA Module 1
AVB201.1 Microsoft Access VBA Module 1AVB201.1 Microsoft Access VBA Module 1
AVB201.1 Microsoft Access VBA Module 1
 
Automating SolidWorks with Excel
Automating SolidWorks with ExcelAutomating SolidWorks with Excel
Automating SolidWorks with Excel
 

Mehr von Vijay Perepa

Access essential training framework
Access essential training   frameworkAccess essential training   framework
Access essential training frameworkVijay Perepa
 
Add picture to comment
Add picture to commentAdd picture to comment
Add picture to commentVijay Perepa
 
Excel short cut series function keys
Excel short cut series   function keysExcel short cut series   function keys
Excel short cut series function keysVijay Perepa
 
PowerPoint Info-Graphics
PowerPoint Info-GraphicsPowerPoint Info-Graphics
PowerPoint Info-GraphicsVijay Perepa
 
Types of charts in Excel and How to use them
Types of charts in Excel and How to use themTypes of charts in Excel and How to use them
Types of charts in Excel and How to use themVijay Perepa
 
Pivot table essential learning 1
Pivot table   essential learning 1Pivot table   essential learning 1
Pivot table essential learning 1Vijay Perepa
 
Pivot table essential learning 2
Pivot table   essential learning 2Pivot table   essential learning 2
Pivot table essential learning 2Vijay Perepa
 
Modeling in microsoft excel
Modeling in microsoft excelModeling in microsoft excel
Modeling in microsoft excelVijay Perepa
 
Understanding excel’s error values
Understanding excel’s error valuesUnderstanding excel’s error values
Understanding excel’s error valuesVijay Perepa
 
Excel training commands not in ribbon
Excel training   commands not in ribbonExcel training   commands not in ribbon
Excel training commands not in ribbonVijay Perepa
 
E learning excel vba programming lesson 5
E learning excel vba programming  lesson 5E learning excel vba programming  lesson 5
E learning excel vba programming lesson 5Vijay Perepa
 
Power point short cut keys
Power point short cut keysPower point short cut keys
Power point short cut keysVijay Perepa
 
Understanding excel’s error values
Understanding excel’s error valuesUnderstanding excel’s error values
Understanding excel’s error valuesVijay Perepa
 
Excel information formulae par ii ameet z academy
Excel information formulae par ii  ameet z academyExcel information formulae par ii  ameet z academy
Excel information formulae par ii ameet z academyVijay Perepa
 
Excel information formulae ameet z academy
Excel information formulae   ameet z academyExcel information formulae   ameet z academy
Excel information formulae ameet z academyVijay Perepa
 
Excel text formulae ameet z academy
Excel text formulae   ameet z academyExcel text formulae   ameet z academy
Excel text formulae ameet z academyVijay Perepa
 
E-Learning from Ameetz (ISERROR formula)
E-Learning from Ameetz (ISERROR formula)E-Learning from Ameetz (ISERROR formula)
E-Learning from Ameetz (ISERROR formula)Vijay Perepa
 
E learning excel short cut keys
E learning excel short cut keysE learning excel short cut keys
E learning excel short cut keysVijay Perepa
 

Mehr von Vijay Perepa (20)

Access essential training framework
Access essential training   frameworkAccess essential training   framework
Access essential training framework
 
Add picture to comment
Add picture to commentAdd picture to comment
Add picture to comment
 
Excel short cut series function keys
Excel short cut series   function keysExcel short cut series   function keys
Excel short cut series function keys
 
PowerPoint Info-Graphics
PowerPoint Info-GraphicsPowerPoint Info-Graphics
PowerPoint Info-Graphics
 
Types of charts in Excel and How to use them
Types of charts in Excel and How to use themTypes of charts in Excel and How to use them
Types of charts in Excel and How to use them
 
Pivot table essential learning 1
Pivot table   essential learning 1Pivot table   essential learning 1
Pivot table essential learning 1
 
Pivot table essential learning 2
Pivot table   essential learning 2Pivot table   essential learning 2
Pivot table essential learning 2
 
Modeling in microsoft excel
Modeling in microsoft excelModeling in microsoft excel
Modeling in microsoft excel
 
Understanding excel’s error values
Understanding excel’s error valuesUnderstanding excel’s error values
Understanding excel’s error values
 
Excel training commands not in ribbon
Excel training   commands not in ribbonExcel training   commands not in ribbon
Excel training commands not in ribbon
 
E learning excel vba programming lesson 5
E learning excel vba programming  lesson 5E learning excel vba programming  lesson 5
E learning excel vba programming lesson 5
 
Pivot table
Pivot tablePivot table
Pivot table
 
Power point short cut keys
Power point short cut keysPower point short cut keys
Power point short cut keys
 
Understanding excel’s error values
Understanding excel’s error valuesUnderstanding excel’s error values
Understanding excel’s error values
 
Excel information formulae par ii ameet z academy
Excel information formulae par ii  ameet z academyExcel information formulae par ii  ameet z academy
Excel information formulae par ii ameet z academy
 
Excel information formulae ameet z academy
Excel information formulae   ameet z academyExcel information formulae   ameet z academy
Excel information formulae ameet z academy
 
Excel text formulae ameet z academy
Excel text formulae   ameet z academyExcel text formulae   ameet z academy
Excel text formulae ameet z academy
 
E-Learning from Ameetz (ISERROR formula)
E-Learning from Ameetz (ISERROR formula)E-Learning from Ameetz (ISERROR formula)
E-Learning from Ameetz (ISERROR formula)
 
E learning excel short cut keys
E learning excel short cut keysE learning excel short cut keys
E learning excel short cut keys
 
Sumif () ppt
Sumif () pptSumif () ppt
Sumif () ppt
 

Kürzlich hochgeladen

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesFatimaKhan178732
 

Kürzlich hochgeladen (20)

BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Separation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and ActinidesSeparation of Lanthanides/ Lanthanides and Actinides
Separation of Lanthanides/ Lanthanides and Actinides
 

E learning excel vba programming lesson 2

  • 1. What we learned in 1 st Session 1. VBA is EVENT DRIVEN LANGUAGE 2. OBJECT-BASED LANGUAGE 3. What is IDE ( Integrated Development Environmetn) 4. How to go to VBA IDE 5. Various Features of IDE - VBE Menu - Project Window - Code Window - Properties Window - Immediate Window 6. Option Explicit - Why Option Explicit - How to set Auto for Option Explicit 7. Code Modules - General Purpose Code Modules - Work Book Code Modules - Work Sheet Code Modules Ameetz.com
  • 2. How to Create Modules To create a new general purpose module you can use the Insert - >Module option from the VBE menu toolbar: After inserting the first new general purpose module, you’ll have a new entry in the VBAProject window. Now you have a new collection called Modules and the new module you just created will be listed as one of the members of the collection. Any more modules you add will be listed as new members of the collection. You can double-click on any of them and view its contents in the Code Window. The one property that a module has is its Name. You can give more meaningful names than just “Module1” or “Module2” by changing the name in the properties window while the module is the current object of affection active object. Ameetz.com
  • 3. How to Create Modules..contd… There’s no rule for naming modules except that they must start with an alpha character and can't contain certain special characters, I like to give mine names that start with “atz” (for ameetz) followed by some description of the use of the code within them. Examples might be names like: atzUtilities , atzDeclarations , atzOps_Operations There is no practical limit to the number of modules. The maximum size of any individual module is 64K. However we can insert lot of coding into one module hence there will be memory limit issues. Ameetz.com
  • 4. How to Create Modules..contd… Workbook Events Just what are the workbook events? You can get a complete list of them from the code window while the There is one and only one code module per workbook Workbook Code module that is associated with Workbook Event handling. At content is displayed: You the technical level, this module, along with the can display that content by worksheet event handling modules are Class Modules. double-clicking the That need not concern you. Just be aware that if you ThisWorkbook object in the want to do any coding that deals with events that VBAProject window. You’ll occur at the workbook level, you do it in this get a display similar to module. above picture Ameetz.com
  • 5. How to Create Modules..contd… If you use the left pull-down of the workbook’s code module you’ll see that there is a specific Workbook entry. If you choose that item, the VBE will automatically insert a stub (just the beginning declaration and end statement for the procedure) for the Workbook_Open() event. You can delete that entry if you don’t need code to deal with something you want to happen when the workbook is opened. With your cursor placed inside of any Workbook related procedure, even just a stub, you can then use the pull-down on the right to find a list of all the available event handlers for the workbook, as shown below : NOTE: If the cursor is not in a workbook event handling procedure, the list on the right will show you a list of non-workbook event procedure names in the module. Ameetz.com
  • 6. How to Create Modules..contd… WORKSHEET CODE MODULES There is one and only one code module per worksheet that is associated with Worksheet Event handling. However, each sheet has its very own code module that is separate and distinct from all of the others even though they may all have event for a given event for those worksheets. At the technical level, this module, just like the event handling module for the workbook are Class Modules. Remember that if you want to do any coding that deals with events that occur at the worksheet level, you do it in these modules. Worksheet Events Just what are the worksheet events? You can get a complete list of them from the code window while any Worksheet Code module content is displayed: You can display that content by double-clicking any worksheet object in the VBAProject window. The code module for that sheet will be displayed as given below: Ameetz.com
  • 7. For worksheets, when you choose the Worksheet item in the left pulldown list, the default event is the Worksheet_SelectionChange(ByVal Target As Range) event. This even triggers any time you make a new selection on the sheet – such as simply moving to another cell. The new cell becomes the selection, and thus you’ve had a selection change. As with the Workbook events, you can now get a complete list of Worksheet Events available to be programmed against by using the right-side pull-down (indicated as “(Declarations)”). This list is much shorter than the Workbook’s list, as there are 9 events (from Excel 2003) provide considerable versatility in dealing with worksheets. Out of the list, the Change() event is probably the one that most often has code associated with it. A Change() occurs when a user alters the contents (value) of one or more cells on the sheet. Worksheet formula recalculations don’t trigger this event, but they do trigger the Calculate() event. Ameetz.com
  • 8. The ‘Target’ and ‘Cancel’ Objects Often in worksheet event stubs provided by the VBE you will see reference to two special objects (sometimes more or others also): Cancel and/or Target. Target represents the Range [which is an object that represents a single cell, a group of cells, one or more rows and/or one or more columns] that is active at the time the event took place. Think of Target as the actual object itself. Anything you do to Target is done to the actual Range that it represents. Changes made to Target will appear on the sheet itself. The Cancel object is a Boolean type object. A Boolean object can only have one of two conditions assigned to it: TRUE or FALSE. By default a Boolean object is FALSE (and has a numeric value of zero). If your code sets Cancel = TRUE then the underlying event action is cancelled: the DoubleClick never takes place or the RightClick never gets completed. These are handy events to use to take very special actions with – you can have someone double-click in a cell (and set Cancel = True) to begin a series of events unique to that cell. A real world example of this type of thing in one application I developed is that in a data area matrix that has dates in the top row, a double-click on a date causes all rows with an empty cell in that column to become hidden: a kind of auto filter based on empty cells for that one column. Ameetz.com
  • 9. Procedures: Function and Sub Code modules contain code, and that code is placed into procedures, and procedures fall into two categories: Sub (or subroutines) and Function(s). FUNCTIONS The difference between a Sub and a Function is simply that a function can return a value to the procedure that called it. That procedure can be another Function, a Sub or even to a worksheet cell. When it is done using a worksheet formula, the Function is known as a User Defined Function, or UDF. Potentially all Functions are UDFs. One other distinction between Functions and Subs is that (generally) Functions can only affect a single cell in a workbook, while Subs can do their work and affect almost any aspect of a workbook or worksheet. When it is used as a UDF, it can only affect the cell that it is called from; it cannot alter the contents of other cells. Ameetz.com
  • 10. SUBS Sub procedures are just like Functions, except that they do not return a value in the same way that a Function does. They can accept arguments, or not, just like a Function does. VBA Sub Procedure Example Ameetz.com
  • 11. VBA Function Procedure Example The word Macro is slang for the word VBA procedure. • A procedure is defined as a named group of statements that are run as a unit. A statement is simply 1 complete line of code. • VBA procedures are used to perform tasks such as controlling Excel’s environment, communicating with databases, calculating equations, analyzing data etc. Ameetz.com
  • 12. What is a VBA Procedure? Contd.. • A VBA procedure unit or block consists of a procedure statement (Sub or Function) and an ending statement with statements in between. • A VBA procedure are constructed from three types of statements: executable, declaration and assignment statements. The statements between a procedure’s declaration and ending statement (i.e. Sub and End Sub) perform the procedure's task; what you are trying do. •For example, the procedure to the right commands Excel's Sort feature on the active worksheet when it is run. • Procedures are typed and stored in a Module. • Procedures are executed or run (same meaning) in order to apply their statements. When a procedure is run, its statements (i.e. lines) are executed in a top-down line by line fashion performing operations. Think of reading a book page. Note that typing a procedure in a module does not run it. You must do this after typing it by variety of different methods. Until you run it, it is just basically text sitting in a document. • VBA has two types of procedures: Sub procedures and Function procedures. Ameetz.com
  • 13. Sub Procedures Sub procedures are written when you want to command Excel like creating a chart, analyzing data, coloring cells, copying and pasting data. Function Procedures Function procedures are created when you want to make your own custom worksheet functions or perform a calculation that will be used over and over again. Note that Sub procedures can also do calculations. What to Write So if you want to do a task in Excel, you write a Sub procedure, if you want to write a custom worksheet function, you write a Function procedure. Are their grey areas between the two, yes, but do not worry about them when first starting out Ameetz.com
  • 14. In this example, you create an Excel macro that converts selected formulas to their current values. Sure, you can do this without a macro, but it’s a multistep procedure. To convert a range of formulas to values, you normally complete the following steps: 1. Select the range that contains the formulas to be converted. 2. Copy the range to the Clipboard. 3. Choose Edit Paste Special. 4. Click the Values option button in the Paste Special dialog box, which is shown in Figure 2-1. 5. Click OK. 6. Press Esc. This clears the cut-copy mode indicator (the moving border) in the worksheet. Ameetz.com
  • 15. Here comes the hands-on part. Follow these instructions carefully: 1. Select the range of cells that contains your formulas. The selection can include both values and formulas. In my case, I chose the range A1:D10. 2. Choose Tools Macro Record New Macro. The Record Macro dialog box appears, as shown in Figure 2-3. 3. Enter a name for the macro. Excel provides a default name, but it’s better to use a more descriptive name. ConvertFormulas is a good name for this one. 1 No Spaces Ameetz.com