Excel data checking

倾然丶 夕夏残阳落幕 提交于 2020-01-06 15:59:46

问题


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.

回答1:


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


来源:https://stackoverflow.com/questions/41959118/excel-data-checking

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!