在输入新的数据行之前,确保行完全完成

时间:2014-04-29 07:43:07

标签: excel-vba vba excel

我希望限制用户将数据添加到表中的新行,除非在上一行中输入了所有数据,有人可以帮忙吗?我目前有条件格式,突出显示任何缺少数据的行,但我想超越它。

1 个答案:

答案 0 :(得分:0)

我看不到阻止在新行上输入数据的简单方法。

但是,您可以使用Worksheet_Change事件 test 进行完整输入,如果之前的行不完整,则撤消新数据条目。

像这样的东西

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim lo As ListObject
    Dim lr As ListRow

    ' Get reference to the first or only Table in the worksheet
    Set lo = Me.ListObjects(1)

    ' If the change was not in the Table, there is nothing to do
    If Application.Intersect(Target, lo.DataBodyRange) Is Nothing Then Exit Sub

    ' If there is only one row in the Table, there is nothing to do
    If lo.ListRows.Count <= 1 Then Exit Sub

    ' Count the number of entries in the second last row
    '  if its less than the number of columns, that row is incomplete
    If Application.CountA(lo.ListRows(lo.ListRows.Count - 1).Range) _
      <> lo.ListColumns.Count Then
        ' Delete the last (newly created) row
        lo.ListRows(lo.ListRows.Count).Delete
        lo.ListRows(lo.ListRows.Count).Range.SpecialCells(xlCellTypeBlanks).Select
        MsgBox "Please enter complete data before starting a new row!", _
          vbOKOnly + vbError, "Undoing last data entry"
    End If
End Sub