我正在使用Excel“数据库”开发项目,以跟踪项目的生产。有时人们会多次扫描某件商品,这会导致在发送给客户时出现问题。
我有一个警报弹出窗口,以防止人们多次扫描某个项目,除非进行返工。如果该项目存在于“数据库”中,则存在带有vbYesNo按钮的MsgBox。如果人们单击“是”,则是重做。如果人们单击“否”,这是一个错误,他们退出了子目录。
我需要一种方法来处理返工,并使其更改与原始项目相同的行中单元格的值。
这是我到目前为止的代码。
Private Sub gbatchd_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 'Check DB for duplicate value
Set depo = dbgrids.Sheets("Deposition")
Set found = depo.Columns("A").Find(what:=valuetofind, LookIn:=xlValues, lookat:=xlWhole)
valuetofind = gbatchd.Text
FR = depo.Range("A" & Rows.Count).End(xlUp).Row
If KeyCode = 13 Then
For i = 1 To FR
If gbatch.Cells(i, 1).Value = valuetofind Then
MsgBox "This batch has already been deposited!" & vbCrLf & "Rework?", vbYesNo, "Rework?"
If answer = vbNo Then
Exit Sub
Else
depo.Cells(found.Row, 5).Value = "Rework"
End If
End If
Next
End If
End Sub
答案 0 :(得分:1)
撇开对解决方案体系结构的任何批评,您的代码丢失的是分配给answer
变量:
answer = MsgBox("This batch has already been deposited!" & vbCrLf & "Rework?", vbYesNo, "Rework?")
使用Option Explicit
并使用适当的类型(check out this article)声明变量。
代码的清理版本可能看起来像这样:
Option Explicit 'At the very top of your module.
'... Other code ...
Private Sub gbatchd_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer) 'Check DB for duplicate value
Dim wsDepo As Excel.Worksheet
Dim rngFound As Excel.Range
Dim sValueToFind As String
Dim answer As VbMsgBoxResult
If KeyCode = KeyCodeConstants.vbKeyReturn Then
'Barcode reader has sent the Enter (Return) key.
'Attempt to find the value.
sValueToFind = Trim(gbatchd.Text)
Set wsDepo = dbgrids.Worksheets("Deposition")
Set rngFound = wsDepo.Columns(1).Find(What:=sValueToFind, LookIn:=xlValues, LookAt:=xlWhole)
If Not rngFound Is Nothing Then
'Value was found. Ask whether it is a rework.
answer = MsgBox("This batch has already been deposited!" & vbCrLf & "Rework?", vbYesNo, "Rework?")
If answer = VbMsgBoxResult.vbYes Then
wsDepo.Cells(rngFound.Row, 5).Value = "Rework"
End If
End If
'Cleanup.
Set rngFound = Nothing
Set wsDepo = Nothing
End If
End Sub