【问题标题】:Required field validation on microsoft excelmicrosoft excel 上的必填字段验证
【发布时间】:2012-08-22 02:29:13
【问题描述】:

我在 excel(2003/2007) 中有 5 列(SNO、姓名、电话、地址、密码)。
Excel 可以有“n”号。记录。
我想应用验证,这样,当我们保存 Excel 表时,“SNO”(包含整数值)列的值永远不应为空,当任何一个(名称、电话、地址、引脚)列具有值时。
如果不是这种情况,则应该出现错误消息。
我正在手动输入数据。
我试过了:
编辑:

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If Worksheets("Sheet1").Range("SNO").Value = "" Then
        MsgBox "You must fill in SNO."
        Cancel = True
    End If
End Sub

我们可以在不编写代码的情况下应用验证吗?

【问题讨论】:

  • @brettdj/brettdj我已经编辑了我的问题,我付出了努力。

标签: excel excel-2007 excel-2003 vba


【解决方案1】:

据我所知,VBA 是最好的(唯一的?)方法。

试试

Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim rng As Range
    Dim rw As Range
    Dim dat As Variant

    ' change to your sheet name
    With Worksheets("Sheet4")
        ' Get data range to check
        ' Assumes SNO is column A and Name etc is Column B to E
        Set rng = .Range(.UsedRange.Columns(1), .UsedRange.Columns(5))
        For Each rw In rng.Rows
            ' If SNO cell is empty
            If rw.Cells(1, 1) = "" Then
                ' Check if other cells are not blank
                dat = Join(Application.Transpose(Application.Transpose(rw.Value)))
                If dat <> "" Then
                    MsgBox "You must fill in SNO."
                    Cancel = True
                    Exit For
                End If
            End If
        Next
    End With
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-23
    • 2013-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-14
    相关资源
    最近更新 更多