SlideShare une entreprise Scribd logo
1  sur  34
EXCEL VBA
PROGRAMMING
PART 1
MORTEZA NOSHAD
MSPRO.NOSHAD@GMAIL.COM
0912 - 84 98 775
WHAT CAN YOU DO WITH VBA?
Inserting a bunch of text
Automating a task you perform frequently
Automating repetitive operations
Creating a custom command
Creating a custom button
Developing new worksheet functions
Creating custom add-ins for Excel
Creating complete, macro-driven applications
ADVANTAGES AND DISADVANTAGES OF
VBA
Excel always executes the task in exactly the same way. (In most
cases, consistency is a good thing.)
Excel performs the task much faster than you can do it manually
(unless, of course, you’re Clark Kent).you’re a good macro programmer, Excel always performs the
task without errors (which probably can’t be said about you or
me)If you set things up properly, someone who doesn’t know
anything about Excel can perform the task
You can do things in Excel that are otherwise impossible —
which can make you a very popular person around the officeFor long, time-consuming tasks, you don’t have to sit in front of
your computer and get bored. Excel does the work, while you
hang out at the water cooler
VBA ADVANTAGES
ADVANTAGES AND DISADVANTAGES OF
VBA
You have to know how to write programs in VBA (but that’s why
you bought this book, right?). Fortunately, it’s not as difficult as
you might expectOther people who need to use your VBA programs must have
their own copies of Excel. It would be nice if you could press a
button that transforms your Excel/VBA application into a stand-
alone program, but that isn’t possible (and probably never will
be)
Sometimes, things go wrong. In other words, you can’t blindly
assume that your VBA program will always work correctly under
all circumstances. Welcome to the world of debugging and, if
others are using your macros, technical support
VBA is a moving target. As you know, Microsoft is continually
upgrading Excel. Even though Microsoft puts great effort into
compatibility between versions, you may discover that the VBA
code you’ve written doesn’t work properly with older versions or
VBA DISADVANTAGES
VBA IN A NUTSHELL
You perform actions in VBA by writing (or recording) code in a
VBA module
A VBA module consists of Sub procedures
A VBA module can also have Function procedures
VBA manipulates objects
Objects are arranged in a hierarchy
Objects of the same type form a collection
You refer to an object by specifying its position in the object
hierarchy, using a dot (a.k.a., a period) as a separator
If you omit specific references, Excel uses the active objects
Objects have properties
VBA IN A NUTSHELL
You refer to a property of an object by combining the object
name with the property name, separated by a dot
You can assign values to variables
Objects have methods
You specify a method by combining the object with the method,
separated by a dot
VBA includes all the constructs of modern programming
languages, including variables, arrays, and looping
MACRO
Create your first macro
Use relative references
Macro shortcut key-Place macro
Examining macro
How Excel Executes Statements
Saving workbooks that contain macros
Understanding macro security
VISUAL BASIC EDITOR
Working with the Project Explorer
Working with a Code Window
Getting VBA code into a module
Enter the code directly.
Use the Excel macro recorder to record your actions and convert
them to VBA code
Copy the code from one module and paste it into another.
Customizing the VBA Environment
SUBS VERSUS FUNCTIONS
A Sub procedure is a group of VBA
statements that performs an action (or
actions) with Excel.
A Function procedure is a group of VBA
statements that performs a calculation and
returns a single value.
NAMING SUBS AND FUNCTIONS
You can use letters, numbers, and some punctuation characters, but the first
character must be a letter.
You can’t use any spaces or periods in the name.
VBA does not distinguish between uppercase and lowercase letters.
You can’t embed any of the following characters in a name: #, $, %, &, @,^, *,
or !.
If you write a Function procedure for use in a formula, make sure the name
does not look like a cell address (for example, AC12).
Names can be no longer than 255 characters. (Of course, you would never
make a procedure name this long.)
Ideally, a procedure’s name should describe the routine’s purpose. A good
practice is to create a name by combining a verb and a noun — for example,
ProcessData, PrintReport, Sort_Array, or CheckFilename
EXCECUTING SUB PROCEDURES
With the Run➪Run sub/UserForm command (in the VBE) & F5 key.
From Excel’s Macro dialog box. You open this box by choosing
Developer➪Code➪Macros). Or you can press the Alt+F8 shortcut key. require
an argument.
Using the Ctrl+key shortcut assigned to the Sub procedure
Clicking a button or a shape on a worksheet
From another Sub procedure that you write
From a button on the Quick Access Toolbar
From a custom item on the ribbon you develop
Automatically, when you open or close a workbook
When an event occurs
From the Immediate window in the VBE
EXECUTING FUNCTION PROCEDURES
By calling the function from another Sub procedure or
Function procedure
By using the function in a worksheet formula

COMMENTS
A comment is the simplest type of VBA statement.
Because VBA ignores these statements, they can consist
of anything you want. You can insert a comment to
remind yourself why you did something or to clarify
some particularly elegant code you wrote. Use
comments liberally and extensively to describe what
the code does (which isn’t always obvious by reading
the code itself). Often, code that makes perfect sense
today mystifies you tomorrow
COMMENTS
When testing a procedure, you may want to remove
some statements temporarily. Rather than delete the
statements, you can convert them to comments. Then
when testing is completed, convert the comments back
to statements. In the VBE, choose view➪Toolbars➪Edit
to display the Edit toolbar. To convert a block of
statements to comments, select the statements and
click the Comment Block button. To remove the
apostrophes, select the statements and click the
COMMENTS
• The following tips can help you make effective use of
comments:
• Briefly describe the purpose of each Sub or Function
procedure
• you write.
• Use comments to keep track of changes you make to a
procedure.
• Use a comment to indicate that you’re using a function
or a construct
• in an unusual or nonstandard manner.
• Use comments to describe the variables you use,
especially if you don’t use meaningful variable names.
• Use a comment to describe any workarounds you
develop to overcome bugs in Excel.
UNDERSTANDING VARIABLES
A variable is simply a named storage location in your
computer’s memory. You have lots of flexibility in
naming your variables, so make the variable names as
descriptive as possible. You assign a value to a variable
by using the equal sign operator.You can use letters, numbers, and some punctuation characters,
but the first character must be a letter.
You cannot use any spaces or periods in a variable name.
VBA does not distinguish between uppercase and lowercase
letters.
You cannot use the following characters in a variable name: #, $,
%, &, or !.
Variable names can be no longer than 255 characters.
UNDERSTANDING VARIABLES
wear yourself out typing the entire name of a
variable. Just type the first two or three characters and
then hit Control+Space. The VBE will either complete the
entry for you or — if the choice is ambiguous — show
you a pick list to select from. In fact, this slick trick
works with reserved words too.
has many reserved words that you can’t use for
variable names or procedure names. These include
words such as Sub, Dim, With, End, and For. If you
attempt to use one of these words as a variable, you may
get a compile error (your code won’t run). So, if an
assignment statement produces an error message,
double-check and make sure that the variable name
WHAT ARE VBA’S DATA TYPES?
DECLARING AND SCOPING VARIABLES
WORKING WITH CONSTANTS
WORKING WITH OPERATORS
WORKING WITH ARRAYS
INTRODUCING THE EXCEL OBJECT MODEL
INTRODUCING THE EXCEL OBJECT MODEL
1
2
3
COLLECTIONS OF OBJECTS
REFERRING TO OBJECTS
In this case, the number is not in quotation marks. Bottom line? If
you refer to an object by using its name, use quotation marks. If
you refer to an object by using its index number, use a plain
number without quotation marks.
NAVIGATING THROUGH THE HIERARCHY
WORKING WITH RANGE OBJECTS
WORKING WITH RANGE OBJECTS
WORKING WITH RANGE OBJECTS
WORKING WITH RANGE OBJECTS
USING VBA AND WORKSHEET FUNCTIONS
VBA FUNCTIONS THAT DO MORE THAN RETURN A
VALUE

Contenu connexe

Tendances (20)

Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
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
 
VBA - Macro For Ms.Excel
VBA - Macro For Ms.ExcelVBA - Macro For Ms.Excel
VBA - Macro For Ms.Excel
 
MACROS excel
MACROS excelMACROS excel
MACROS excel
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
 
Using macros in microsoft excel part 1
Using macros in microsoft excel   part 1Using macros in microsoft excel   part 1
Using macros in microsoft excel part 1
 
Microsoft Excel - Macros
Microsoft Excel - MacrosMicrosoft Excel - Macros
Microsoft Excel - Macros
 
Vb 6.0 controls
Vb 6.0 controlsVb 6.0 controls
Vb 6.0 controls
 
Excel formulas with-example-narration
Excel formulas with-example-narrationExcel formulas with-example-narration
Excel formulas with-example-narration
 
Excelpresentationdatavalidation
ExcelpresentationdatavalidationExcelpresentationdatavalidation
Excelpresentationdatavalidation
 
Excel macro
Excel macroExcel macro
Excel macro
 
Data validation - Excel
Data validation - ExcelData validation - Excel
Data validation - Excel
 
Excel formulas tf-jul1605
Excel formulas tf-jul1605Excel formulas tf-jul1605
Excel formulas tf-jul1605
 
Intermediate Excel
Intermediate Excel Intermediate Excel
Intermediate Excel
 
Vb basics
Vb basicsVb basics
Vb basics
 
Basic excel training
Basic excel trainingBasic excel training
Basic excel training
 
Microsoft Excel Tutorial
Microsoft Excel TutorialMicrosoft Excel Tutorial
Microsoft Excel Tutorial
 
50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks 50 MS Excel Tips and Tricks
50 MS Excel Tips and Tricks
 
Excel for beginner
Excel for beginnerExcel for beginner
Excel for beginner
 
Visual basic ppt for tutorials computer
Visual basic ppt for tutorials computerVisual basic ppt for tutorials computer
Visual basic ppt for tutorials computer
 

En vedette

Why Is Excel VBA So Important For Banks ?
Why Is Excel VBA So Important For Banks ?Why Is Excel VBA So Important For Banks ?
Why Is Excel VBA So Important For Banks ?Jack Tellington
 
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
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programmingiveytechnologyclub
 
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...John Head
 
Excel VbA for Technical works (Highway).
Excel VbA for Technical works (Highway).Excel VbA for Technical works (Highway).
Excel VbA for Technical works (Highway).Avinash Devela
 
Working with the Microsoft Office Object Models
Working with the Microsoft Office Object ModelsWorking with the Microsoft Office Object Models
Working with the Microsoft Office Object ModelsLearnNowOnline
 
Excel vba
Excel vbaExcel vba
Excel vbaYen_CY
 

En vedette (11)

Why Is Excel VBA So Important For Banks ?
Why Is Excel VBA So Important For Banks ?Why Is Excel VBA So Important For Banks ?
Why Is Excel VBA So Important For Banks ?
 
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
 
General VbA
General VbAGeneral VbA
General VbA
 
Intro to Excel VBA Programming
Intro to Excel VBA ProgrammingIntro to Excel VBA Programming
Intro to Excel VBA Programming
 
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...
JMP106 “Kum Bah Yah” Meets “Lets Kick Butt” : The Integration of IBM Lotus No...
 
Excel VbA for Technical works (Highway).
Excel VbA for Technical works (Highway).Excel VbA for Technical works (Highway).
Excel VbA for Technical works (Highway).
 
Working with the Microsoft Office Object Models
Working with the Microsoft Office Object ModelsWorking with the Microsoft Office Object Models
Working with the Microsoft Office Object Models
 
Excel vba
Excel vbaExcel vba
Excel vba
 
Excel vba macro programing
Excel vba macro programingExcel vba macro programing
Excel vba macro programing
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Vba Excel Level 2
Vba Excel Level 2Vba Excel Level 2
Vba Excel Level 2
 

Similaire à Vba part 1

200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJ200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJDannySingh23
 
Access tips access and sql part 2 putting vba and sql together
Access tips  access and sql part 2  putting vba and sql togetherAccess tips  access and sql part 2  putting vba and sql together
Access tips access and sql part 2 putting vba and sql togetherquest2900
 
Excel for SEO -from Distilled UK
Excel for SEO -from Distilled UKExcel for SEO -from Distilled UK
Excel for SEO -from Distilled UKEldad Sotnick-Yogev
 
Creating A User‑Defined Function In Excel Using Vba
Creating A User‑Defined Function In Excel Using VbaCreating A User‑Defined Function In Excel Using Vba
Creating A User‑Defined Function In Excel Using VbaChester Tugwell
 
Access tips access and sql part 3 practical examples
Access tips  access and sql part 3  practical examplesAccess tips  access and sql part 3  practical examples
Access tips access and sql part 3 practical examplesquest2900
 
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AM
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AMBLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AM
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AMAvyaya Tarnaka
 
Exp2003 exl ppt_02
Exp2003 exl ppt_02Exp2003 exl ppt_02
Exp2003 exl ppt_02lonetree
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhiibinstitute0
 

Similaire à Vba part 1 (20)

Vba part 1
Vba part 1Vba part 1
Vba part 1
 
200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJ200 Mega eBook Collection- http://bit.ly/3WEZuYJ
200 Mega eBook Collection- http://bit.ly/3WEZuYJ
 
Access tips access and sql part 2 putting vba and sql together
Access tips  access and sql part 2  putting vba and sql togetherAccess tips  access and sql part 2  putting vba and sql together
Access tips access and sql part 2 putting vba and sql together
 
Vbabook ed2
Vbabook ed2Vbabook ed2
Vbabook ed2
 
Vba primer
Vba primerVba primer
Vba primer
 
Automating SolidWorks with Excel
Automating SolidWorks with ExcelAutomating SolidWorks with Excel
Automating SolidWorks with Excel
 
VBA
VBAVBA
VBA
 
Ma3696 Lecture 1
Ma3696 Lecture 1Ma3696 Lecture 1
Ma3696 Lecture 1
 
Excel for SEO -from Distilled UK
Excel for SEO -from Distilled UKExcel for SEO -from Distilled UK
Excel for SEO -from Distilled UK
 
Excel 2007 Unit P
Excel 2007 Unit PExcel 2007 Unit P
Excel 2007 Unit P
 
Excel intermediate
Excel intermediateExcel intermediate
Excel intermediate
 
Chapter.01
Chapter.01Chapter.01
Chapter.01
 
Creating A User‑Defined Function In Excel Using Vba
Creating A User‑Defined Function In Excel Using VbaCreating A User‑Defined Function In Excel Using Vba
Creating A User‑Defined Function In Excel Using Vba
 
Quick start learn dax basics in 30 minutes
Quick start   learn dax basics in 30 minutesQuick start   learn dax basics in 30 minutes
Quick start learn dax basics in 30 minutes
 
Access tips access and sql part 3 practical examples
Access tips  access and sql part 3  practical examplesAccess tips  access and sql part 3  practical examples
Access tips access and sql part 3 practical examples
 
Vba 2 (students copy)
Vba 2 (students copy)Vba 2 (students copy)
Vba 2 (students copy)
 
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AM
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AMBLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AM
BLOCK YOUR DATE FOR FEB 13 MONDAY @ 9AM
 
Excel vba
Excel vbaExcel vba
Excel vba
 
Exp2003 exl ppt_02
Exp2003 exl ppt_02Exp2003 exl ppt_02
Exp2003 exl ppt_02
 
Learn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in DelhiLearn VBA Training & Advance Excel Courses in Delhi
Learn VBA Training & Advance Excel Courses in Delhi
 

Dernier

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfjoe51371421
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...kellynguyen01
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideChristina Lin
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfCionsystems
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...gurkirankumar98700
 

Dernier (20)

why an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdfwhy an Opensea Clone Script might be your perfect match.pdf
why an Opensea Clone Script might be your perfect match.pdf
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop SlideBuilding Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
Building Real-Time Data Pipelines: Stream & Batch Processing workshop Slide
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Active Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdfActive Directory Penetration Testing, cionsystems.com.pdf
Active Directory Penetration Testing, cionsystems.com.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
(Genuine) Escort Service Lucknow | Starting ₹,5K To @25k with A/C 🧑🏽‍❤️‍🧑🏻 89...
 

Vba part 1

  • 1. EXCEL VBA PROGRAMMING PART 1 MORTEZA NOSHAD MSPRO.NOSHAD@GMAIL.COM 0912 - 84 98 775
  • 2. WHAT CAN YOU DO WITH VBA? Inserting a bunch of text Automating a task you perform frequently Automating repetitive operations Creating a custom command Creating a custom button Developing new worksheet functions Creating custom add-ins for Excel Creating complete, macro-driven applications
  • 3. ADVANTAGES AND DISADVANTAGES OF VBA Excel always executes the task in exactly the same way. (In most cases, consistency is a good thing.) Excel performs the task much faster than you can do it manually (unless, of course, you’re Clark Kent).you’re a good macro programmer, Excel always performs the task without errors (which probably can’t be said about you or me)If you set things up properly, someone who doesn’t know anything about Excel can perform the task You can do things in Excel that are otherwise impossible — which can make you a very popular person around the officeFor long, time-consuming tasks, you don’t have to sit in front of your computer and get bored. Excel does the work, while you hang out at the water cooler VBA ADVANTAGES
  • 4. ADVANTAGES AND DISADVANTAGES OF VBA You have to know how to write programs in VBA (but that’s why you bought this book, right?). Fortunately, it’s not as difficult as you might expectOther people who need to use your VBA programs must have their own copies of Excel. It would be nice if you could press a button that transforms your Excel/VBA application into a stand- alone program, but that isn’t possible (and probably never will be) Sometimes, things go wrong. In other words, you can’t blindly assume that your VBA program will always work correctly under all circumstances. Welcome to the world of debugging and, if others are using your macros, technical support VBA is a moving target. As you know, Microsoft is continually upgrading Excel. Even though Microsoft puts great effort into compatibility between versions, you may discover that the VBA code you’ve written doesn’t work properly with older versions or VBA DISADVANTAGES
  • 5. VBA IN A NUTSHELL You perform actions in VBA by writing (or recording) code in a VBA module A VBA module consists of Sub procedures A VBA module can also have Function procedures VBA manipulates objects Objects are arranged in a hierarchy Objects of the same type form a collection You refer to an object by specifying its position in the object hierarchy, using a dot (a.k.a., a period) as a separator If you omit specific references, Excel uses the active objects Objects have properties
  • 6. VBA IN A NUTSHELL You refer to a property of an object by combining the object name with the property name, separated by a dot You can assign values to variables Objects have methods You specify a method by combining the object with the method, separated by a dot VBA includes all the constructs of modern programming languages, including variables, arrays, and looping
  • 7. MACRO Create your first macro Use relative references Macro shortcut key-Place macro Examining macro How Excel Executes Statements Saving workbooks that contain macros Understanding macro security
  • 8. VISUAL BASIC EDITOR Working with the Project Explorer Working with a Code Window Getting VBA code into a module Enter the code directly. Use the Excel macro recorder to record your actions and convert them to VBA code Copy the code from one module and paste it into another. Customizing the VBA Environment
  • 9. SUBS VERSUS FUNCTIONS A Sub procedure is a group of VBA statements that performs an action (or actions) with Excel. A Function procedure is a group of VBA statements that performs a calculation and returns a single value.
  • 10. NAMING SUBS AND FUNCTIONS You can use letters, numbers, and some punctuation characters, but the first character must be a letter. You can’t use any spaces or periods in the name. VBA does not distinguish between uppercase and lowercase letters. You can’t embed any of the following characters in a name: #, $, %, &, @,^, *, or !. If you write a Function procedure for use in a formula, make sure the name does not look like a cell address (for example, AC12). Names can be no longer than 255 characters. (Of course, you would never make a procedure name this long.) Ideally, a procedure’s name should describe the routine’s purpose. A good practice is to create a name by combining a verb and a noun — for example, ProcessData, PrintReport, Sort_Array, or CheckFilename
  • 11. EXCECUTING SUB PROCEDURES With the Run➪Run sub/UserForm command (in the VBE) & F5 key. From Excel’s Macro dialog box. You open this box by choosing Developer➪Code➪Macros). Or you can press the Alt+F8 shortcut key. require an argument. Using the Ctrl+key shortcut assigned to the Sub procedure Clicking a button or a shape on a worksheet From another Sub procedure that you write From a button on the Quick Access Toolbar From a custom item on the ribbon you develop Automatically, when you open or close a workbook When an event occurs From the Immediate window in the VBE
  • 12. EXECUTING FUNCTION PROCEDURES By calling the function from another Sub procedure or Function procedure By using the function in a worksheet formula 
  • 13. COMMENTS A comment is the simplest type of VBA statement. Because VBA ignores these statements, they can consist of anything you want. You can insert a comment to remind yourself why you did something or to clarify some particularly elegant code you wrote. Use comments liberally and extensively to describe what the code does (which isn’t always obvious by reading the code itself). Often, code that makes perfect sense today mystifies you tomorrow
  • 14. COMMENTS When testing a procedure, you may want to remove some statements temporarily. Rather than delete the statements, you can convert them to comments. Then when testing is completed, convert the comments back to statements. In the VBE, choose view➪Toolbars➪Edit to display the Edit toolbar. To convert a block of statements to comments, select the statements and click the Comment Block button. To remove the apostrophes, select the statements and click the
  • 15. COMMENTS • The following tips can help you make effective use of comments: • Briefly describe the purpose of each Sub or Function procedure • you write. • Use comments to keep track of changes you make to a procedure. • Use a comment to indicate that you’re using a function or a construct • in an unusual or nonstandard manner. • Use comments to describe the variables you use, especially if you don’t use meaningful variable names. • Use a comment to describe any workarounds you develop to overcome bugs in Excel.
  • 16. UNDERSTANDING VARIABLES A variable is simply a named storage location in your computer’s memory. You have lots of flexibility in naming your variables, so make the variable names as descriptive as possible. You assign a value to a variable by using the equal sign operator.You can use letters, numbers, and some punctuation characters, but the first character must be a letter. You cannot use any spaces or periods in a variable name. VBA does not distinguish between uppercase and lowercase letters. You cannot use the following characters in a variable name: #, $, %, &, or !. Variable names can be no longer than 255 characters.
  • 17. UNDERSTANDING VARIABLES wear yourself out typing the entire name of a variable. Just type the first two or three characters and then hit Control+Space. The VBE will either complete the entry for you or — if the choice is ambiguous — show you a pick list to select from. In fact, this slick trick works with reserved words too. has many reserved words that you can’t use for variable names or procedure names. These include words such as Sub, Dim, With, End, and For. If you attempt to use one of these words as a variable, you may get a compile error (your code won’t run). So, if an assignment statement produces an error message, double-check and make sure that the variable name
  • 18. WHAT ARE VBA’S DATA TYPES?
  • 23.
  • 24. INTRODUCING THE EXCEL OBJECT MODEL
  • 25. INTRODUCING THE EXCEL OBJECT MODEL 1 2 3
  • 27. REFERRING TO OBJECTS In this case, the number is not in quotation marks. Bottom line? If you refer to an object by using its name, use quotation marks. If you refer to an object by using its index number, use a plain number without quotation marks.
  • 33. USING VBA AND WORKSHEET FUNCTIONS
  • 34. VBA FUNCTIONS THAT DO MORE THAN RETURN A VALUE