【问题标题】:Form validates data before submitting表单在提交前验证数据
【发布时间】:2014-07-29 11:15:18
【问题描述】:

我是 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

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

【问题讨论】:

  • 欢迎来到 Stack Overflow!你已经为你的项目写了一个很好的描述,并包含了你的程序的 sn-p,做得很好。现在,由于这是一个问答网站,我会要求您编辑您的帖子并添加我们可以回答的实际 问题问题。请参阅this 作为参考。

标签: vba validation excel


【解决方案1】:

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

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

【讨论】:

  • 谢谢你——正是我需要的:)。我没有完全工作,但我设法弄清楚并进行了一些更改(即从“If Not isOK Then”行中删除了“Not”,并从“Set LastRow =”行中删除了“Set”。
  • 当然,这会教我检查我在说什么!如您所知,我发布了这个未经测试的内容,但至少您能够发现明显的错误!
猜你喜欢
  • 2011-10-22
  • 1970-01-01
  • 2016-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-30
相关资源
最近更新 更多