The document discusses user defined functions (UDFs) in VBA for Excel. It provides examples of sub procedures, functions, and how to create custom functions. UDFs allow users to define their own functions to perform calculations that may not be covered by existing functions. The examples demonstrate simple UDFs for calculating sums, factorials, and day of the week from a date. It also shows how UDFs can call other UDFs. The document provides guidance on writing UDFs and examples of using UDFs in Excel.
2. 19/03/2013
REVIEW PROCEDURE DALAM VBA
A Sub procedure is a series of Visual Basic statements enclosed by the Sub and End Sub
statements that performs actions but doesn't return a value. A Sub procedure can take
arguments, such as constants, variables, or expressions that are passed by a calling
procedure. If a Sub procedure has no arguments, the Sub statement must include an
empty set of parentheses.
Sub Prosedur1(a) ’Prosedur berparameter a
MsgBox a
End Sub
Sub Prosedur2() ’Prosedur tak berparamerter
Dim text As String
text = InputBox("masukan text")
Call Prosedur1(text) ’Pemanggilan prosedur1
End Sub
Sub Prosedur3()
Call Prosedur2 ’Pemanggilan Prosedur2
End Sub 2
3. 19/03/2013
UDF (USER DEFINED FUNCTION)
Most calculations can be achieved with what is provided, but it
isn't long before you find yourself wishing that there was a
function that did a particular job, and you can't find anything
suitable in the list. You need a UDF.
A UDF (User Defined Function) is simply a function that you
create yourself with VBA. UDFs are often called "Custom
Functions". A UDF can remain in a code module attached to a
workbook, in which case it will always be available when that
workbook is open. Alternatively you can create your own add-in
containing one or more functions that you can install into Excel
just like a commercial add-in.
UDFs can be accessed by code modules too. Often UDFs are
created by developers to work solely within the code of a VBA
procedure and the user is never aware of their existence.
Like any function, the UDF can be as simple or as complex as you
want. Let's start with an easy one...
3
4. 19/03/2013
A Function procedure is a series of Visual Basic statements enclosed by the
Function and End Function statements. A Function procedure is similar to a Sub
procedure, but a function can also return a value. A Function procedure can take
arguments, such as constants, variables, or expressions that are passed to it by a
calling procedure. If a Function procedure has no arguments, its Function
statement must include an empty set of parentheses. A function returns a value by
assigning a value to its name in one or more statements of the procedure.
In the following example, the Celsius function calculates degrees Celsius from
degrees Fahrenheit. When the function is called from the Main procedure, a
variable containing the argument value is passed to the function. The result of the
calculation is returned to the calling procedure and displayed in a message box.
Example:
Sub Main()
temp = Application.InputBox(Prompt:= _
"Please enter the temperature in degrees F.", Type:=1)
MsgBox temp & “Fahrenheit = " & Celsius(temp) & " deraja Celcius."
End Sub
Function Celsius(fDegrees as single) as single
Celsius = (fDegrees - 32) * 5 / 9
End Function
4
6. 19/03/2013
FUNCTION (FUNGSI)
Fungsi merupakan bagian program yang
menghasilkan suatu nilai pada nama fungsi
tersebut.
Ada dua jenis fungsi
Fungsi standar (fungsi yang sudah tersedia dalam
bahasa program yang digunakan)
Fungsi terdefinisi (fungsi yang didefinisikan oleh
pengguna / User Defined Function)
6
7. 19/03/2013
FUNSI STANDAR
Fungsi standar yang disediakan excel jumlahanya
bisa mencapai ribuan
Fungsi standar bisa kita pakai atau panggil dengan
perintah Formula Tab Insert functions atau dengan
mengklik ikon pada Ribbon Formula
Di dalam excel 2007 fungsi standar dikelompokan
berdasarkan grup Function Library yang terdiri dari:
Financial; Logical; Text; Date&time; Statistical;
Lookup&Reference; Math&Trig; Database;
Information
7
8. 19/03/2013
UDF (USER DEFINED FUNCTION)
UDF merupakan fungsi yang didefinisikan atau dibuat oleh
user
UDF dibuat dengan tujuan
untuk mempersingkat program jika membutuhkan perhitungan-
perhitungan yang dilakukan berulang kali dengan perintah yang
sama,
Untuk mengatasi ketidak tersediaan fungsi standar misalnya
membuat rumus sendiri
Suatu fungsi dapat dipanggil oleh prosedur, fungsi lain atau
fungsi itu sendiri
UDF dapat dipanggil baik dalam jendela Excel maupun di
jendela VBA dengan syarat dibuat dalam module
8
9. 19/03/2013
CONTOH FUNGSI (UDF) SEDERHANA
Sub HasilJumlah()
Dim a As Integer, b As Integer
a = InputBox("Angka ke-1")
b = InputBox("angka ke-2")
MsgBox “jumlah “ & a & “ dan “ & b & “ adalah “ & _
Jumlah(a, b)
End Sub
Function Jumlah(x As Integer, y As Integer)
Jumlah = x + y
End Function
9
10. 19/03/2013
CONTOH UDF (2)
Function Faktorial(ByVal y As Integer) as long ‘Deklarasi nama fungsi
If y = 0 Then
Faktorial = 1
Exit Function
End If
y=y-1
Faktorial = Faktorial(y) * (y + 1)
End Function
Contoh Prosedur yang memangil/menggunakan UDF
Sub NilaiFaktorial() 'Deklarasi nama prosedur
Dim Masukan As integer, Hasil As integer 'Deklarasi variable bertipe integer
Masukan = InputBox(“Inputkan Nilai yang akan dicari faktorialnya :", _ "Faktorial")
Hasil = Faktorial(Masukan) 'pemakaian fungsi yang hasilnya disimpan di variable
MsgBox ("Nilai faktorial dari " & Masukan & " adalah " & Hasil)
End Sub
10
11. CONTOH UDF(2)
19/03/2013
Function NmHari(Isitgl As byte) Case 4
Dim HariKe As byte NmHari = "Rabu"
HariKe = Weekday(Isitgl, Case 5
vbSunday) NmHari = "Kamis"
Select Case HariKe Case 6
Case 1 NmHari = "Jum'at"
NmHari = "Ahad" Case 7
Case 2 NmHari = "Sabtu"
NmHari = "Senin" End Select
Case 3 End Function
11
12. 19/03/2013
Sub NamaHari() Sub Format1()
Dim i As Byte With Selection
Cells(1, 1) = "Hari ke" .HorizontalAlignment = xlCenter
Cells(1, 2) = "Nama Hari"
.VerticalAlignment = xlCenter
For i = 1 To 7
Cells(i + 1, 1) = i
.EntireColumn.AutoFit
Cells(i + 1, 2) = NmHari(i) .Borders.LineStyle = xlContinuous
Next i End With
Range("A1:B8").Select End Sub
Format1
Range("A1:B1").Select Sub Warna()
Warna With Selection.Interior
Cells(2, 4).Select .Pattern = xlSolid
End Sub
.PatternColorIndex = xlAutomatic
.Color = 65535
End With
End Sub
12
13. 19/03/2013
CONTOH (3) UDF DIPANGGIL DI UDF
Function CtoF(C As Single) As Single Function FtoC(F As Single) As Single
CtoF = C * 1.8 + 32 FtoC = (F - 32) * 5 / 9
End Function End Function
Function CtoK(C As Single) As Single Function KtoC(K As Single) As Single
CtoK = C + 273 KtoC = K - 273
End Function End Function
Function CtoR(C As Single) As Single Function RtoC(R As Single) As Single
CtoR = C * 0.8 RtoC = R * 5 / 4
End Function End Function
Function KtoR(K As Single) As Single
KtoR = KtoC(CtoR(K))
End Function
13
16. 19/03/2013
CONTOH FUNGSI UNTUK MENGHITUNG DERET
Function DeretAsli(Angka As Integer)
Dim i As Integer
For i = 1 To Angka
DeretAsli = DeretAsli + i
Next i
End Function
Function DeretGanjil(Angka As Integer)
Dim i As Integer
For i = 1 To Angka Step 2
DeretGanjil = DeretGanjil + i
Next i
End Function
Function DeretGenap(Angka As Integer)
Dim i As Integer
For i = 0 To Angka Step 2
DeretGenap = DeretGenap + i
Next i
End Function 16
17. 19/03/2013
LATIHAN DAN TUGAS
Buatlah fungsi-fungsi untuk
persamaan-perasamaan berikut:
GLBB
GMB
Momentum
Gaya
Usaha
Energi
Pers. Gelombang
17