Option Explicit
Sub ThisIsASub()
MsgBox "This is a Sub" + vbNewLine _
+ "You can see it in the list of Macros" + vbNewLine _
+ "because it has no parameters." + vbNewLine _
+ "(Tools/Macro/Macros...)" + vbNewLine _
+ "It has no ""Return Value"""
End Sub
Sub ThisIsASubWithAParameter(InputParameter As String)
MsgBox "This is a Sub" + vbNewLine _
+ "You can NOT see it in the list of Macros" + vbNewLine _
+ "because it has a parameter." + vbNewLine _
+ "(Tools/Macro/Macros...)" + vbNewLine _
+ "It has no ""Return Value""" + vbNewLine _
+ "The parameter is: " & InputParameter
End Sub
Sub TestASub()
MsgBox "This is also a Sub" + vbNewLine _
+ "You can see it in the list of Macros" + vbNewLine _
+ "because it has no parameters." + vbNewLine _
+ "(Tools/Macro/Macros...)" + vbNewLine _
+ "It has no ""Return Value""" + vbNewLine _
+ "Calling Sub ""ThisIsASubWithAParameter""..."
ThisIsASubWithAParameter "Sheet333"
End Sub
Function DoubleANumber(ANumber As Integer) As Integer
DoubleANumber = 2 * ANumber
MsgBox "This is a Function" + vbNewLine _
+ "You can see it in the list of User-Defined" + vbNewLine _
+ "functions." + vbNewLine _
+ "(Insert/Function/User Defined)" + vbNewLine _
+ "It has a ""Return Value""" + vbNewLine _
+ "Use like any Excel function." + vbNewLine _
+ "e.g., in C1: ""=A1+DoubleANumber(B1)-1" + vbNewLine _
+ "It can also be used in VBA."
End Function
Sub DemoByValAndByRef()
Dim nPrice As Double
Dim nQuantity As Integer
Dim nCost As Double
nPrice = 4.52
nQuantity = 10
MsgBox "Price = " & nPrice & vbNewLine _
+ "Quantity = " & nQuantity
nCost = CalculateCost(nPrice, nQuantity)
MsgBox "Price = " & nPrice & vbNewLine _
+ "Quantity = " & nQuantity & vbNewLine _
+ "Cost = " & nCost & " (used a different Quantity)"
End Sub
Function CalculateCost(Price As Double, ByVal Quantity As Integer) As Double
' Try to change both Price and Quantity.
' We can change both, but only the new Price will "take"
' ByVal gives this routine a copy of the variable
' ByRef gives this routine a pointer to the variable
' (ByRef is the default)
Price = Price * 1.1
Quantity = Quantity * 2
CalculateCost = Price * Quantity
End Function