SlideShare a Scribd company logo
1 of 17
Download to read offline
19/03/2013




USER DEFINED FUNCTION (UDF)
VBA FOR EXCEL 2007/2010
Komputer 2 Pertemuan 6
UIN SGD Bandung
Selasa, 19 Maret 2013




                                       1
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
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
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
19/03/2013




             5
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
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
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
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
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
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
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
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
19/03/2013




CONTOH PENGGUNAAN DI EXCEL




                                     14
19/03/2013


BUATLAH PROGRAM SEPERTI TAMPILAN
BERIKUT




                                         15
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
19/03/2013




LATIHAN DAN TUGAS

        Buatlah fungsi-fungsi untuk
         persamaan-perasamaan berikut:
          GLBB
          GMB

          Momentum

          Gaya

          Usaha

          Energi

          Pers. Gelombang


                                              17

More Related Content

What's hot

Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)RichardWarburton
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1Teksify
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2rohassanie
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2Hariz Mustafa
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingHariz Mustafa
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6rohassanie
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesRasan Samarasinghe
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondMario Fusco
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language InnovationsShahriar Hyder
 

What's hot (20)

Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)Pragmatic functional refactoring with java 8 (1)
Pragmatic functional refactoring with java 8 (1)
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
C++lecture9
C++lecture9C++lecture9
C++lecture9
 
data Structure Lecture 1
data Structure Lecture 1data Structure Lecture 1
data Structure Lecture 1
 
FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2FP 201 - Unit4 Part 2
FP 201 - Unit4 Part 2
 
Lecture02 class -_templatev2
Lecture02 class -_templatev2Lecture02 class -_templatev2
Lecture02 class -_templatev2
 
Computer Programming- Lecture 9
Computer Programming- Lecture 9Computer Programming- Lecture 9
Computer Programming- Lecture 9
 
Lecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handlingLecture05 operator overloading-and_exception_handling
Lecture05 operator overloading-and_exception_handling
 
FP 201 - Unit 6
FP 201 - Unit 6FP 201 - Unit 6
FP 201 - Unit 6
 
Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6Understanding Subroutines and Functions in VB6
Understanding Subroutines and Functions in VB6
 
Computer Programming- Lecture 4
Computer Programming- Lecture 4Computer Programming- Lecture 4
Computer Programming- Lecture 4
 
Lecture 12: Classes and Files
Lecture 12: Classes and FilesLecture 12: Classes and Files
Lecture 12: Classes and Files
 
Operators
OperatorsOperators
Operators
 
DIWE - Working with MySQL Databases
DIWE - Working with MySQL DatabasesDIWE - Working with MySQL Databases
DIWE - Working with MySQL Databases
 
Commons Nabla
Commons NablaCommons Nabla
Commons Nabla
 
Op ps
Op psOp ps
Op ps
 
FP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyondFP in Java - Project Lambda and beyond
FP in Java - Project Lambda and beyond
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
C# 3.0 Language Innovations
C# 3.0 Language InnovationsC# 3.0 Language Innovations
C# 3.0 Language Innovations
 
Functions
FunctionsFunctions
Functions
 

Similar to Materi 6 user definedfunction

Similar to Materi 6 user definedfunction (20)

Unit-III.pptx
Unit-III.pptxUnit-III.pptx
Unit-III.pptx
 
Mastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_argumentsMastering Python lesson 4_functions_parameters_arguments
Mastering Python lesson 4_functions_parameters_arguments
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 
functions- best.pdf
functions- best.pdffunctions- best.pdf
functions- best.pdf
 
ForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptxForLoopandUserDefinedFunctions.pptx
ForLoopandUserDefinedFunctions.pptx
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Function in cpu 2
Function in cpu 2Function in cpu 2
Function in cpu 2
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
 
Chapter 1.ppt
Chapter 1.pptChapter 1.ppt
Chapter 1.ppt
 
User defined functions in matlab
User defined functions in  matlabUser defined functions in  matlab
User defined functions in matlab
 
Function
FunctionFunction
Function
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
C functions list
C functions listC functions list
C functions list
 
C++ Functions
C++ FunctionsC++ Functions
C++ Functions
 
Function BPK2
Function BPK2Function BPK2
Function BPK2
 
Chapter 4
Chapter 4Chapter 4
Chapter 4
 
Unit 8
Unit 8Unit 8
Unit 8
 
Module 3-Functions
Module 3-FunctionsModule 3-Functions
Module 3-Functions
 
Functions
FunctionsFunctions
Functions
 

More from Al Frilantika

Hakekat fisika dan keselamantan kerja di laboratorium
Hakekat fisika dan keselamantan kerja di laboratoriumHakekat fisika dan keselamantan kerja di laboratorium
Hakekat fisika dan keselamantan kerja di laboratoriumAl Frilantika
 
Solusi ukk mat xi ipa 2015 2016
Solusi ukk mat xi ipa 2015 2016Solusi ukk mat xi ipa 2015 2016
Solusi ukk mat xi ipa 2015 2016Al Frilantika
 
Solusi ukk mat ips 2015 2016
Solusi ukk mat ips 2015 2016Solusi ukk mat ips 2015 2016
Solusi ukk mat ips 2015 2016Al Frilantika
 
Ukk fisika xi 2015 2016 solusi
Ukk fisika xi 2015 2016 solusiUkk fisika xi 2015 2016 solusi
Ukk fisika xi 2015 2016 solusiAl Frilantika
 
Solusi UKK Fisika MIA X 2016
Solusi UKK Fisika MIA X 2016Solusi UKK Fisika MIA X 2016
Solusi UKK Fisika MIA X 2016Al Frilantika
 
Optikal geometri Kelas X
Optikal geometri Kelas XOptikal geometri Kelas X
Optikal geometri Kelas XAl Frilantika
 
Soal dan pembahasan UAM 2016
Soal dan pembahasan UAM  2016Soal dan pembahasan UAM  2016
Soal dan pembahasan UAM 2016Al Frilantika
 
Uas gasal fis xii 2014 2015
Uas gasal fis xii 2014 2015Uas gasal fis xii 2014 2015
Uas gasal fis xii 2014 2015Al Frilantika
 
Uas ganjil Fisika MA kls XI thn 2014 2015
Uas ganjil  Fisika MA kls XI thn 2014 2015Uas ganjil  Fisika MA kls XI thn 2014 2015
Uas ganjil Fisika MA kls XI thn 2014 2015Al Frilantika
 
Uas ganji fisika x 2014 2015 solusi
Uas ganji fisika x 2014 2015 solusiUas ganji fisika x 2014 2015 solusi
Uas ganji fisika x 2014 2015 solusiAl Frilantika
 
Daftar Nilai TIK 2013 2014
Daftar Nilai TIK 2013 2014Daftar Nilai TIK 2013 2014
Daftar Nilai TIK 2013 2014Al Frilantika
 
Solusi ukk fisika xi 2013 2014
Solusi ukk fisika xi 2013 2014Solusi ukk fisika xi 2013 2014
Solusi ukk fisika xi 2013 2014Al Frilantika
 
Solusi ukk mat xi ips 2014 tanpa opsi
Solusi ukk mat xi ips 2014 tanpa opsiSolusi ukk mat xi ips 2014 tanpa opsi
Solusi ukk mat xi ips 2014 tanpa opsiAl Frilantika
 
Sousi UKK Fisika x tahun 2014
Sousi UKK Fisika x tahun 2014Sousi UKK Fisika x tahun 2014
Sousi UKK Fisika x tahun 2014Al Frilantika
 
Nilai komp ii 2013 fis a b
Nilai komp ii 2013 fis a bNilai komp ii 2013 fis a b
Nilai komp ii 2013 fis a bAl Frilantika
 
N ilai komp pbi a 2012 2013
N ilai komp pbi a 2012 2013N ilai komp pbi a 2012 2013
N ilai komp pbi a 2012 2013Al Frilantika
 
Dhn tik pendidikan 2013
Dhn tik pendidikan 2013Dhn tik pendidikan 2013
Dhn tik pendidikan 2013Al Frilantika
 
Materi 5 conditional
Materi 5 conditionalMateri 5 conditional
Materi 5 conditionalAl Frilantika
 

More from Al Frilantika (20)

Hakekat fisika dan keselamantan kerja di laboratorium
Hakekat fisika dan keselamantan kerja di laboratoriumHakekat fisika dan keselamantan kerja di laboratorium
Hakekat fisika dan keselamantan kerja di laboratorium
 
Solusi ukk mat xi ipa 2015 2016
Solusi ukk mat xi ipa 2015 2016Solusi ukk mat xi ipa 2015 2016
Solusi ukk mat xi ipa 2015 2016
 
Solusi ukk mat ips 2015 2016
Solusi ukk mat ips 2015 2016Solusi ukk mat ips 2015 2016
Solusi ukk mat ips 2015 2016
 
Ukk fisika xi 2015 2016 solusi
Ukk fisika xi 2015 2016 solusiUkk fisika xi 2015 2016 solusi
Ukk fisika xi 2015 2016 solusi
 
Solusi UKK Fisika MIA X 2016
Solusi UKK Fisika MIA X 2016Solusi UKK Fisika MIA X 2016
Solusi UKK Fisika MIA X 2016
 
Optikal geometri Kelas X
Optikal geometri Kelas XOptikal geometri Kelas X
Optikal geometri Kelas X
 
Suhu dan kalor
Suhu dan kalorSuhu dan kalor
Suhu dan kalor
 
Soal dan pembahasan UAM 2016
Soal dan pembahasan UAM  2016Soal dan pembahasan UAM  2016
Soal dan pembahasan UAM 2016
 
Uas gasal fis xii 2014 2015
Uas gasal fis xii 2014 2015Uas gasal fis xii 2014 2015
Uas gasal fis xii 2014 2015
 
Uas ganjil Fisika MA kls XI thn 2014 2015
Uas ganjil  Fisika MA kls XI thn 2014 2015Uas ganjil  Fisika MA kls XI thn 2014 2015
Uas ganjil Fisika MA kls XI thn 2014 2015
 
Uas ganji fisika x 2014 2015 solusi
Uas ganji fisika x 2014 2015 solusiUas ganji fisika x 2014 2015 solusi
Uas ganji fisika x 2014 2015 solusi
 
Nilai komp ii 2014
Nilai komp ii 2014Nilai komp ii 2014
Nilai komp ii 2014
 
Daftar Nilai TIK 2013 2014
Daftar Nilai TIK 2013 2014Daftar Nilai TIK 2013 2014
Daftar Nilai TIK 2013 2014
 
Solusi ukk fisika xi 2013 2014
Solusi ukk fisika xi 2013 2014Solusi ukk fisika xi 2013 2014
Solusi ukk fisika xi 2013 2014
 
Solusi ukk mat xi ips 2014 tanpa opsi
Solusi ukk mat xi ips 2014 tanpa opsiSolusi ukk mat xi ips 2014 tanpa opsi
Solusi ukk mat xi ips 2014 tanpa opsi
 
Sousi UKK Fisika x tahun 2014
Sousi UKK Fisika x tahun 2014Sousi UKK Fisika x tahun 2014
Sousi UKK Fisika x tahun 2014
 
Nilai komp ii 2013 fis a b
Nilai komp ii 2013 fis a bNilai komp ii 2013 fis a b
Nilai komp ii 2013 fis a b
 
N ilai komp pbi a 2012 2013
N ilai komp pbi a 2012 2013N ilai komp pbi a 2012 2013
N ilai komp pbi a 2012 2013
 
Dhn tik pendidikan 2013
Dhn tik pendidikan 2013Dhn tik pendidikan 2013
Dhn tik pendidikan 2013
 
Materi 5 conditional
Materi 5 conditionalMateri 5 conditional
Materi 5 conditional
 

Materi 6 user definedfunction

  • 1. 19/03/2013 USER DEFINED FUNCTION (UDF) VBA FOR EXCEL 2007/2010 Komputer 2 Pertemuan 6 UIN SGD Bandung Selasa, 19 Maret 2013 1
  • 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
  • 15. 19/03/2013 BUATLAH PROGRAM SEPERTI TAMPILAN BERIKUT 15
  • 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