SlideShare a Scribd company logo
Creating a User‑Defined Function
in Excel using VBA
       Microsoft Office Training
            Excel Training



               www.bluepecan.co.uk
• Excel includes many different functions that
  help you complete calculations, but have you
  ever wished for a function that that doesn’t
  exist in Excel? If you have, this tutorial will
  explain how to create a function DIY style.




                    www.bluepecan.co.uk
• You create custom functions in the Visual
  Basic Editor (VBE) which you can get to by
  clicking Tools > Macro > Visual Basic Editor or
  by using the shortcut key ALT F11. If you are
  using Excel 2007 click on the Developer ribbon
  and then click on the Visual Basic button.
• Once in the VBE environment you will need to
  create a module to hold your function. Click
  Insert > Module

                    www.bluepecan.co.uk
• A function is defined with a name (for the
  function) and if necessary between 1 and 60
  arguments. For example the Excel worksheet
  function VLookup has 4 arguments.




                   www.bluepecan.co.uk
A func tio n with no arg ume nts

• Several VBA functions such as rand() have no
  arguments. In the same way you can create
  custom functions that have no arguments.
  The following function will display the path
  and filename of the active workbook.




                   www.bluepecan.co.uk
Function File()
  File = ActiveWorkbook.FullName
End Function

• Notice the function starts and ends with
  ‘Function’ rather than sub.



                   www.bluepecan.co.uk
• Enter =File() into a worksheet to see the
  result.
  or
• Click on fx (Insert Function) and open the User
  Defined category to see your function listed
  here



                    www.bluepecan.co.uk
• The next function displays the username (as
  set in Tools | Options | General)
  Function User()
     User = Application.username
  End Function




                    www.bluepecan.co.uk
A Cus to m Func tio ns with Arg ume nts

• The following function simply calculates a value plus
  VAT. In an empty worksheet create a column of
  prices.
• Then switch to the VBE environment and in a module
  create the following custom function. Notice that
  with this function you need to place arguments in
  the brackets after the function name.
• The sales argument will require you to select the cell
  containing the sales value for which you wish to add
  the VAT to.

                       www.bluepecan.co.uk
Function vat(sales)
      vat = sales * 1.15
   End Function
• Use the VAT function to calculate the VAT
  inclusive value in your list of prices




                   www.bluepecan.co.uk
• We could also add a markup value as part of
  our function by adding a second argument.




                   www.bluepecan.co.uk
Function retail(sales, markup)
  retail = sales * (markup + 1) * 1.15
End Function




                    www.bluepecan.co.uk
• Enter a markup percentage on your worksheet
  and refer to this value in the second argument
  of the function (separated from the first by a
  comma).
• You can always use the functions argument
  dialogue box to enter cell references or
  values.


                   www.bluepecan.co.uk
• The following function calculates the amount
  of time that has elapsed between a start time
  and the end time.
• The function also works for times over two
  separate days, in other words when the start
  time is greater than the end time.



                   www.bluepecan.co.uk
Function CalTime(StartTime, EndTime)
  If StartTime > EndTime Then
  CalTime = EndTime - StartTime + 1
  Else
  CalTime = EndTime - StartTime
  End If
End Function

                  www.bluepecan.co.uk
• See this Excel training tutorial on the Blue
  Pecan website




                     www.bluepecan.co.uk

More Related Content

What's hot (20)

Spring annotations notes
Spring annotations notesSpring annotations notes
Spring annotations notes
Vipul Singh
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우
Arawn Park
 
Buttons In .net Visual Basic
Buttons In .net Visual BasicButtons In .net Visual Basic
Buttons In .net Visual Basic
manish maurya
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 
Express node js
Express node jsExpress node js
Express node js
Yashprit Singh
 
Ajax
AjaxAjax
Ajax
Esteban Chamba
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
Hitesh-Java
 
NoSQL panorama - Jean Seiler Softeam
NoSQL panorama - Jean Seiler SofteamNoSQL panorama - Jean Seiler Softeam
NoSQL panorama - Jean Seiler Softeam
TelecomValley
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworks
EndranNL
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
boyney123
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
Hitesh-Java
 
3. Java Script
3. Java Script3. Java Script
3. Java Script
Jalpesh Vasa
 
Javascript
JavascriptJavascript
Javascript
Vibhor Grover
 
Jquery validate - TuanNA
Jquery validate - TuanNAJquery validate - TuanNA
Jquery validate - TuanNA
Framgia Vietnam
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
rudib
 
Laravel ppt
Laravel pptLaravel ppt
Laravel ppt
Mayank Panchal
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 
cookies.ppt
cookies.pptcookies.ppt
cookies.ppt
SchoolEducationDepar
 
Spring annotations notes
Spring annotations notesSpring annotations notes
Spring annotations notes
Vipul Singh
 
Object-oriented Programming-with C#
Object-oriented Programming-with C#Object-oriented Programming-with C#
Object-oriented Programming-with C#
Doncho Minkov
 
코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우코틀린 멀티플랫폼, 미지와의 조우
코틀린 멀티플랫폼, 미지와의 조우
Arawn Park
 
Buttons In .net Visual Basic
Buttons In .net Visual BasicButtons In .net Visual Basic
Buttons In .net Visual Basic
manish maurya
 
JavaScript: Variables and Functions
JavaScript: Variables and FunctionsJavaScript: Variables and Functions
JavaScript: Variables and Functions
Jussi Pohjolainen
 
Collections - Array List
Collections - Array List Collections - Array List
Collections - Array List
Hitesh-Java
 
NoSQL panorama - Jean Seiler Softeam
NoSQL panorama - Jean Seiler SofteamNoSQL panorama - Jean Seiler Softeam
NoSQL panorama - Jean Seiler Softeam
TelecomValley
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworks
EndranNL
 
Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.Introduction into ES6 JavaScript.
Introduction into ES6 JavaScript.
boyney123
 
Java IO, Serialization
Java IO, Serialization Java IO, Serialization
Java IO, Serialization
Hitesh-Java
 
Jquery validate - TuanNA
Jquery validate - TuanNAJquery validate - TuanNA
Jquery validate - TuanNA
Framgia Vietnam
 
ASP.NET MVC Performance
ASP.NET MVC PerformanceASP.NET MVC Performance
ASP.NET MVC Performance
rudib
 
JavaScript - Chapter 11 - Events
 JavaScript - Chapter 11 - Events  JavaScript - Chapter 11 - Events
JavaScript - Chapter 11 - Events
WebStackAcademy
 

Similar to Creating A User‑Defined Function In Excel Using Vba (20)

MIS 226: Chapter 2
MIS 226: Chapter 2MIS 226: Chapter 2
MIS 226: Chapter 2
macrob14
 
Excel 2007-functions-formulas
Excel 2007-functions-formulasExcel 2007-functions-formulas
Excel 2007-functions-formulas
Sankar Natarajan
 
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptxTLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
ElNioJanSomeraAmpo
 
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptxADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
michellemariebalejon
 
Excel Overview.pptx
Excel Overview.pptxExcel Overview.pptx
Excel Overview.pptx
NewmanLeke
 
functions_in_microsoft_excel_2003
 functions_in_microsoft_excel_2003 functions_in_microsoft_excel_2003
functions_in_microsoft_excel_2003
gaurav jain
 
Excel Function Training
Excel Function TrainingExcel Function Training
Excel Function Training
kapileshwar kumar
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
jpdas54
 
Getting started with microsoft excel.
Getting started with microsoft excel.Getting started with microsoft excel.
Getting started with microsoft excel.
Jatin Mishra
 
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
TechMit
 
Excel.fns frmls
Excel.fns frmlsExcel.fns frmls
Excel.fns frmls
rowenick
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
srv1972
 
Elementary Data Analysis with MS Excel_Day-3
Elementary Data Analysis with MS Excel_Day-3Elementary Data Analysis with MS Excel_Day-3
Elementary Data Analysis with MS Excel_Day-3
Redwan Ferdous
 
Excell vba
Excell vbaExcell vba
Excell vba
Heru Cahyo
 
Office session10
Office session10Office session10
Office session10
Muhammad Ehtisham Siddiqui
 
excel 101 formulas.pdf
excel 101 formulas.pdfexcel 101 formulas.pdf
excel 101 formulas.pdf
Vilas Kharat
 
100 Excel Functions you should know in one handy PDF.pdf
100 Excel Functions you should know in one handy PDF.pdf100 Excel Functions you should know in one handy PDF.pdf
100 Excel Functions you should know in one handy PDF.pdf
Mohammad Shaar
 
Useful Excel Functions & Formula Used everywhere.pptx
Useful Excel Functions & Formula Used everywhere.pptxUseful Excel Functions & Formula Used everywhere.pptx
Useful Excel Functions & Formula Used everywhere.pptx
vanshikatyagi74
 
Data management and excel appication.pptx
Data management and excel appication.pptxData management and excel appication.pptx
Data management and excel appication.pptx
OlabodeSamuel3
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]
MaridelBajeta
 
MIS 226: Chapter 2
MIS 226: Chapter 2MIS 226: Chapter 2
MIS 226: Chapter 2
macrob14
 
Excel 2007-functions-formulas
Excel 2007-functions-formulasExcel 2007-functions-formulas
Excel 2007-functions-formulas
Sankar Natarajan
 
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptxTLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
TLE 6 PPT Q4 - Advanced Functions And Formulas In Excel.pptx
ElNioJanSomeraAmpo
 
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptxADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
ADVANCED FUNCTIONS AND FORMULAS IN EXCEL.pptx
michellemariebalejon
 
Excel Overview.pptx
Excel Overview.pptxExcel Overview.pptx
Excel Overview.pptx
NewmanLeke
 
functions_in_microsoft_excel_2003
 functions_in_microsoft_excel_2003 functions_in_microsoft_excel_2003
functions_in_microsoft_excel_2003
gaurav jain
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
jpdas54
 
Getting started with microsoft excel.
Getting started with microsoft excel.Getting started with microsoft excel.
Getting started with microsoft excel.
Jatin Mishra
 
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
Lean Excel Top Functions - Quick Reference Guide with 500 Examples (Scott Rat...
TechMit
 
Excel.fns frmls
Excel.fns frmlsExcel.fns frmls
Excel.fns frmls
rowenick
 
Excel formulas-manual
Excel formulas-manualExcel formulas-manual
Excel formulas-manual
srv1972
 
Elementary Data Analysis with MS Excel_Day-3
Elementary Data Analysis with MS Excel_Day-3Elementary Data Analysis with MS Excel_Day-3
Elementary Data Analysis with MS Excel_Day-3
Redwan Ferdous
 
excel 101 formulas.pdf
excel 101 formulas.pdfexcel 101 formulas.pdf
excel 101 formulas.pdf
Vilas Kharat
 
100 Excel Functions you should know in one handy PDF.pdf
100 Excel Functions you should know in one handy PDF.pdf100 Excel Functions you should know in one handy PDF.pdf
100 Excel Functions you should know in one handy PDF.pdf
Mohammad Shaar
 
Useful Excel Functions & Formula Used everywhere.pptx
Useful Excel Functions & Formula Used everywhere.pptxUseful Excel Functions & Formula Used everywhere.pptx
Useful Excel Functions & Formula Used everywhere.pptx
vanshikatyagi74
 
Data management and excel appication.pptx
Data management and excel appication.pptxData management and excel appication.pptx
Data management and excel appication.pptx
OlabodeSamuel3
 
Etech. mitch. [autosaved]
Etech. mitch. [autosaved]Etech. mitch. [autosaved]
Etech. mitch. [autosaved]
MaridelBajeta
 
Ad

Recently uploaded (11)

Personal Development_ Unit 3.1. Relationships
Personal Development_ Unit 3.1. RelationshipsPersonal Development_ Unit 3.1. Relationships
Personal Development_ Unit 3.1. Relationships
adeborja
 
Personal Development_ Unit 3.1. Thinking
Personal Development_ Unit 3.1. ThinkingPersonal Development_ Unit 3.1. Thinking
Personal Development_ Unit 3.1. Thinking
adeborja
 
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
shikosham
 
Personal Development 3.2.. Metacognition
Personal Development 3.2.. MetacognitionPersonal Development 3.2.. Metacognition
Personal Development 3.2.. Metacognition
adeborja
 
Psychology_of_ Habit_Formation.pptx
Psychology_of_      Habit_Formation.pptxPsychology_of_      Habit_Formation.pptx
Psychology_of_ Habit_Formation.pptx
raghavsharma19765
 
Maslow’s need of hierarchy of needs.pptx
Maslow’s need of hierarchy of needs.pptxMaslow’s need of hierarchy of needs.pptx
Maslow’s need of hierarchy of needs.pptx
SheetalVatsa2
 
Map Reading & Where to Get Free Maps and Apps.pptx
Map Reading & Where to Get Free Maps and Apps.pptxMap Reading & Where to Get Free Maps and Apps.pptx
Map Reading & Where to Get Free Maps and Apps.pptx
Bob Mayer
 
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Vikash Gautam
 
PERSONAL DEVELOPMENT about relarionships
PERSONAL DEVELOPMENT about relarionshipsPERSONAL DEVELOPMENT about relarionships
PERSONAL DEVELOPMENT about relarionships
adeborja
 
Women_Empowerment_LPU_Presentation.pptx.
Women_Empowerment_LPU_Presentation.pptx.Women_Empowerment_LPU_Presentation.pptx.
Women_Empowerment_LPU_Presentation.pptx.
jinny kaur
 
Goal-Setting-Strategies-PP-8-15-06 in life.ppt
Goal-Setting-Strategies-PP-8-15-06 in life.pptGoal-Setting-Strategies-PP-8-15-06 in life.ppt
Goal-Setting-Strategies-PP-8-15-06 in life.ppt
bendaribendari
 
Personal Development_ Unit 3.1. Relationships
Personal Development_ Unit 3.1. RelationshipsPersonal Development_ Unit 3.1. Relationships
Personal Development_ Unit 3.1. Relationships
adeborja
 
Personal Development_ Unit 3.1. Thinking
Personal Development_ Unit 3.1. ThinkingPersonal Development_ Unit 3.1. Thinking
Personal Development_ Unit 3.1. Thinking
adeborja
 
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
Beyond the Lampshade Woody Allen’s Unlikely Wisdom for Profound Personal Grow...
shikosham
 
Personal Development 3.2.. Metacognition
Personal Development 3.2.. MetacognitionPersonal Development 3.2.. Metacognition
Personal Development 3.2.. Metacognition
adeborja
 
Psychology_of_ Habit_Formation.pptx
Psychology_of_      Habit_Formation.pptxPsychology_of_      Habit_Formation.pptx
Psychology_of_ Habit_Formation.pptx
raghavsharma19765
 
Maslow’s need of hierarchy of needs.pptx
Maslow’s need of hierarchy of needs.pptxMaslow’s need of hierarchy of needs.pptx
Maslow’s need of hierarchy of needs.pptx
SheetalVatsa2
 
Map Reading & Where to Get Free Maps and Apps.pptx
Map Reading & Where to Get Free Maps and Apps.pptxMap Reading & Where to Get Free Maps and Apps.pptx
Map Reading & Where to Get Free Maps and Apps.pptx
Bob Mayer
 
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Title Love Beyond the Screen The Truth About Social Media Relationships (1) f...
Vikash Gautam
 
PERSONAL DEVELOPMENT about relarionships
PERSONAL DEVELOPMENT about relarionshipsPERSONAL DEVELOPMENT about relarionships
PERSONAL DEVELOPMENT about relarionships
adeborja
 
Women_Empowerment_LPU_Presentation.pptx.
Women_Empowerment_LPU_Presentation.pptx.Women_Empowerment_LPU_Presentation.pptx.
Women_Empowerment_LPU_Presentation.pptx.
jinny kaur
 
Goal-Setting-Strategies-PP-8-15-06 in life.ppt
Goal-Setting-Strategies-PP-8-15-06 in life.pptGoal-Setting-Strategies-PP-8-15-06 in life.ppt
Goal-Setting-Strategies-PP-8-15-06 in life.ppt
bendaribendari
 
Ad

Creating A User‑Defined Function In Excel Using Vba

  • 1. Creating a User‑Defined Function in Excel using VBA Microsoft Office Training Excel Training www.bluepecan.co.uk
  • 2. • Excel includes many different functions that help you complete calculations, but have you ever wished for a function that that doesn’t exist in Excel? If you have, this tutorial will explain how to create a function DIY style. www.bluepecan.co.uk
  • 3. • You create custom functions in the Visual Basic Editor (VBE) which you can get to by clicking Tools > Macro > Visual Basic Editor or by using the shortcut key ALT F11. If you are using Excel 2007 click on the Developer ribbon and then click on the Visual Basic button. • Once in the VBE environment you will need to create a module to hold your function. Click Insert > Module www.bluepecan.co.uk
  • 4. • A function is defined with a name (for the function) and if necessary between 1 and 60 arguments. For example the Excel worksheet function VLookup has 4 arguments. www.bluepecan.co.uk
  • 5. A func tio n with no arg ume nts • Several VBA functions such as rand() have no arguments. In the same way you can create custom functions that have no arguments. The following function will display the path and filename of the active workbook. www.bluepecan.co.uk
  • 6. Function File() File = ActiveWorkbook.FullName End Function • Notice the function starts and ends with ‘Function’ rather than sub. www.bluepecan.co.uk
  • 7. • Enter =File() into a worksheet to see the result. or • Click on fx (Insert Function) and open the User Defined category to see your function listed here www.bluepecan.co.uk
  • 8. • The next function displays the username (as set in Tools | Options | General) Function User() User = Application.username End Function www.bluepecan.co.uk
  • 9. A Cus to m Func tio ns with Arg ume nts • The following function simply calculates a value plus VAT. In an empty worksheet create a column of prices. • Then switch to the VBE environment and in a module create the following custom function. Notice that with this function you need to place arguments in the brackets after the function name. • The sales argument will require you to select the cell containing the sales value for which you wish to add the VAT to. www.bluepecan.co.uk
  • 10. Function vat(sales) vat = sales * 1.15 End Function • Use the VAT function to calculate the VAT inclusive value in your list of prices www.bluepecan.co.uk
  • 11. • We could also add a markup value as part of our function by adding a second argument. www.bluepecan.co.uk
  • 12. Function retail(sales, markup) retail = sales * (markup + 1) * 1.15 End Function www.bluepecan.co.uk
  • 13. • Enter a markup percentage on your worksheet and refer to this value in the second argument of the function (separated from the first by a comma). • You can always use the functions argument dialogue box to enter cell references or values. www.bluepecan.co.uk
  • 14. • The following function calculates the amount of time that has elapsed between a start time and the end time. • The function also works for times over two separate days, in other words when the start time is greater than the end time. www.bluepecan.co.uk
  • 15. Function CalTime(StartTime, EndTime) If StartTime > EndTime Then CalTime = EndTime - StartTime + 1 Else CalTime = EndTime - StartTime End If End Function www.bluepecan.co.uk
  • 16. • See this Excel training tutorial on the Blue Pecan website www.bluepecan.co.uk