嗨我对vb相对较新,请原谅不良代码,
我希望代码计算每小时使用的升数。
用户形象要求用户输入机器小时数以及当天投入机器的燃油量(需要升数)。它还要求用户从组合框中选择机器编号,并从组合框中选择位置。
然后使用一个工作表来记录所有这些条目(sheet1),然后将sheet2中的信息更新为最新信息。
我需要代码在信息更新之前花费当前时间,并将其从新的当前小时数中删除,这将更新当前小时数。然后将所需的升数除以先前计算的值。
然后我希望将此值放入sheet2中的列升/小时。
这是我目前的代码:
Private Sub btnupdate_Click()
Dim row As Long 'next available row
With Sheets("Sheet1")
'get the next avialable row in Sheet1
row = .Range("A" & .Rows.Count).End(xlUp).row + 1
'put the text box values in this row
.Range("A" & row).Value = cmbplantno.Value
.Range("B" & row).Value = Txthours.Value
.Range("C" & row).Value = txtfuel.Value
.Range("D" & row).Value = caldate.Value
.Range("E" & row).Value = cmblocation.Value
End With
Dim TheLastRow As Long
TheLastRow = Sheet1.UsedRange.SpecialCells(xlCellTypeLastCell).row
Rows(TheLastRow).Select
Selection.Copy
ActiveWorkbook.Sheets("Sheet2").Activate
Columns("A:A").Select
Selection.AutoFilter
ActiveSheet.Range("$A$1:$A$63").AutoFilter Field:=1, Criteria1:=cmbplantno
Range("A2:A63") = cmbplantno.Value
Range("E2:E63") = Txthours.Value
Range("F2:F63") = caldate.Value
Range("M2:M63") = cmblocation.Value
Range("N2:N63") = txtfuel.Value
Rows("63:63").Select
Selection.Delete Shift:=xlUp
ActiveSheet.Range("$A$1:$A$63").AutoFilter Field:=1
cmbplantno.Value = ""
Txthours.Value = ""
txtfuel.Value = ""
cmblocation.Value = ""
End Sub
Sub FillCtrlUnique(myRange As Range, myControl As Control)
Dim Coll As New Collection
Dim var As Variant
Dim cell As Range
On Error Resume Next
For Each cell In myRange
Coll.Add Item:=cell.Value, Key:=CStr(cell.Value)
Next cell
On Error GoTo 0
With myControl
.Clear
For Each var In Coll
.AddItem var
Next var
End With
Set Coll = Nothing
End Sub
Private Sub UserForm_Initialize()
Dim sRange As Range
Set sRange = Range("E2:E62", Range("E63"))
FillCtrlUnique sRange, Me.cmblocation
With Worksheets("Sheet1")
cmbplantno.List = .Range("A2:A62" & .Range("A" & .Rows.Count)).Value
End With
End Sub
Private Sub UserForm_Terminate()
ThisWorkbook.Close savechanges:=True
Application.Quit
End Sub
编辑1:工作表示例
对此的任何帮助将不胜感激。我会把头发拉了好几个星期,试图自己解决这个问题。