Sie sind auf Seite 1von 5

14/08/2013 - IrDarmadiMM

Contoh(1) Contoh(2)
1.Value 2. Plus
‘ Commentar dapat ditulis dengan diawali oleh apostrophe (‘)
‘ Baris lanjutkan di bawahnya dengan memberi: _ ( Underscore )
Sub subPlus()
Application.ActiveWorkbook. _
Sub subValue() Worksheets(1).Cells(1, 3).Value = _
Application.ActiveWorkbook. _ Application.ActiveWorkbook. _
Worksheets(1).Cells(1, 1).Value = 1 Worksheets(1).Cells(1, 1).Value + _
End Sub Application.ActiveWorkbook. _
Worksheets(1).Cells(1, 2).Value
Private Sub Worksheet_Activate()
End Sub
Application.ActiveWorkbook. _
Worksheets(1).Cells(1, 1).Value = 1
End Sub

Contoh(3) Contoh(4)
3. Full Name 4. Message Box
Sub subFullName() Sub subMessageBox()
Application.ActiveWorkbook. _ ‘ keyboard ENTER dinyatakan dengan ( Chr(13) & Chr(10)) atau vbCrLf
Worksheets(1).Cells(1, 3).Value = _
MsgBox "Maintenance Department"
Application.ActiveWorkbook. _
Worksheets(1).Cells(1, 1).Value & _ MsgBox "Maintenance Department", vbCritical
MsgBox "Maintenance" & VBA.Constants.vbCrLf & "Department"
“, “ & _
MsgBox "Maintenance" & vbCrLf & "Department"
Application.ActiveWorkbook. _
End Sub
Worksheets(1).Cells(1, 2).Value
End Sub

1
14/08/2013 - IrDarmadiMM

Contoh(5) Contoh(6)
1.Value 2. Plus
Public Function funSetValue() As Byte Public Function funPlus _
funSetValue = 1 (aInput As Currency, _
End Function bInput As Currency) _
As Currency
Public Function funSetValue _
(intInput As Integer) As Byte funPlus = aInput + bInput
funSetValue = 1
End Function End Function

Contoh penggunaan
Contoh(7) Function , Call Sub-Routine
3. Full Name Private Sub subMessageBox(blnType As Boolean)
If blnType = True Then
Public Function funFullName _ Dim strMSG As String
(strFirstName As String, _ strMSG = funFullName("Parviz", "Habibi")
strLastName As String) _ MsgBox strMSG
As String Else
MsgBox "HSP"
funFullName = strFirstName & _ End If
", " & strLastName End Sub
End Function

Private Sub subCall()


Call subMessageBox(True)
End Sub

2
14/08/2013 - IrDarmadiMM

If … Contoh(9) Select Case Contoh(10)


Sub ifContoh1() Sub selectCase()
Dim curA As Currency Dim curA As Currency
curA = Application.ActiveWorkbook.ActiveSheet. _ curA = Application.ActiveWorkbook.ActiveSheet. _
Cells(1, 1).Value
Cells(1, 1).Value
Select Case curA
If curA = 0 Then
Case 0
MsgBox "Zero" MsgBox "Zero"
ElseIf curA = 1 Then Case 1
MsgBox "One" MsgBox "One"
Else Case Else
MsgBox "Others" MsgBox "Others"
End If End Select
End Sub
End Sub

For Next Loop Contoh (11) Do Until Loop Contoh (12)

•For Next •Do Until


Sub forLoop() Sub doUntil()
Dim iCounter As Byte Dim iCounter As Byte
For iCounter = 1 To 10 iCounter = 1
Debug.Print iCounter Do Until iCounter = 10
'If iCounter = 8 Then Exit For Debug.Print iCounter
Next iCounter iCounter = iCounter + 1
End Sub If iCounter = 9 Then Exit Do
Loop
End Sub

3
14/08/2013 - IrDarmadiMM

Do While Loop Contoh (13) For ... Next Contoh(14)


Sub StartForEach1()
Dim xCounter As Byte
•Do While 'VBAProject.ThisWorkbook.Worksheets.Count
For xCounter = 1 To 3
VBAProject.ThisWorkbook.Worksheets(xCounter).Range("B1").Value = 55
Sub doWhile() Next xCounter
Dim iCounter As Byte End Sub
iCounter = 1
Sub StartForEach2()
Do While iCounter < 11 Dim yCounter As Byte
Debug.Print iCounter Dim xCounter As Byte
' Range("C3:E6") = Range(Cells(3, 3), Cells(6, 5))
iCounter = iCounter + 1
For yCounter = 3 To 6
If iCounter = 9 Then Exit Do For xCounter = 3 To 5
Loop VBAProject.Sheet1.Cells(yCounter, xCounter).Value = 10
End Sub Next xCounter
Next yCounter
End Sub

For Each Contoh (15) Pemberian COLOR Contoh(16)


Sub ForEach1()
Dim aWorkSheet As Worksheet
For Each aWorkSheet In VBAProject.ThisWorkbook.Worksheets Sub startWith1()
aWorkSheet.Range("B1").Value = 55 VBAProject.Sheet1.Range("A1:D4").Value = 123
Next aWorkSheet
VBAProject.Sheet1.Range("A1:D4").Font.Color = _
End Sub
VBA.ColorConstants.vbRed
Sub ForEach2() VBAProject.Sheet1.Range("A1:D4").Font.Bold = True
Dim aCell As Range VBAProject.Sheet1.Range("A1:D4").Interior.Color = _
'Dim aRange As Range VBA.ColorConstants.vbGreen
'Set aRange = VBAProject.Sheet1.Range("C3:E6").Cells MsgBox VBAProject.Sheet1.Range("A1:D4").Count
'For Each aCell In aRange End Sub
For Each aCell In VBAProject.Sheet1.Range("C3:E6").Cells
'If aCell.Address = VBAProject.Sheet1.Range("E4").Address Then
' Exit For
'End If
aCell.Value = 10
Next aCell
End Sub

4
14/08/2013 - IrDarmadiMM

Penggunaan With Contoh (17) Penggunaan Forms


Sub With1()
With VBAProject.Sheet1.Range("A1:D4")
.Value = 123 •Insert ~ UserForm
With .Font
.Color = VBA.ColorConstants.vbRed •Properties
.Bold = True
End With

frm
.Interior.Color = VBA.ColorConstants.vbGreen
MsgBox .Count
End With
End Sub
frmMain.Show
Sub With2()
Dim myRange As Range
Unload ProjectGlobal.frmSandClock
Set myRange = VBAProject.Sheet1.Range("A1:D4")
With myRange With ProjectGlobal.frmExport
.Value = 123
MsgBox .Count .Left = Application.UsableWidth - .Width
End With .Top = Application.UsableHeight / 6
End Sub
End With

Penggunaan COMTROL
Contoh(18) Penggunaan Dim Contoh(24)
Private Sub txtLastName_Change() Sub countSubTasks()
Dim FirstName As String
Dim LastName As String Dim TSK As Task
Dim FullName As String Dim tCounter As Byte
FirstName = txtFirstName.Text
'tCounter = 0
LastName = txtLastName.Text
FullName = FirstName & " " & LastName For Each TSK In Application.ActiveProject.Tasks
txtFullName.Text = FullName If Not TSK Is Nothing Then
End Sub
TSK.Text12 = ""
Private Sub cmdStatsPrintPreview_Click() If TSK.Summary = False Then
'print tCounter = tCounter + 1
With VBAProject.Sheet4.PageSetup
If VBAProject.Sheet1.optPortrait.Value = True Then
TSK.Text12 = tCounter
.Orientation = xlPortrait End If
Else End If
.Orientation = xlLandscape
End If
Next TSK
End With MsgBox tCounter
VBAProject.Sheet4.PrintPreview End Sub
End Sub

Das könnte Ihnen auch gefallen