0% found this document useful (0 votes)
14 views5 pages

Input BBM

Uploaded by

nutella001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views5 pages

Input BBM

Uploaded by

nutella001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Private Sub cmdTanggal_Click()

Call AdvancedCalendar
End Sub
'=======================================
' TOMBOL BERSIHKAN
'=======================================
Private Sub cmdBersihkan_Click()
BersihkanForm
End Sub
Private Sub cmshift_Change()
Dim wsInput As Worksheet
Dim kodeUnit As String
Dim shift As String
Dim tanggal As String
Dim lastRow As Long
Dim i As Long
Dim found As Boolean

'Kolom berdasarkan header (dinamis)


Dim colKodeUnit As Long
Dim colShift As Long
Dim colJenis As Long
Dim colType As Long
Dim colMerk As Long
Dim colTanggal As Long ' New column for Date

On Error GoTo ErrorHandler

'Set worksheet sumber data


Set wsInput = ThisWorkbook.Sheets("InputDataHM")

'Ambil input dari form


kodeUnit = Me.cmUnit.value
shift = Me.cmshift.value
tanggal = Me.txtTanggal.value ' Get the date input

'Validasi input
If Trim(kodeUnit) = "" Or Trim(shift) = "" Or Trim(tanggal) = "" Then
MsgBox "Harap isi Kode Unit, Shift, dan Tanggal terlebih dahulu!",
vbExclamation
Exit Sub
End If

'Cari posisi kolom berdasarkan header


With wsInput.Rows(1)
colKodeUnit = .Find(What:="No Unit", LookIn:=xlValues,
LookAt:=xlWhole).Column
colShift = .Find(What:="Jam Kerja", LookIn:=xlValues,
LookAt:=xlWhole).Column
colJenis = .Find(What:="Jenis Alat Berat", LookIn:=xlValues,
LookAt:=xlWhole).Column
colType = .Find(What:="Type Alat", LookIn:=xlValues,
LookAt:=xlWhole).Column
colOprt = .Find(What:="Nama Operator", LookIn:=xlValues,
LookAt:=xlWhole).Column
colMerk = .Find(What:="Merk Alat Berat", LookIn:=xlValues,
LookAt:=xlWhole).Column
colTanggal = .Find(What:="Tanggal", LookIn:=xlValues,
LookAt:=xlWhole).Column ' Find the Date column

End With

'Cari data
lastRow = wsInput.Cells(wsInput.Rows.Count, colKodeUnit).End(xlUp).Row
found = False

For i = 2 To lastRow
If UCase(wsInput.Cells(i, colKodeUnit).value) = UCase(kodeUnit) And _
UCase(wsInput.Cells(i, colShift).value) = UCase(shift) And _
wsInput.Cells(i, colTanggal).value = tanggal Then ' Check the selected
date

'Tampilkan data ke form


Me.lbljenisalat.Caption = wsInput.Cells(i, colJenis).value
Me.lbltypealat.Caption = wsInput.Cells(i, colType).value
Me.lblmerkalat.Caption = wsInput.Cells(i, colMerk).value
Me.lbloperator.Caption = wsInput.Cells(i, colOprt).value
found = True
Exit For
End If
Next i

If Not found Then


MsgBox "Data tidak ditemukan untuk:" & vbCrLf & _
"Kode Unit: " & kodeUnit & vbCrLf & _
"Shift: " & shift & vbCrLf & _
"Tanggal: " & tanggal, vbInformation, "Pemberitahuan"
End If

Exit Sub

ErrorHandler:
If Err.Number = 91 Then
MsgBox "Kolom header tidak ditemukan! Pastikan header berikut ada di sheet
InputDataHM:" & vbCrLf & _
"- Kode Unit" & vbCrLf & _
"- Shift" & vbCrLf & _
"- Jenis Alat" & vbCrLf & _
"- Type Alat" & vbCrLf & _
"- Merk Alat" & vbCrLf & _
"- Tanggal", vbCritical
Else
MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical
End If
End Sub

Private Sub UserForm_Initialize()


On Error Resume Next
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("InputDataHM")

' Jika sheet InputDataHM tidak ditemukan


If ws Is Nothing Then
MsgBox "Sheet InputDataHM tidak ditemukan!", vbCritical
Exit Sub
End If
' Isi ComboBox dengan data unik
FillUniqueCombo Me.cmUnit, ws.Range("C2:C" & ws.Cells(ws.Rows.Count,
"C").End(xlUp).Row)
FillUniqueCombo Me.cmshift, ws.Range("G2:G" & ws.Cells(ws.Rows.Count,
"G").End(xlUp).Row)

'Update Stok Akhir


UpdateStokAkhir

End Sub

Private Sub FillUniqueCombo(cmb As ComboBox, rng As Range)


Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")

Dim cell As Range


For Each cell In rng
If Not IsEmpty(cell.value) And Not dict.Exists(cell.value) Then
dict.Add cell.value, cell.value
End If
Next cell

cmb.List = dict.Keys

End Sub
Private Sub cmdSimpan_Click()
Dim wsIsi As Worksheet, wsBBM As Worksheet
Dim newStock As Double, previousStock As Double
Dim lastRowBBM As Long, lastRowIsi As Long

On Error GoTo ErrorHandler

' Validasi input wajib


If Me.txtTanggal = "" Or Me.cmUnit = "" Or Me.txtjumlah = "" Then
MsgBox "Isi Tanggal, No Unit, dan Jumlah BBM!", vbExclamation
Exit Sub
End If

' Set worksheet


Set wsBBM = ThisWorkbook.Sheets("BBMInput")
Set wsIsi = ThisWorkbook.Sheets("IsiBBM")

' 1. Ambil stok terakhir dari kolom I di BBMInput


lastRowBBM = wsBBM.Cells(wsBBM.Rows.Count, 9).End(xlUp).Row ' Kolom I = kolom
ke-9

If lastRowBBM >= 2 Then


previousStock = wsBBM.Cells(lastRowBBM, 9).value ' Ambil stok akhir
sebelumnya
Else
previousStock = 0 ' Jika data kosong, mulai dari 0
End If

' 2. Hitung stok baru (stok sebelumnya - jumlah yang digunakan)


newStock = previousStock - CDbl(Me.txtjumlah.value)

' 3. Simpan ke sheet IsiBBM


lastRowIsi = wsIsi.Cells(wsIsi.Rows.Count, 1).End(xlUp).Row + 1
With wsIsi
.Cells(lastRowIsi, 1).value = CDate(Me.txtTanggal.value)
.Cells(lastRowIsi, 2).value = Me.cmUnit.value
.Cells(lastRowIsi, 3).value = Me.lbloperator.Caption
.Cells(lastRowIsi, 4).value = Me.lbljenisalat.Caption
.Cells(lastRowIsi, 5).value = Me.lblmerkalat.Caption
.Cells(lastRowIsi, 6).value = Me.lbltypealat.Caption
.Cells(lastRowIsi, 7).value = Me.cmshift.value
.Cells(lastRowIsi, 8).value = Me.txtisi.value
.Cells(lastRowIsi, 9).value = CDbl(Me.txtjumlah.value)
.Cells(lastRowIsi, 10).value = Me.txtPenanggungJawab.value
.Cells(lastRowIsi, 11).value = newStock ' Kolom K
End With

' 4. Update stok di BBMInput (tambahkan baris baru di kolom I)


lastRowBBM = wsBBM.Cells(wsBBM.Rows.Count, 1).End(xlUp).Row + 1
wsBBM.Cells(lastRowBBM, 9).value = newStock ' Kolom I

' Bersihkan form


Me.txtTanggal = ""
Me.cmUnit = ""
Me.txtjumlah = ""

MsgBox "Data tersimpan! Stok baru: " & newStock & " liter", vbInformation
Exit Sub

ErrorHandler:
MsgBox "Error: " & Err.Description, vbCritical
End Sub

'Bersihkan Form
Private Sub ResetForm()
Me.txtTanggal.value = ""
Me.cmUnit.value = ""
Me.lbloperator.Caption = ""
Me.lbljenisalat.Caption = ""
Me.lblmerkalat.Caption = ""
Me.lbltypealat.Caption = ""
Me.cmshift.value = ""
Me.txtisi.value = ""
Me.txtjumlah.value = ""
Me.txtPenanggungJawab.value = ""

'Reset Form
Call ResetForm
End Sub
Private Sub UpdateStokAkhir()
Dim wsBBM As Worksheet
Dim lastRow As Long
Dim lastStock As Double

On Error GoTo ErrorHandler

' Set worksheet BBMInput


Set wsBBM = ThisWorkbook.Sheets("BBMInput")

' Cari baris terakhir di kolom I (Stok Akhir)


lastRow = wsBBM.Cells(wsBBM.Rows.Count, 9).End(xlUp).Row

' Ambil nilai stok terakhir


If lastRow >= 2 Then
lastStock = wsBBM.Cells(lastRow, 9).value
Else
lastStock = 0
End If

' Tampilkan di label


Me.lblstok.Caption = " " & Format(lastStock, "#,##0") & " liter"
Exit Sub

ErrorHandler:
MsgBox "Error saat mengambil stok: " & Err.Description, vbCritical
End Sub

' Optional: Jika ingin update stok saat label diklik


Private Sub lblstok_Click()
UpdateStokAkhir
End Sub

You might also like