Excel VBA Formula Spreadsheet
Excel VBA Formula Spreadsheet
Edition 1
i
Excel formulas Vs VBA Macros
Contents
Introduction
Conclusion
iii
Excel VBA Formulas for Spreadsheet
1.Introduction
This book is written to provide the basics of the Excel VBA Formula for excel spread sheet
,this book VBA Formulas offer ease and convenience at your understanding
The Book Covers Way to work with VBA Formulas for spreadsheet
Influence the Basic Understanding of Formulas & VBA Formulas to Strike Similarity in
spreadsheet management.
The book is recommended to all the potential Learners who look for help in understanding
the Excel VBA Formula’s fundamentals and will get insight of an appropriate Way to do so.
All functions are 2016 Excel IDE Designed
1
Excel VBA Formulas for Spreadsheet
Function imedife(key)
imedife = IIf(key <= 18, "Minor", "Major")
End Function
This Will Create A new UDF (User Defined Function) with name imedife,we Can use this As we
Required. now type in Excel the function = imedife ,the UDF shows in small letter to
differentiate itself from Standard Excel Formula
In Cell B1 Type
= imedife (A1)
Syntax
=IF(A1<18,"Minor","Major")
2
Excel VBA Formulas for Spreadsheet
This Will Create A new UDF (User Defined Function) with name Emplycity,we Can use this As
we Required. now type in Excel the function =Emplycity ,the UDF shows in small letter to
differentiate itself from Standard Excel Formula
In Cell C1 Type
= Emplycity (A1)
Syntax
3
Excel VBA Formulas for Spreadsheet
LOOKUP(lookup_value, array)
=LOOKUP(6983,A1:C10) ,
Excel will perform search to find out the city of the employeeID 6983
Syntax
=VLOOKUP(6983,A1:C10,3,TRUE)
Excel will perform search to find out the city of the employeeID 6983
4
Excel VBA Formulas for Spreadsheet
Use the If Then statement in Excel VBA code lines function to act if a specific condition is met.
To create this Function, execute the following steps
Function grade(S)
If S < 19 Then
grade = "Worst"
Else
If S < 49 Then
grade = "Average"
Else
If S < 79 Then
grade = "Good"
Else
If S < 100 Then
grade = "E celle t
Else
grade = "n/a"
End If
End If
End If
End If
End Function
This Will Create A new UDF (User Defined Function) with name grade(S),we Can use this As
we Required. type in Excel the function = grade(A1) ,the UDF Co es up i s all letter’s to
differentiate itself from Standard Excel Formula
5
Excel VBA Formulas for Spreadsheet
In Cell B1 Type
= grade(A1)
Syntax
=IF(B1>79,"Excellent",IF(B1>49,"Good",IF(B1>19,"Average",IF(B1>1,"Worst","N/a"))))
6
Excel VBA Formulas for Spreadsheet
Use the If Then statement in Excel VBA code lines function to act if any of 2 specific condition
is met. Insert the below code in a New Module
Function Lans(Lval)
If Lval = "100" Or Lval = "99" Then
Lans = "Great"
Else
Lans = "ok"
End If
End Function
This Will Create A new UDF (User Defined Function) with name Lans(Lval)
we Can use this As we Required. type in Excel the function = Lans(Lval) ,the UDF Comesup in
s all letter’s to differe tiate itself fro “ta dard E cel For ula
In Cell B1 Type
= Lans(A1)
Syntax
7
Excel VBA Formulas for Spreadsheet
=IF(A1=100,"Great", IF(A1=99,"Great","ok") )
OR(logical1, [logical2], ..
=IF(OR(A1=100,A1=99),"Great","ok")
8
Excel VBA Formulas for Spreadsheet
Use the If Then statement in Excel VBA code lines function to act if the both 2 specific
condition is met. Insert the below code in a New Module.
This Will Create A new UDF (User Defined Function) with name Lhans(Lval, Lvxc) we Can use
this As we Required. type in Excel the function As =Lhans(Lval, Lvxc), the UDF Comes up in
s all letter’s to differe tiate itself fro “ta dard E cel For ula
In Cell C1 Type
= Lhans(A1,B1)
9
Excel VBA Formulas for Spreadsheet
Syntax
=IF(AND(A1=100,B1=99),"Great","ok")
10
Excel VBA Formulas for Spreadsheet
Use the If Then statement in Excel VBA code lines function to act if the both 2 specific
condition is met. Insert the below code in a New Module.
Lhanzs = "TRUE"
Else
Lhazs = "FALSE"
End If
End Function
This Will Create A new UDF (User Defined Function) with name Lhazs(Lval, Lvxc, Lvzl) we Can
use this As we Required. type in Excel the function As = Lhazs(Lval, Lvxc, Lvzl)), the UDF
Co es up i s all letter’s to differe tiate itself fro “ta dard E cel For ula,if a o e
condition met it turns TRUE.
In Cell D1 Type
=Lhazs(A1,B1,C1)
11
Excel VBA Formulas for Spreadsheet
Syntax
=XOR(A1>30,B1>50,C1=20)
12
Excel VBA Formulas for Spreadsheet
=A1*B1
13
Excel VBA Formulas for Spreadsheet
Function hlkup(product)
hlkup = Application.WorksheetFunction.HLookup(product,Range("produ"), 3, 0)
End Function
= hlkup(product,Range("produ"), 3, 0)
Syntax
=HLOOKUP(C25,B17:D22,3,FALSE)
14
Excel VBA Formulas for Spreadsheet
Function Matched(product)
In Cell E2 Type
Syntax
=MATCH(E17,$C$17:$C$22,0)
15
Excel VBA Formulas for Spreadsheet
score :A2:C8
screval :C2:C8
Scorval:A2:A8
Function Rlookupname(val)
Rlookupname = Application.WorksheetFunction.Index(Range("scorval"),
WorksheetFunction.Match(val, Range("screval"), 0))
End Function
In Cell D1 Type
= Rlookupname(88)
16
Excel VBA Formulas for Spreadsheet
Syntax
=INDEX(A2:C8,(MATCH(G2,C2:C8,0)),2)
17
Excel VBA Formulas for Spreadsheet
Function Vlkup(val)
Vlkup = Application.WorksheetFunction.VLookup(val,Range("score"), 3, 0)
End Function
score :A2:C8
Syntax
=VLOOKUP(8,score,3,FALSE)
18
Excel VBA Formulas for Spreadsheet
The Code Will Create A new UDF (User Defined Function) with name DayName,With this now
type in Excel the function = DayName ,the UDF shows in small letter to differentiate itself
from Standard Excel Formula
Syntax
19
Excel VBA Formulas for Spreadsheet
Excel will perform search for 3rd And the Day Results As Tuesday
Upon Entering A day Drag the Cell bottom for Next day to follow
20
Excel VBA Formulas for Spreadsheet
This Will Create A new UDF (User Defined Function) with name xlVers(),we Can use this As we
Required. type in Excel the function = xlVers(),the UDF Comes up i s all letter’s to
differentiate itself from Standard Excel Formula
In Cell A1 Type
= xlVers()
21
Excel VBA Formulas for Spreadsheet
Function isDat(S)
isDat = isDate(S)
End Function
This Will Create A new UDF (User Defined Function) with name isDat()
we Can use this As we Required. type in Excel the function = isDat() ,the UDF Comes up in
s all letter’s to differe tiate itself fro “ta dard E cel For ula.
In Cell B1 Type
= isDat (A1)
22
Excel VBA Formulas for Spreadsheet
Use the Application.Volatile True statement in Excel VBA function code lines to force the
formula to recalculate, Insert the below code in a New Module.
Function Qtravg()
Application.Volatile True
End Function
In This the code line Application Volatile True forces the function to recalculate every time
the cell or range value changes, the new UDF (User Defined Function) with name Qtravg
recalculate’s e er ti e.
In Cell F2 Type
= Qtravg()
23
Excel VBA Formulas for Spreadsheet
Use the FIX Function of Excel VBA code lines function to use it in Excel sheet ,Insert the below
code in a New Module.
Function Stdyr()
Stdyr = Fix(2016)
End Function
This Will Create A new UDF (User Defined Function) with name Stdyr() we Can use this As we
Required. type in Excel the function As = Stdyr(), the UDF Comes up in the year as 2016 which
is constant figure
In Cell A1 Type
= Stdyr(A1)
2. Defined Name
Define Name for Any cell With Value 2016 , T pe i a cell As =std r
24
Excel VBA Formulas for Spreadsheet
25
Excel VBA Formulas for Spreadsheet
FD = "Bastille Day"
Case Is = #8/15/1990#
FD = "Assomption Day"
Case Is = #11/1/2016#
FD = "La Toussaint"
Case Is = #11/11/2016#
FD = "Armistice Day"
Case Is = #12/25/2016#
FD = "Noel Day"
Case Is = #12/26/2016#
FD = "Cristmas Day(Alsace)"
Case #1/1/2016# To #12/31/2016#
FD = "Non-Festival Day"
End Select
FestivDay = FD
= FestivDay( (A1)
This Will Create A new UDF (User Defined Function) with name FestivDay( (A1)we Can use
this As we Required. type in Excel the function As = FestivDay( A1), the UDF Comes up in
s all letter’s to differe tiate itself fro “ta dard E cel For ula,if the da fall’s to Natio al
In Cell B1 Type
= FestivDay(A1)
26
Excel VBA Formulas for Spreadsheet
Syntax
=NOT(OR(A1>10,B1>10,C1>10))
27
Excel VBA Formulas for Spreadsheet
28
Excel VBA Formulas for Spreadsheet
Function Brickwall(Count)
Brickwall = (Count * 0.75) + (Count * 1.5)
End Function
In Cell B1 Type
= Brickwall(A1)
Syntax
29
Excel VBA Formulas for Spreadsheet
Function Formuladisplay(X)
Formuladisplay = X.Formula
End Function
= Hedqtrs( (C1)
Syntax
FORMULATEXT(reference)
=FORMULATEXT(C1)
30
Excel VBA Formulas for Spreadsheet
Function Hedqtrs(C)
If LCase(C) = "city" Then Hedqtrs = "Newyork"
End Function
= Hedqtrs( (A1)
Syntax
PROPER(text)
=IF(PROPER(A1)="City","NEWYORK"," ")
31
Excel VBA Formulas for Spreadsheet
Function Columnwidth(Z)
Columnwidth = Z.Columnwidth
End Function
=Columnwidth(A1)
In Excel Sheet
click on → Ho e → Cells → For at → Colu idth to know the size of the Column
32
Excel VBA Formulas for Spreadsheet
Function Rowheight(S)
Rowheight = S. Rowheight
End Function
In Cell B1 Type
= Rowheight(A1)
In Excel Sheet
33
Excel VBA Formulas for Spreadsheet
Function Mnthname(Mn)
Mnthname = MonthName(Mn, True)
End Function
=Monthname(A1)
Syntax
=CHOOSE(A1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12,C13)
34
Excel VBA Formulas for Spreadsheet
35
Excel VBA Formulas for Spreadsheet
Function Spce(W)
Spce = Space(W)
End Function
= Spce(3)
In D1 As =A1&B1&C1
36
Excel VBA Formulas for Spreadsheet
Conclusion
A User Defined function that is located in a code module in a workbook will go along with the
Workbook As long as you share. Likewise the function remains with the workbook and will
always be available to share.
If your workbook refers UDF you can save the Macro code –Module in Basic file (.bas) format
Mail the. bas file to someone with whom file you want the UDF to be available else you can
share the file also.
37
This book was distributed courtesy of:
For your own Unlimited Reading and FREE eBooks today, visit:
http://www.Free-eBooks.net
Share this eBook with anyone and everyone automatically by selecting any of the
options below:
COPYRIGHT INFORMATION
Free-eBooks.net respects the intellectual property of others. When a book's copyright owner submits their work to Free-eBooks.net, they are granting us permission to distribute such material. Unless
otherwise stated in this book, this permission is not passed onto others. As such, redistributing this book without the copyright owner's permission can constitute copyright infringement. If you
believe that your work has been used in a manner that constitutes copyright infringement, please follow our Notice and Procedure for Making Claims of Copyright Infringement as seen in our Terms
of Service here:
http://www.free-ebooks.net/tos.html