code input hm'
code input hm'
Dim isEditing As Boolean ' Untuk menandai apakah sedang dalam mode edit
Dim blnLoading As Boolean
Private originalHMAwal As Double
Private originalHMAkhir As Double
Private originalTotalHM As Double
Private originalSisaHM As Double
'=======================================
' TOMBOL BERSIHKAN
'=======================================
Private Sub cmdBersihkan_Click()
BersihkanForm
End Sub
Private Sub cmdEdit_Click()
If Me.lsDataInput.ListIndex = -1 Then
MsgBox "Pilih data yang akan diedit ", vbExclamation
Exit Sub
End If
selectedRow = Me.lsDataInput.ListIndex + 2
With ThisWorkbook.Sheets("InputDataHM")
'Isi nilai form
Me.txtTanggal.value = .Cells(selectedRow, 1).value
Me.cmNamaOperator.value = .Cells(selectedRow, 2).value
Me.cmNoUnit.value = .Cells(selectedRow, 3).value
Me.txtAlatBerat.value = .Cells(selectedRow, 4).value
Me.txtTipeAlatBerat.value = .Cells(selectedRow, 5).value
Me.txtMerkAlatBerat.value = .Cells(selectedRow, 6).value
Me.cmshift.value = .Cells(selectedRow, 7).value
Me.txtHMAwal.value = .Cells(selectedRow, 8).value
Me.txtHMAkhir.value = .Cells(selectedRow, 9).value
Me.txtTotalHm.value = .Cells(selectedRow, 10).value
Me.txtSisaHM.value = .Cells(selectedRow, 11).value
Me.txtKegiatan.value = .Cells(selectedRow, 12).value
isEditing = True
MsgBox "Edit data dan klik Simpan untuk menyimpan perubahan.", vbInformation
End Sub
CariHMTerbaru = maxHM
End Function
' Hitung Sisa HM hanya untuk data baru atau jika HM Akhir diubah saat edit
If Not isEditing Or (isEditing And CDbl(Me.txtHMAkhir.value) <>
originalHMAkhir) Then
Dim masterRow As Variant
masterRow = Application.Match(Me.cmNoUnit.value, masterWs.Range("D2:D" &
masterWs.Cells(masterWs.Rows.Count, "D").End(xlUp).Row), 0)
If IsError(masterRow) Then
MsgBox "No Unit tidak ditemukan di MasterAlat!", vbExclamation
Exit Sub
Else
Dim minimalCharge As Double
minimalCharge = masterWs.Cells(masterRow + 1, 13).value
Dim lastSisaHM As Double
lastSisaHM = GetLastSisaHM(Me.cmNoUnit.value, Me.txtTanggal.value)
sisaHM = lastSisaHM - totalHM
End If
End If
isEditing = False
BersihkanForm
LoadDataToListBox
End Sub
' Loop mundur untuk mencari data terakhir sebelum tanggal input
Dim i As Long
For i = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row To 2 Step -1
If ws.Cells(i, 3).value = noUnit And ws.Cells(i, 1).value < tanggal Then
previousSisaHM = ws.Cells(i, 11).value
Exit For
End If
Next i
' 2. Jika tidak ada data sebelumnya, ambil Minimal Charge dari MasterAlat
If previousSisaHM = -1 Then
Dim masterWs As Worksheet
Set masterWs = ThisWorkbook.Sheets("MasterAlat")
Dim masterRow As Variant
masterRow = Application.Match(noUnit, masterWs.Range("D2:D" &
masterWs.Cells(masterWs.Rows.Count, "D").End(xlUp).Row), 0)
GetLastSisaHM = sisaHM
End Function
Private Sub cmdTanggal_Click()
Call AdvancedCalendar
End Sub
Private Sub cmdKeluar_Click()
' Tutup UserForm saat ini
Me.Hide
End Sub
cmb.List = dict.Keys
End Sub
'=======================================
' FUNGSI BANTU
'=======================================
Sub LoadDataToListBox()
Dim ws As Worksheet
Set ws = ThisWorkbook.Sheets("InputDataHM")
Sub BersihkanForm()
Me.txtTanggal.value = ""
Me.cmNamaOperator.value = ""
Me.cmNoUnit.value = ""
Me.txtAlatBerat.value = ""
Me.txtTipeAlatBerat.value = ""
Me.txtMerkAlatBerat.value = ""
Me.cmshift.value = ""
Me.txtHMAwal.value = ""
Me.txtHMAkhir.value = ""
Me.txtSisaHM.value = ""
Me.txtTotalHm.value = ""
Me.txtKegiatan.value = ""
isEditing = False
End Sub
' Loop dari baris terakhir ke atas untuk mencari data terbaru
For i = lastRowInput To 2 Step -1
If wsInput.Cells(i, 3).value = noUnit Then
latestHMAkhir = wsInput.Cells(i, 9).value ' Kolom 9 (HM Akhir)
Exit For
End If
Next i
End Sub
Private Sub UpdateCalculations()
On Error Resume Next
Dim newHMAwal As Double
Dim newHMAkhir As Double
Dim newTotalHM As Double
Dim newSisaHM As Double