SlideShare a Scribd company logo
1 of 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

More Related Content

What's hot

What's hot (20)

An introduction to vba and macros
An introduction to vba and macrosAn introduction to vba and macros
An introduction to vba and macros
 
Vba
Vba Vba
Vba
 
VBA - Macro For Ms.Excel
VBA - Macro For Ms.ExcelVBA - Macro For Ms.Excel
VBA - Macro For Ms.Excel
 
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
 
E learning excel vba programming lesson 1
E learning excel vba programming  lesson 1E learning excel vba programming  lesson 1
E learning excel vba programming lesson 1
 
MACROS excel
MACROS excelMACROS excel
MACROS excel
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
 
Excel tips and tricks you should try
Excel tips and tricks you should tryExcel tips and tricks you should try
Excel tips and tricks you should try
 
Excel Macro Magic
Excel Macro MagicExcel Macro Magic
Excel Macro Magic
 
Ms Excel Basic to Advance Tutorial
Ms Excel Basic to Advance TutorialMs Excel Basic to Advance Tutorial
Ms Excel Basic to Advance Tutorial
 
Ms excel
Ms excelMs excel
Ms excel
 
Tutorial 2
Tutorial 2Tutorial 2
Tutorial 2
 
Excel macro
Excel macroExcel macro
Excel macro
 
Microsoft Excel 2016 Basics Course contents
Microsoft Excel 2016 Basics Course contentsMicrosoft Excel 2016 Basics Course contents
Microsoft Excel 2016 Basics Course contents
 
Excel Training.pptx
Excel Training.pptxExcel Training.pptx
Excel Training.pptx
 
Advanced Microsoft Excel
Advanced Microsoft ExcelAdvanced Microsoft Excel
Advanced Microsoft Excel
 
MS Excel Tips & Tricks
MS Excel Tips & TricksMS Excel Tips & Tricks
MS Excel Tips & Tricks
 
Ms excel basic about Data, graph and pivot table
Ms excel basic about Data, graph and pivot table Ms excel basic about Data, graph and pivot table
Ms excel basic about Data, graph and pivot table
 
Visual Programming Lecture.pptx
Visual Programming Lecture.pptxVisual Programming Lecture.pptx
Visual Programming Lecture.pptx
 

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 

Similar to 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
 

Recently uploaded

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
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
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AIABDERRAOUF MEHENNI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...OnePlan Solutions
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
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
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
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
 
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
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVshikhaohhpro
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendArshad QA
 
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
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 

Recently uploaded (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
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
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
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
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
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)
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
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 ...
 
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
 
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
 
Optimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTVOptimizing AI for immediate response in Smart CCTV
Optimizing AI for immediate response in Smart CCTV
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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
 
Test Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and BackendTest Automation Strategy for Frontend and Backend
Test Automation Strategy for Frontend and Backend
 
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
 
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
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 

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