0

I have 5 columns (SNO,Name,Phone,Address,Pin) in excel(2003/2007).
Excel can have "n" no. of records.
I want to apply a validation , such that , when we save the excel sheet,value of "SNO"(which contains integer value) column should never be empty , when any one (Name,Phone,Address,Pin) column has value.
If this is not the case then an error message should come.
I am entering the data manually.
I tried :
EDIT:

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

Can we apply the validation , without writing the code?

1
  • @brettdj/brettdjI have edited my question , with the effort that I put in. Aug 22, 2012 at 3:04

1 Answer 1

0

So far as I can tell VBA is the best (only?) way to do this.

Try

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.