You are on page 1of 5

AN INTRODUCTION TO Visual Basic For Applications

Functions of variable arguments


Public Function SumAll(ParamArray var() As Variant) As Double
Dim i As Integer
Dim tmp As Double
For i = LBound(var) To UBound(var)
If IsNumeric(var(i)) Then tmp = tmp + var(i)
Next
SumAll = tmp
End Function
Sub test1()
Dim arr1, s
arr1 = Array(1, 2, 3)
s = SumAll(1, 2, 3)
MsgBox (s)
s = SumAll(1, 2, 3, 4)
MsgBox (s)
s = SumAll(-1.2, 2.1, 3, 1)
MsgBox (s)
End Sub

EXERCISE:

Create a function that display a 3 by 4 matrix in cells A1:D3, containing elements from 1 to 12 in two
ways:
Function matrix() As Variant
Dim v() As Variant
Dim N As Long
Dim r As Long
Dim c As Long
ReDim v(1 To 3, 1 To 4)
For r = 1 To 3
For c = 1 To 4
N = N + 1
v(r, c) = N
Cells(r, c) = v(r, c) 'this displays in horizontal order all the
numbers between 1 and 12 in a 3 by 4 matrix'
Next c
Next r
test = v
End Function

Function matrix2() As Variant


Dim v() As Variant
Dim N As Long
Dim r As Long
Dim c As Long
ReDim v(1 To 3, 1 To 4)
For c = 1 To 4
For r = 1 To 3
N = N + 1
v(r, c) = N
Cells(r, c + 6) = v(r, c) 'this displays in vertical order all the numbers
between 1 and 12 in a 3 by 4 matrix'
Next r
Next c
test = v
End Function

EXERCISE 2

Create a 3 by 3 matrix having as elements a(i,j)=i+j.

Given 2 positions from the inputbox, I and j,display a(I,j)

SOLUTION:
Sub display_matrix()
Dim arr(3, 3) As Variant
For i = 1 To 3
For j = 1 To 3
arr(i, j) = i + j
Next j
Next i
MsgBox (arr(1, 1))
Pos1=inputbox(enter the first position)
Pos2=inputbox(enter the second position)
MsgBox(arr(Pos1,Pos2))
End Sub

EXERCISE 3

Given 2 integers lower and upper(limits) create an array that displays all the elements from lower to
upper.

SOLUTION:
Function LoadNumbers(low As Long, High As Long) As Long()
Dim ResultArray() As Long
Dim Ndx As Long the index
Dim Val As Long
If low > High Then
Exit Function
End If
ReDim ResultArray(1 To (High - low + 1))
Val = low
For Ndx = LBound(ResultArray) To UBound(ResultArray)
ResultArray(Ndx) = Val
Val = Val + 1
Next Ndx
LoadNumbers = ResultArray() 'not ResultArray but with ()'
End Function

Sub Atest()
Dim arr() As Long
Dim N As Long
arr = LoadNumbers(low:=101, High:=110)
For N = LBound(arr) To UBound(arr)
MsgBox (arr(N))
Next N
End Sub

REDIM AND PRESERVE ARRAYS


Private Sub arrays()
Dim a() As Variant
i = 0
ReDim a(5)
a(0) = "XYZ"
a(1) = 41.25
a(2) = 22
ReDim Preserve a(7)
For i = 3 To 7
a(i) = i
Next
End Sub

EXERCISE 4

Create two functions:one that computes the sum of even numbers from a range and another that
computes the average of the elements from a range.
'Functions of ranges'
Function sumevennumbers(rng As Range)
'declare now a range object and call it cell'
Dim cell As Range
For Each cell In rng
If cell.Value Mod 2 = 0 Then
sumevennumbers = sumevennumbers + cell.Value
End If
Next 'this is for ending the For Each loop'
End Function

Function customAverage(rng As Range, low As Integer, up As Integer)


Dim cell As Range, total As Integer, count As Integer
For Each cell In rng
If cell.Value >= low And cell.Value <= up Then
total = total + cell.Value
count = count + 1
End If
Next
Application.Volatile 'this is a very precious call.Whenever an argument
changes,the return value of function
'changes
customAverage = total / count
End Function

EXERCISE 5

Create a Macro (Sub routine) that whenever you click on a cell, it prints down Sales,Production,Logistic
each one line under the other.

SOLUTION:
Sub typing()
'
' typing Macro
'

'
ActiveCell.FormulaR1C1 = "sales"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "production"
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.FormulaR1C1 = "logistics"
ActiveCell.Offset(1, 0).Range("A1").Select
End Sub

EXERCISE 6

Given a number entered from the input box, call it n,and a clicked cell, write a macro that starting from
that cell writes Hello on each cell, n times.

SOLUTION:
Sub typing4()
'
'typing4 Macro
'

'
n = InputBox("enter the number of cells")
For i = 1 To n
ActiveCell.FormulaR1C1 = "Hello"
ActiveCell.Offset(1, 0).Range("A1").Select
Next
End Sub
EMPTYSHEET and Message Boxes
'Message boxes'
Sub message_box1()
MsgBox "this is fun"
MsgBox "The entered values is" & Range("A1").Value
MsgBox "This is:" & vbNewLine & " a new message"
Application.Volatile
MsgBox "Range(A1).Value" & vbNewLine & Range("A1").Value & vbNewLine &
Range("A2").Value
End Sub
REMARK:& inside MsgBox is used for newline

'Empty Sheet application


Private Sub emptysheet_Click()
Dim answer As Integer
answer = MsgBox("Are you sure you want to empty the sheet?", vbYesNo +
vbQuestion, "Empty Sheet")
If answer = vbYes Then
Cells.ClearContents
Else
'do nothing
End If
End Sub

You might also like