VBA 4 Function and Sub Procedures
VBA 4 Function and Sub Procedures
ExcelFunctions.net
Search Site:
Custom Search
las
User-Defined VBA Function & Sub Procedures
las
In Excel Visual Basic, a set of commands to perform a
specific task is placed into a procedure, which can be a
n Excel
Function procedure or a Sub procedure (also known as
functions and subroutines).
Functions
Arguments
vs 2007
VBA procedures can be passed data via arguments,
which are declared in the procedure definition. For
example, you could have a VBA Sub procedure that
adds an Integer to every cell in the current selected
https://www.excelfunctions.net/vba-functions-and-subroutines.html 1/11
3/9/2020 VBA Function and Sub Procedures
Optional Arguments
You can also define VBA procedures to have Optional
arguments. These are arguments that the user can
supply if they want, but if they are omitted, the
procedure will assign a default value to them.
https://www.excelfunctions.net/vba-functions-and-subroutines.html 2/11
3/9/2020 VBA Function and Sub Procedures
https://www.excelfunctions.net/vba-functions-and-subroutines.html 3/11
3/9/2020 VBA Function and Sub Procedures
Function
.
.
.
End Function
End Function
Sub main()
End Sub
https://www.excelfunctions.net/vba-functions-and-subroutines.html 5/11
3/9/2020 VBA Function and Sub Procedures
=SumMinus(10, 5, 2)
Sub
.
.
.
End Sub
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.Font.Size = iFontSize
https://www.excelfunctions.net/vba-functions-and-subroutines.html 6/11
3/9/2020 VBA Function and Sub Procedures
End Sub
Selection.HorizontalAlignment = xlCenter
Selection.VerticalAlignment = xlCenter
Selection.Font.Bold = True
End Sub
Sub main()
Call Format_Centered_And_Sized( 20 )
End Sub
https://www.excelfunctions.net/vba-functions-and-subroutines.html 7/11
3/9/2020 VBA Function and Sub Procedures
Sub main()
End Sub
https://www.excelfunctions.net/vba-functions-and-subroutines.html 8/11
3/9/2020 VBA Function and Sub Procedures
https://www.excelfunctions.net/vba-functions-and-subroutines.html 9/11
3/9/2020 VBA Function and Sub Procedures
. preceded by
. the keyword
End Sub Public, this
makes the
procedure
accessible to
all other
modules in
the VBA
Project.
https://www.excelfunctions.net/vba-functions-and-subroutines.html 10/11
3/9/2020 VBA Function and Sub Procedures
VAT_Amount = 0
If sVAT_Rate <= 0 Then
MsgBox "Expected a Positive value of sVAT_Rate but
Received " & sVAT_Rate
Exit Function
End If
.
.
.
End Function
Statements
https://www.excelfunctions.net/vba-functions-and-subroutines.html 11/11