如果所选行在第一列中缺少数据,则数据输入表单将失败

Data Entry Form Fails if selected row has missing data in the first column

我收到一个 运行1004 时间错误,当我 select 我的列表框中的一个项目时,无法获得工作表函数 class 的匹配项 属性使用编辑按钮进行编辑。

调试突出显示了问题子项。我意识到,如果第一列中有数据,它将正常工作,如果没有数据,则会失败。

我对突出显示的行加了星号。我知道它与第一列中的内容不匹配。如果行期间有数据,有没有办法让运行?有时第一列一开始没有数据输入。

或者我是否应该创建一个带有自动递增数字的隐藏列,以便第一列始终有数据?

有关此操作的更多详细信息。我正在为文本框分配一个值,以便它可用于识别我正在积极处理的行。它通过将第一列 A:A 分配给 txtRowNumber 来实现。如果列表框中没有 selected 项目,宏将 select 下一个空行,如下所示子例程 submit().

我的目标是能够 select 该行,而不管第一列中是否有数据。我希望能够继续编辑和保存任何行 selected

谢谢!

Private Sub EditButton_Click()

    If selected_list = 0 Then
        MsgBox "No Row has been selected", vbkonly + vbInformation, "Edit"
        
        Exit Sub
            
    End If
    
    'code to update the value to respective controls
    
    *Me.txtRowNumber.Value = Application.WorksheetFunction.Match(Me.LstDataBase.List(Me.LstDataBase.ListIndex, 0), ThisWorkbook.Sheets("Active").Range("A:A"), 0)*
    
    Me.SctaskInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 0)
    Me.TechInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 1)
    Me.CustomerInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 2)
    Me.SectionInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 3)
    
    Me.OldSNInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 4)
    Me.OldBTInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 5)
    Me.OldModelInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 6)
    
    Me.NewSNInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 7)
    Me.NewBTInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 8)
    Me.NewModelInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 9)
    Me.StatusInput.Value = Me.LstDataBase.List(Me.LstDataBase.ListIndex, 10)

End Sub
Sub Submit()
    Dim sh As Worksheet
    Dim iRow As Long
    
    Set sh = ThisWorkbook.Sheets("Active")
    
    If frmform.txtRowNumber.Value = "" Then
        iRow = [counta(Active!A:A)] + 1
    Else
        iRow = frmform.txtRowNumber.Value
    End If
    
    With sh
    
    .Cells(iRow, 1) = frmform.SctaskInput.Value
    .Cells(iRow, 2) = frmform.TechInput.Value
    .Cells(iRow, 3) = frmform.CustomerInput.Value
    .Cells(iRow, 4) = frmform.SectionInput.Value
    .Cells(iRow, 5) = frmform.OldSNInput.Value
    .Cells(iRow, 6) = frmform.OldBTInput.Value
    .Cells(iRow, 7) = frmform.OldModelInput.Value
    .Cells(iRow, 8) = frmform.NewSNInput.Value
    .Cells(iRow, 9) = frmform.NewBTInput.Value
    .Cells(iRow, 10) = frmform.NewModelInput.Value
    .Cells(iRow, 11) = frmform.StatusInput.Value
    .Cells(iRow, 14) = IIf(frmform.YesOpt.Value = True, "Yes", "No")
    .Cells(iRow, 15) = [text(Now(), "YYYY/MM/DD HH:MM:SS")]
    .Cells(iRow, 16) = Application.UserName
    
    End With
End Sub
如果未找到搜索值,

Application.WorksheetFunction.Match 将 return 运行时错误。

通常的处理方式是

Dim idx As Long
On Error Resume Next
idx = Application.WorksheetFunction.Match(Me.LstDataBase.List(Me.LstDataBase.ListIndex), ThisWorkbook.Sheets("Active").Range("A:A"), 0)
On Error GoTo 0
If idx = 0 Then
    ' value not found, now what?
Else
    Me.txtRowNumber.Value = idx
End If