SlideShare a Scribd company logo
1 of 34
Download to read offline
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 office
For 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 expect
Other 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 with a future version of Excel
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 Uncomment Block button.
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.
• Write comments as you develop code, instead of saving the task for
a final step.
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 isn’t a reserved word.
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

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 1Vijay Perepa
 
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
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA NotesYang Ye
 
Transforming Power Point Show with VBA
Transforming Power Point Show with VBATransforming Power Point Show with VBA
Transforming Power Point Show with VBADCPS
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basicsHang Dong
 
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
 
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
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosNick Weisenberger
 
MS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportMS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportPrafull Dhamankar
 
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
 
Advance MS Excel
Advance MS ExcelAdvance MS Excel
Advance MS Excelacute23
 

What's hot (20)

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
 
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 introduction
Vba introductionVba introduction
Vba introduction
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA Notes
 
Transforming Power Point Show with VBA
Transforming Power Point Show with VBATransforming Power Point Show with VBA
Transforming Power Point Show with VBA
 
Vba
Vba Vba
Vba
 
Excel vba
Excel vbaExcel vba
Excel vba
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
 
VBA
VBAVBA
VBA
 
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
 
Vba Class Level 3
Vba Class Level 3Vba Class Level 3
Vba Class Level 3
 
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
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
 
MACROS excel
MACROS excelMACROS excel
MACROS excel
 
MS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportMS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project report
 
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
 
Advance MS Excel
Advance MS ExcelAdvance MS Excel
Advance MS Excel
 
Excel ch10
Excel ch10Excel ch10
Excel ch10
 
Learn Excel 2016 VBA and Macros in 24 Hours (Part 1 of 2) On Practical's Asp...
Learn Excel 2016 VBA and Macros in 24 Hours  (Part 1 of 2) On Practical's Asp...Learn Excel 2016 VBA and Macros in 24 Hours  (Part 1 of 2) On Practical's Asp...
Learn Excel 2016 VBA and Macros in 24 Hours (Part 1 of 2) On Practical's Asp...
 

Viewers also liked

هفت اصل طراحی تجربه خرید در فروشگاه
هفت اصل طراحی تجربه خرید در فروشگاههفت اصل طراحی تجربه خرید در فروشگاه
هفت اصل طراحی تجربه خرید در فروشگاهMorteza Noshad
 
تئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهتئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهEsmat Hajvahedi
 
ساختار سازماني و سازماندهی
ساختار سازماني و سازماندهیساختار سازماني و سازماندهی
ساختار سازماني و سازماندهیMorteza Noshad
 
فرهنگ سازمانی
فرهنگ سازمانیفرهنگ سازمانی
فرهنگ سازمانیEsmat Hajvahedi
 
نظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریتنظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریتAli Mirfallah
 
تئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهتئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهsomayeh fatehiyan
 
اسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفتهاسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفتهAli Masoombeigi
 
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت   خلاقیت، نوآوری و کارآفرینیتئوری های مدیریت   خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینیNozar Poursheikhian
 
Cloud Security and Risk Management
Cloud Security and Risk ManagementCloud Security and Risk Management
Cloud Security and Risk ManagementMorteza Javan
 
Organization and management theory
Organization and management theoryOrganization and management theory
Organization and management theoryAli Mirfallah
 

Viewers also liked (13)

Organizing
OrganizingOrganizing
Organizing
 
هفت اصل طراحی تجربه خرید در فروشگاه
هفت اصل طراحی تجربه خرید در فروشگاههفت اصل طراحی تجربه خرید در فروشگاه
هفت اصل طراحی تجربه خرید در فروشگاه
 
تئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهتئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفته
 
ساختار سازماني و سازماندهی
ساختار سازماني و سازماندهیساختار سازماني و سازماندهی
ساختار سازماني و سازماندهی
 
سکوت سازمانی
سکوت سازمانیسکوت سازمانی
سکوت سازمانی
 
فرهنگ سازمانی
فرهنگ سازمانیفرهنگ سازمانی
فرهنگ سازمانی
 
نظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریتنظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریت
 
تئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهتئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفته
 
خلاصه کتاب تئوری های سازمان و مدیریت
خلاصه  کتاب تئوری های سازمان و مدیریتخلاصه  کتاب تئوری های سازمان و مدیریت
خلاصه کتاب تئوری های سازمان و مدیریت
 
اسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفتهاسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفته
 
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت   خلاقیت، نوآوری و کارآفرینیتئوری های مدیریت   خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینی
 
Cloud Security and Risk Management
Cloud Security and Risk ManagementCloud Security and Risk Management
Cloud Security and Risk Management
 
Organization and management theory
Organization and management theoryOrganization and management theory
Organization and management theory
 

Similar to Excel VBA Programming for Beginners

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
 
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
 
Access tips access and sql part 4 building select queries on-the-fly
Access tips  access and sql part 4  building select queries on-the-flyAccess tips  access and sql part 4  building select queries on-the-fly
Access tips access and sql part 4 building select queries on-the-flyquest2900
 
Microsoft excel training
Microsoft excel trainingMicrosoft excel training
Microsoft excel trainingEmilyE120
 
Exp2003 exl ppt_02
Exp2003 exl ppt_02Exp2003 exl ppt_02
Exp2003 exl ppt_02lonetree
 
Excel ways training_course_contents
Excel ways training_course_contentsExcel ways training_course_contents
Excel ways training_course_contentsSai ExcelWays
 

Similar to Excel VBA Programming for Beginners (20)

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
 
Vbabook ed2
Vbabook ed2Vbabook ed2
Vbabook ed2
 
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
 
Vba primer
Vba primerVba primer
Vba primer
 
Learn Excel Macro
Learn Excel Macro  Learn Excel Macro
Learn Excel Macro
 
Excel for SEO -from Distilled UK
Excel for SEO -from Distilled UKExcel for SEO -from Distilled UK
Excel for SEO -from Distilled UK
 
Excel intermediate
Excel intermediateExcel intermediate
Excel intermediate
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
 
Chapter.01
Chapter.01Chapter.01
Chapter.01
 
Excel 2007 Unit P
Excel 2007 Unit PExcel 2007 Unit P
Excel 2007 Unit P
 
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
 
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
 
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
 
Vba 2 (students copy)
Vba 2 (students copy)Vba 2 (students copy)
Vba 2 (students copy)
 
Access tips access and sql part 4 building select queries on-the-fly
Access tips  access and sql part 4  building select queries on-the-flyAccess tips  access and sql part 4  building select queries on-the-fly
Access tips access and sql part 4 building select queries on-the-fly
 
Microsoft excel training
Microsoft excel trainingMicrosoft excel training
Microsoft excel training
 
Exp2003 exl ppt_02
Exp2003 exl ppt_02Exp2003 exl ppt_02
Exp2003 exl ppt_02
 
Excel ways training_course_contents
Excel ways training_course_contentsExcel ways training_course_contents
Excel ways training_course_contents
 

Recently uploaded

buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutionsmonugehlot87
 
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
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...soniya singh
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝soniya singh
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
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
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxTier1 app
 
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
 
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
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
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
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...aditisharan08
 
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
 
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
 

Recently uploaded (20)

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...
 
buds n tech IT solutions
buds n  tech IT                solutionsbuds n  tech IT                solutions
buds n tech IT solutions
 
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
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
Russian Call Girls in Karol Bagh Aasnvi ➡️ 8264348440 💋📞 Independent Escort S...
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
Call Girls in Naraina Delhi 💯Call Us 🔝8264348440🔝
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
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
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptxKnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
KnowAPIs-UnknownPerf-jaxMainz-2024 (1).pptx
 
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
 
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
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
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
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...Unit 1.1 Excite Part 1, class 9, cbse...
Unit 1.1 Excite Part 1, class 9, cbse...
 
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...
 
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
 

Excel VBA Programming for Beginners

  • 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 office For 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 expect Other 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 with a future version of Excel 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 Uncomment Block button.
  • 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. • Write comments as you develop code, instead of saving the task for a final step.
  • 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 isn’t a reserved word.
  • 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