SlideShare a Scribd company logo
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 1
Vijay 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 India
ibinstitute0
 
2016 Excel/VBA Notes
2016 Excel/VBA Notes2016 Excel/VBA Notes
2016 Excel/VBA Notes
Yang Ye
 
Transforming Power Point Show with VBA
Transforming Power Point Show with VBATransforming Power Point Show with VBA
Transforming Power Point Show with VBA
DCPS
 
Excel vba
Excel vbaExcel vba
Excel vba
Almeda Asuncion
 
Vba Class Level 1
Vba Class Level 1Vba Class Level 1
Vba Class Level 1
Ben Miu CIM® FCSI A+
 
Excel VBA programming basics
Excel VBA programming basicsExcel VBA programming basics
Excel VBA programming basics
Hang 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 afzal
1995786
 
Vba Class Level 3
Vba Class Level 3Vba Class Level 3
Vba Class Level 3
Ben Miu CIM® FCSI A+
 
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
quest2900
 
Getting started with Microsoft Excel Macros
Getting started with Microsoft Excel MacrosGetting started with Microsoft Excel Macros
Getting started with Microsoft Excel Macros
Nick Weisenberger
 
MACROS excel
MACROS excelMACROS excel
MACROS excel
Zenobia Sukhia
 
MS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project reportMS Excel Macros/ VBA Project report
MS Excel Macros/ VBA Project report
Prafull 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 Delhi
ibinstitute0
 
Advance MS Excel
Advance MS ExcelAdvance MS Excel
Advance MS Excel
acute23
 
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...
Amity University | FMS - DU | IMT | Stratford University | KKMI International Institute | AIMA | DTU
 

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
 
سکوت سازمانی
سکوت سازمانیسکوت سازمانی
سکوت سازمانی
Mahsa Asgharloo
 
فرهنگ سازمانی
فرهنگ سازمانیفرهنگ سازمانی
فرهنگ سازمانیEsmat Hajvahedi
 
نظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریتنظریه کوانتوم در مدیریت
نظریه کوانتوم در مدیریت
Ali Mirfallah
 
تئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفتهتئوری های مدیریت پیشرفته
تئوری های مدیریت پیشرفته
somayeh fatehiyan
 
خلاصه کتاب تئوری های سازمان و مدیریت
خلاصه  کتاب تئوری های سازمان و مدیریتخلاصه  کتاب تئوری های سازمان و مدیریت
خلاصه کتاب تئوری های سازمان و مدیریت
Moshaveran international Business School
 
اسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفتهاسلايد آموزشي مدیریت استراتژیک پیشرفته
اسلايد آموزشي مدیریت استراتژیک پیشرفته
Ali Masoombeigi
 
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت   خلاقیت، نوآوری و کارآفرینیتئوری های مدیریت   خلاقیت، نوآوری و کارآفرینی
تئوری های مدیریت خلاقیت، نوآوری و کارآفرینی
Nozar Poursheikhian
 
Cloud Security and Risk Management
Cloud Security and Risk ManagementCloud Security and Risk Management
Cloud Security and Risk Management
Morteza Javan
 
Organization and management theory
Organization and management theoryOrganization and management theory
Organization and management theory
Ali 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 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
DannySingh23
 
Vbabook ed2
Vbabook ed2Vbabook ed2
Vbabook ed2
NilsonVallecillo
 
Automating SolidWorks with Excel
Automating SolidWorks with ExcelAutomating SolidWorks with Excel
Automating SolidWorks with Excel
Razorleaf Corporation
 
Learn Excel Macro
Learn Excel Macro  Learn Excel Macro
Learn Excel Macro
AbhisheK Kumar Rajoria
 
Excel for SEO -from Distilled UK
Excel for SEO -from Distilled UKExcel for SEO -from Distilled UK
Excel for SEO -from Distilled UK
Eldad Sotnick-Yogev
 
Excel intermediate
Excel intermediateExcel intermediate
Excel intermediate
Kevin McLogan
 
Excel-VBA
Excel-VBAExcel-VBA
Excel-VBA
DHRUYEN HADIYA
 
Excel 2007 Unit P
Excel 2007 Unit PExcel 2007 Unit P
Excel 2007 Unit P
Raja Waseem Akhtar
 
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
Chester 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 examples
quest2900
 
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 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
quest2900
 
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_02
lonetree
 
Excel ways training_course_contents
Excel ways training_course_contentsExcel ways training_course_contents
Excel ways training_course_contents
Sai ExcelWays
 

Similar to Vba part 1 (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

Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
WSO2
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
Matt Welsh
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Jay Das
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
Paco van Beckhoven
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
Adele Miller
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
e20449
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
informapgpstrackings
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Mind IT Systems
 

Recently uploaded (20)

Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Accelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with PlatformlessAccelerate Enterprise Software Engineering with Platformless
Accelerate Enterprise Software Engineering with Platformless
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdfEnhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
Enhancing Project Management Efficiency_ Leveraging AI Tools like ChatGPT.pdf
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024Cracking the code review at SpringIO 2024
Cracking the code review at SpringIO 2024
 
May Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdfMay Marketo Masterclass, London MUG May 22 2024.pdf
May Marketo Masterclass, London MUG May 22 2024.pdf
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Graphic Design Crash Course for beginners
Graphic Design Crash Course for beginnersGraphic Design Crash Course for beginners
Graphic Design Crash Course for beginners
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
Field Employee Tracking System| MiTrack App| Best Employee Tracking Solution|...
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
Custom Healthcare Software for Managing Chronic Conditions and Remote Patient...
 

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