【问题标题】:Find a value within a column, then change a value elsewhere on the same row在列中查找一个值,然后在同一行的其他位置更改一个值
【发布时间】:2018-11-09 17:30:27
【问题描述】:

我正在使用 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

【问题讨论】:

  • 那里似乎没有具体的问题。 “返工”究竟涉及什么?
  • @SJR 返工涉及拒绝比最初更多的项目,该项目正在返工,因为它在筛选过程中出现了问题。因此,每次对项目进行返工时,某种物质的含量都会降低,所有这些都必须是可追溯的。我想我可以使用该信息创建一个新行,然后将其与原始行合并。但我认为这会有点复杂。
  • 您是否有不能使用实际数据库的原因?尝试强制执行参照完整性比“复杂”要更多
  • @Cominter 我在一家安全政策非常严格的公司工作。由于我是公司的新人,他们希望我使用我所拥有的 Excel。
  • excel文件是在网络上供更多用户使用,还是只在一台计算机上供一个用户使用?

标签: excel vba excel-2010


【解决方案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

【讨论】:

  • 优秀... ;-)
  • @Excelosaurus 很抱歉给你这么多工作。我在 sub 之外声明了变量,因为它们也将在其他 sub 中使用。但感谢您的回答和您的时间。我明天一上班就测试一下。
  • @Excelosaurus 我已经尝试过你的建议,但是“返工”被添加到范围末尾的新行中
  • @Excelosaurus 没关系。您的代码有效。我弄乱了变量的顺序。感谢您的帮助。
猜你喜欢
  • 2020-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-27
  • 2021-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多