我正在开发一个带有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
抛开对解决方案体系结构的任何批评,你的代码缺少的是对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