Video : Mengurutkan Data Metode 2: Menggunakan Algoritma Pemrograman
Tutorial Level 6
Berikut adalah kode programnya:
Sub UrutkanDataCara2()
Dim WB1 As Workbook
Dim WS1_1 As Worksheet
Dim BrsAkhr As Long, i As Long
Dim ArrNo() As Long, ArrNama() As String, ArrNilai() As Long
Dim BlmUrut As Boolean
Dim Temp1, Temp2, Temp3
Set WB1 = ActiveWorkbook
Set WS1_1 = ActiveSheet
With WS1_1
BrsAkhr = .Range("A" & .Rows.Count).End(xlUp).Row
ReDim ArrNo(BrsAkhr - 1) As Long
ReDim ArrNama(BrsAkhr - 1) As String
ReDim ArrNilai(BrsAkhr - 1) As Long
For i = 2 To BrsAkhr
ArrNo(i - 1) = .Cells(i, 1).Value
ArrNama(i - 1) = .Cells(i, 2).Value
ArrNilai(i - 1) = .Cells(i, 3).Value
Next i
If ((Not Not ArrNo) <> 0) Then
BlmUrut = True
Do
BlmUrut = False
For i = 1 To UBound(ArrNo) - 1
If ArrNama(i) > ArrNama(i + 1) Then
Temp1 = ArrNo(i)
Temp2 = ArrNama(i)
Temp3 = ArrNilai(i)
ArrNo(i) = ArrNo(i + 1)
ArrNama(i) = ArrNama(i + 1)
ArrNilai(i) = ArrNilai(i + 1)
ArrNo(i + 1) = Temp1
ArrNama(i + 1) = Temp2
ArrNilai(i + 1) = Temp3
BlmUrut = True
Exit For
End If
Next i
Loop While BlmUrut = True
End If
If ((Not Not ArrNo) <> 0) Then
BlmUrut = True
Do
BlmUrut = False
For i = 1 To UBound(ArrNo) - 1
If ArrNilai(i) < ArrNilai(i + 1) Then
Temp1 = ArrNo(i)
Temp2 = ArrNama(i)
Temp3 = ArrNilai(i)
ArrNo(i) = ArrNo(i + 1)
ArrNama(i) = ArrNama(i + 1)
ArrNilai(i) = ArrNilai(i + 1)
ArrNo(i + 1) = Temp1
ArrNama(i + 1) = Temp2
ArrNilai(i + 1) = Temp3
BlmUrut = True
Exit For
End If
Next i
Loop While BlmUrut = True
End If
For i = 2 To BrsAkhr
.Cells(i, 1).Value = ArrNo(i - 1)
.Cells(i, 2).Value = ArrNama(i - 1)
.Cells(i, 3).Value = ArrNilai(i - 1)
Next i
End With
End Sub