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

生产数据ListBox编辑后新增数据异常问题求助

问题根源

编辑操作完成后,txtRowNumberProd文本框的值未被清空,导致后续执行Prod_Submit时,代码会进入Else分支,持续使用最后一次编辑的行号,而非计算新的空白行。

解决方案

方案1:在提交完成后清空行号文本框

修改Prod_Submit代码,在写入数据后添加清空txtRowNumberProd的语句,确保下次新增时触发新行计算:

Sub Prod_Submit()
    Dim sh As Worksheet
    Dim iRow As Long
    
    Set sh = ThisWorkbook.Sheets("ADHData")
    
    If MainForm.txtRowNumberProd.Value = "" Then
        iRow = [Counta(ADHData!A:A)] + 1
    Else
        iRow = MainForm.txtRowNumberProd.Value
    End If
    
    With sh
        .Cells(iRow, 1) = MainForm.OrderA.Value
        .Cells(iRow, 2) = MainForm.StockA.Value
        .Cells(iRow, 3) = MainForm.FaceA.Value
        .Cells(iRow, 4) = MainForm.LinerA.Value
        .Cells(iRow, 5) = MainForm.WidthA.Value
        .Cells(iRow, 6) = MainForm.PrevContA.Value
        .Cells(iRow, 7) = MainForm.ContA.Value
        .Cells(iRow, 8) = MainForm.PrevGoodA.Value
        .Cells(iRow, 9) = MainForm.GoodA.Value
    End With
    
    ' 新增:清空行号文本框
    MainForm.txtRowNumberProd.Value = ""
End Sub

方案2:完善重置过程,加入行号文本框清空

在Prod_Reset的文本框重置部分,添加清空txtRowNumberProd的语句,确保重置操作覆盖所有编辑相关控件:

Sub Prod_Reset()
    Dim iRow As Long
    
    iRow = [Counta(ADHData!A:A)] + 1 ' idetifying the last row
    
    With MainForm
        ' 新增:清空行号文本框
        .txtRowNumberProd.Value = ""
        
        .OrderA.Value = ""
        .StockA.Value = ""
        .FaceA.Value = ""
        .LinerA.Value = ""
        .WidthA.Value = ""
        .PrevContA.Value = ""
        .ContA.Value = ""
        .PrevGoodA.Value = ""
        .GoodA.Value = ""
        
        .Production_TableA.ColumnCount = 9
        .Production_TableA.ColumnHeads = True
        
        .Production_TableA.ColumnWidths = "55,55,70,71,50,106,77,69,42"
        
        If iRow > 1 Then
            .Production_TableA.RowSource = "ADHData!A2:J" & iRow
        Else
            .Production_TableA.RowSource = "ADHData!A2:J21"
        End If
    End With
End Sub

额外优化:修正行号计算逻辑(可选)

当前Select_Prod函数和编辑按钮的行号赋值存在重复加1的问题,可调整为直接返回工作表实际行号:

  • 修改Select_Prod函数:
Function Select_Prod() As Long
    Dim i As Long
    Select_Prod = 0
    For i = 0 To MainForm.Production_TableA.ListCount - 1
        If MainForm.Production_TableA.Selected(i) = True Then
            Select_Prod = i + 2 ' 直接对应工作表A2开始的行号
            Exit For
        End If
    Next i
End Function
  • 编辑按钮中去掉额外的+1:
MainForm.txtRowNumberProd.Value = Select_Prod

内容的提问来源于stack exchange,提问作者Brian Bowles

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 20:21:10