Search code examples
exceldateuserformworksheetvba

Excel data checking


Excel data checking

I have an Excel UserForm which contains the following fields; date, name and work.

In the event that the worksheet contains the same date and name pairing, I want data transfer from the Userform blocked.

Example

A worksheet has one entry:

  • Column A has the date 1/1/2017
  • Column B has the name john

For the worksheet above, the following rules apply:

  1. Information can be passed from the UserForm for 3/1/2017 and john.
  2. Information can be passed from the UserForm for 1/1/2017 and jane.
  3. Information cannot be passed from the UserForm for 1/1/2017 and john.

Solution

  • Dim k As Long
    Dim matched As Boolean
    matched = False
    
    ' Loop over all used rows
    For k = 1 to ActiveSheet.UsedRange.Rows.Count
    
        ' Check if concatenated string of date & name is unique
        ' e.g. '01/01/17john'
        If ActiveSheet.Cells(k, "A").Text & ActiveSheet.Cells(k, "B").Text = _
           myUserForm.DateField.Text & myUserForm.NameField.Text Then
    
            MsgBox "This Name / Date combination is not unique, pick again"
    
            matched = True
    
            Exit For
    
        End If
    
    Next k
    
    If matched = False Then
    
        ' Name/Date combination is unique, send data to sub or whatever... 
    
    End If