You are on page 1of 6

Nama : Taufik Ardhiansyah

NPM : 240110170110
Pemograman Komputer {Program VBA untuk contoh 2.1 Soal termodinamika}

MessageBox untuk N = 1

MessageBox untuk N > 0

MessageBox untuk N=0

Nama : Taufik Ardhiansyah
NPM : 240110170110
Pemograman Komputer {Tipe-tipe Data di VBA dan Fungsinya}

VBA Data Types

When you declare a variable, you should also identify its data type. You're probably already very
familiar with data types because you assign data types to table fields. VBA uses the same data
types to define a variable.

The most important job of a data type is to ensure the validity of your data. Specifying a data
type won't keep you from entering an invalid value, but it will keep you from entering an invalid
type. If you omit the data type, VBA applies the Variant data type to your variable—it's the most
flexible and VBA won't guess at what the data type should be. Table 3.1 compares VBA's many
data types.

Table 3.1 VBA Data Type Comparison

Data Required Default Value VBA Range

Type or Memory Constant
Integer 2 bytes 0 vbInteger –32,768 to 32,767
Long 4 bytes 0 vbLong –2,147,483,648 to 2,147,486,647
Single 4 bytes 0 vbSingle –3402823E38 to –1.401298E–45 or
1.401298E–45 to 3.402823E38
Double 8 bytes 0 vbDouble –1.79769313486232E308 to –
4.94065645841247E–324 or
1.79769313486232E308 to
Currency 8 bytes 0 vbCurrency –922,337,203,477.5808 to
Date 8 bytes 00:00:00 vbDate January 1, 100 to December 31, 9999
Fixed String's Number of vbString 1 to 65,400 characters
String length spaces to
Variable 10 bytes Zero- length vbString 0 to 2 billion characters
String plus the string ("")
Object 4 bytes Nothing vbObject Any Access object, ActiveX component
(vbNothing) or Class object
Boolean 2 bytes False vbBoolean –1 or 0
Variant 16 bytes Empty vbVariant Same as Double
Decimal 14 bytes 0 vbDecimal -
or –7.2998162514264337593543950335
to 7.9228162514264337593543950335
Byte 1 byte 0 vbByte 0 to 255

The Boolean Data Type

Use the Boolean numeric data type to store logical data that contains only two values: on and off,
true and value, yes and no, and so on. The keywords True and False are predefined constants and
are interchangeable with the values –1 and 0, respectively.

?True = 0
?True = -1
?False = 0
?False = -1
?True = False

The Byte Data Type

Byte is VBA's smallest numeric data type and holds a numeric value from 0 to 255. This data
type doesn't include any negative values. If you attempt to assign one, VBA returns an error.

The Currency Data Type

Use the Currency numeric data type to store monetary values from –922,337,203,477.5808 to
922,337,203,685,477.5807. A Currency data type results in a scaled value with accuracy to 15
digits to the left of the decimal point and 4 digits to the right. Use this data type to avoid
rounding errors when precision is of the utmost importance.
The Date Data Type

The Date data type stores a specially formatted numeric value that represents both the date and
time. You don't have to store both the date and time value. The Date data type accepts either the
date or the time, or both. Possible values range from January 1, 100 to December 31, 9999.

The Decimal Data Type

The Decimal data type is a subtype of Variant and not a truly separate data type all its own,
accommodating values from –79,228,162,514,264,337,593,543,950,335 to
79,228,162,514,264,337,593,543,950,335 if the value contains no decimal places. The data type
maintains precision up to 28 decimal places with values from –
7.9228162514264337593543950335 to 7.9228162514264337593543950335.

The Double Data Type

Use the Double data type to store precision floating point numbers from –
1.79769313486232E308 to –4.94065645841247E-324 or 1.79769313486232E308 to

The Integer Data Type

This is probably the most common data type in use, besides String. Use this data type to store
only whole numbers that range from –32,768 to 32,767.

The Long Data Type

The Long data type is also an Integer data type storing only whole numbers, but the range is
much larger than the traditional Integer data type. Use Long to store values from –2,147,483,648
to 2,147,486,647.

The Object Data Type

An Object variable is actually a reference to an Access object, such as a form, report, or control.
Or, the data type can reference an ActiveX component, or a class object created in a class

Class modules are covered briefly in "Introducing the VBA Modules," in Chapter 2 and in more
depth in "Introducing Objects," in Chapter 8.

You'll learn more about Object variables in " in Chapter 8, "Understanding Objects."
The Single Data Type

The Single data type stores precision numbers—numbers with decimal places or fractional
numbers. The data type is similar to Double, but the range is smaller. Use this data type to store
values from –3402823E38 to –1.401298E–45 or from 1.401298E–45 to 3.402823E38.

The String Data Type

String is another very common data type; it stores values or numbers, but treats them as text.
There are two varieties: fixed and variable. A fixed string can handle from 1 to 65,400
characters. To declare a fixed string, use the Dim statement in the form

Dim variablename As String * stringlength

In contrast, the variable String data type grows and shrinks as required to fit its stored value. By
default, all String variables are of this type. To declare this type, use the Dim statement in the

Dim variablename As String

The Variant Data Type

The Variant data type stores numeric and non-numeric values. This data type is the most flexible
of the bunch because it stores very large values of almost any type (matches the Double numeric
data type). Use it only when you're uncertain of the data's type or when you're accommodating
foreign data and you're not sure of the data type's specifications.

The Variant data type is VBA's default, so the following code interprets varValue as a Variant:

Dim varValue

Although the Variant data type is flexible, VBA processes these data types a little slower because
it must determine the most accurate data type for the assigned value. However, most likely,
you'll never notice the performance hit.

The biggest disadvantage is the data type's lack of readability. By that, we mean that you can't
easily determine the appropriate data type by viewing the code, and that can be a problem.

Option Explicit
Fungsi option explicit adalah untuk mendeklarasikan semua variabel sebelum variabel tersebut
digunakan. dengan option explicit akan sangat membantu programmer dalam mengurangi
kesalahan dan mengetahui letak kesalahan atau debug.contoh keuntungan ketika kita
menggunakan option explicit :

Private Sub CommandPesan_Click()

Dim PesanKu As String

Pesan = "Hello VB....." '<-- ada="" code="" kata="" kurang="" pada="" u="" variabel="" yg="">
MsgBox PesanKu

End Sub

Ketika program di run maka MessageBox tidak akan menampilkan pesan sesuai pada coding
tetapi menampilkan MessageBox kosong. Kemudian ketikan Option Expilicit di atas Private Sub
CommandPesan_Click(). Setelah itu runing lagi programnya maka akan muncul pesan kesalahan
“Variable not defined” yang berarti bahwa variabel tidak atau belum didefinisikan….

Ini adalah salah satu keuntungan menggunakan Option Explicit, Jadi programmer mengetahui
letak kesalahan. Mungkin ketika coding belum rumit masih mudah mencari kesalahan namun
ketika coding program sudah banyak dan rumit maka keuntungan dari option explicit sangat