表单在提交之前验证数据

时间:2014-07-29 11:15:18

标签: excel vba validation excel-vba

对于VB而言,我是初学者,我在开发表单时遇到了一些麻烦。我想要实现的是单击形式的表单:

验证四个文本框和九个组合框,确保它们在提交到MS Excel工作表之前都具有值 如果有空字段,则应该出现一个消息框,告诉用户“文本框和/或下拉框”必须包含数据“(或某种效果) 假设所有字段都有值,则工作簿必须不受保护 然后必须将数据输入Excel工作表(每个表单提交等于一行数据,后续条目不得覆盖这些数据)。 必须再次保护工作簿 所有操作完成后,必须隐藏表单。

到目前为止,这是我的代码。我确信它非常简单,可以提高效率 - 欢迎任何建议。提前感谢您的帮助。

Private Sub CommandButton1_Click()
Sheet2.Unprotect
Dim LastRow As Object
Set LastRow = Sheet2.Range("a65536").End(xlUp)
LastRow.Offset(1, 0).Value = ExpRecDrop.Text
Set LastRow = Sheet2.Range("b65536").End(xlUp)
LastRow.Offset(1, 0).Value = CPName.Text
Set LastRow = Sheet2.Range("c65536").End(xlUp)
LastRow.Offset(1, 0).Value = ConEntDrop.Text
Set LastRow = Sheet2.Range("d65536").End(xlUp)
LastRow.Offset(1, 0).Value = ResTypDrop.Text
Set LastRow = Sheet2.Range("e65536").End(xlUp)
LastRow.Offset(1, 0).Value = LangDrop.Text
Set LastRow = Sheet2.Range("f65536").End(xlUp)
LastRow.Offset(1, 0).Value = WritDrop.Text
Set LastRow = Sheet2.Range("g65536").End(xlUp)
LastRow.Offset(1, 0).Value = OwnerDrop.Text
Set LastRow = Sheet2.Range("i65536").End(xlUp)
LastRow.Offset(1, 0).Value = BiRiDrop.Text
Set LastRow = Sheet2.Range("j65536").End(xlUp)
LastRow.Offset(1, 0).Value = ERTextBox.Text
Set LastRow = Sheet2.Range("k65536").End(xlUp)
LastRow.Offset(1, 0).Value = DueDatDrop.Text
Set LastRow = Sheet2.Range("l65536").End(xlUp)
LastRow.Offset(1, 0).Value = SubTypDrop.Text
Set LastRow = Sheet2.Range("o65536").End(xlUp)
LastRow.Offset(1, 0).Value = CommText.Text
Sheet2.Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
Me.Hide
MsgBox "One record written to the 2014 tab"
End Sub

这是我的第一篇文章,如果您需要更多信息,请告诉我。

1 个答案:

答案 0 :(得分:0)

你可以试试这个。我创建了一个布尔(True / False)变量来确定是否应该进行更新,如果您的任何控件为空,它将更改为false并跳过更新。我所做的另一个改变是,不是重复计算下一个可用的单元格,而是计算出下一个自由行号,并将其用作对行进行填充的参考。

Private Sub CommandButton1_Click()

Dim LastRow As Long
Dim isOk As Boolean

sheet2.Unprotect

' Create boolean variable as true and check controls for data, if any data missing, change to false
isOk = True
If ExpRecDrop.Text = "" Then isOk = False
If CPName.Text = "" Then isOk = False
If ConEntDrop.Text = "" Then isOk = False
If ResTypDrop.Text = "" Then isOk = False
If LangDrop.Text = "" Then isOk = False
If WritDrop.Text = "" Then isOk = False
If OwnerDrop.Text = "" Then isOk = False
If BiRiDrop.Text = "" Then isOk = False
If ERTextBox.Text = "" Then isOk = False
If DueDatDrop.Text = "" Then isOk = False
If SubTypDrop.Text = "" Then isOk = False
If CommText.Text = "" Then isOk = False

' if boolean value is still true, update the sheet, else display error message
If Not isOk Then
    With sheet2
        ' get number of the next free row
        Set LastRow = .Range("a65536").End(xlUp).Row + 1
        ' Populate the sheet
        .Cells(LastRow, 1).Value = ExpRecDrop.Text
        .Cells(LastRow, 2).Value = CPName.Text
        .Cells(LastRow, 3).Value = ConEntDrop.Text
        .Cells(LastRow, 4).Value = ResTypDrop.Text
        .Cells(LastRow, 5).Value = LangDrop.Text
        .Cells(LastRow, 6).Value = WritDrop.Text
        .Cells(LastRow, 7).Value = OwnerDrop.Text
        .Cells(LastRow, 8).Value = BiRiDrop.Text
        .Cells(LastRow, 9).Value = ERTextBox.Text
        .Cells(LastRow, 10).Value = DueDatDrop.Text
        .Cells(LastRow, 11).Value = SubTypDrop.Text
        .Cells(LastRow, 12).Value = CommText.Text
        .Protect DrawingObjects:=True, Contents:=True, Scenarios:=True
    End With
    ' Hide userform and confirm
    Me.Hide
    MsgBox "One record written to the 2014 tab"
Else
    ' Error message
    MsgBox "Some data missing"
End If

End Sub