You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA污染检查器代码修正:逻辑错误引发误提示问题

VBA污染检查器代码修正

问题分析

原代码的If-ElseIf分支逻辑混乱,没有正确组合触发"Contamination Check NG"的三个必要前提:电容相同、材料相同、批号不同。错误的逻辑导致只要批号与上一批次不同,就会触发提示,完全忽略了电容和材料必须匹配的条件。

修正后的代码

Sub Txtlot_AfterUpdate()
    Dim lot As Worksheet
    Set lot = ThisWorkbook.Sheets("Sheet1")

    Dim n As Long
    n = lot.Range("A" & Application.Rows.Count).End(xlUp).Row

    ' 提取上一批次的关键参数
    Dim lastCap As String, lastLotCode As String, lastMat As String
    lastCap = lot.Range("A" & n).Value
    lastLotCode = lot.Range("B" & n).Value
    lastMat = lot.Range("C" & n).Value

    ' 严格匹配触发NG的条件:电容相同+材料相同+批号不同
    If Txtcap.Value = lastCap And Txtmat.Value = lastMat And Txtcode.Value <> lastLotCode Then
        MsgBox "Contamination Check NG", vbCritical
        Exit Sub
    End If

    ' 检查通过,执行后续流程
    UserForm2.Show
End Sub

核心修正说明

  • 修复了原代码中Application.Row.count的拼写错误(应为Application.Rows.Count)
  • 重构条件判断逻辑,使用And运算符同时绑定三个必要条件,彻底避免误触发
  • 将上一批次的参数提取到变量中,提升代码的可读性和可维护性
  • 简化流程控制:仅在触发污染检查不通过时终止代码,其余情况正常执行后续的表单显示逻辑

内容的提问来源于stack exchange,提问作者Jonathan Caballegan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 12:28:12