寰俊瀹㈡湇
锛憋急瀹㈡湇
鏀粯瀹濆鏈
鐢佃瘽瀹㈡湇
鍒嗕韩

PMC资源网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3217|回复: 0

vba判断excel中数据是否重复

[复制链接]

6168

主题

540

回帖

2万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
22837
QQ
发表于 2013-1-31 15:15:05 | 显示全部楼层 |阅读模式
Private Sub CommandButton1_Click()
    Dim c(256), sum(256), a(256), b(256)    As Variant    'c(256),sum(256) 获取单元格的值,a(256),b(256) 设置检查的列和列的字符长度
    Dim type1(100)                          As Variant    '保存每一列的类型
    Dim area(100, 100)                      As Variant    '保存数值类型的范围
    Dim i, j, m, s, y, count1, count, k, strlen As Long   'i,j用作循环,m,n 用作控制循环,count 记录有几列要检查,count1 有多少行数据
    Dim str, strMsg, str1   As String                   'str 获取列的字符串,str1 列的长度 strMsg 弹出信息
    Dim startValue, endValue As Integer                 'startValue 设置范围的默认起始值,endValue 默认终止值
    Dim line        As Integer                          '设置从第几行数据开始检查
    Dim str2        As String                           '设置每列的类型 str 字符型,num 数据型,date 日期型
    Dim flag        As Boolean                          '是否有重复值的条件,flag=true and s=1 时有重复值
    str = "2,3,4"                                         '设置选择的列数
    'str1 设置列的最大长度或数值的范围 范围用 num-num 格式 例如 1-2 如果为空则采用设定的默认值
    str1 = ""
    '设置列的数据类型 num 数值型,str 字符型,date 日期型,空为字符型
    str2 = "date,str,num"
    strlen = 10                                         '设置字符的默认长度
    startValue = 1                                      '设置数值范围的默认起始值
    endValue = 100                                     '设置数值范围的默认终止值
    m = InStr(1, str, ",")
    n = InStr(1, str1, ",")
    l = InStr(1, str2, ",")
    i = 1
    line = 1                                            '设置从第一行数据开始检查
   
    Do While 1 = 1
        If Cells(i, 1) = "" And Cells(i, 2) = "" And Cells(i + 1, 1) = "" And Cells(i + 1, 2) = "" Then
             Exit Do
        Else
             count1 = count1 + 1                        '计算有多少行数据
        End If
        i = i + 1
    Loop
    i = 0
    While m > 0
     a(i) = Left(str, m - 1) + 0                        '获取所设置的列
     If l = 0 Then
         type1(i) = "str"
     Else
         type1(i) = Left(str2, l - 1)
     End If
     If UCase(type1(i)) = "STR" Then                           '如果是字符型
        If n = 0 Then
          b(i) = Left(str1, n)                          '获取所设置的列的长度
        Else
          b(i) = Left(str1, n - 1)
        End If
        If b(i) = "" Then
          b(i) = strlen
        End If
     Else
        If UCase(type1(i)) = "NUM" Then                        '如果是数值型
           strsub = Left(str1, n - 1)
           l1 = InStr(1, strsub, "-")
           If l1 = 0 Or l1 = 1 Then
             area(i, 0) = startValue
             If Not VBA.IsNumeric(Right(strsub, Len(strsub) - l1)) Then
               area(i, 1) = endValue
             Else
               area(i, 1) = Right(strsub, Len(strsub) - l1)
             End If
           Else
             area(i, 0) = Left(strsub, l1 - 1)            '该列的范围
             If Not VBA.IsNumeric(Right(strsub, Len(strsub) - l1)) Then
               area(i, 1) = endValue
             Else
               area(i, 1) = Right(strsub, Len(strsub) - l1)
             End If
           End If
        End If
     End If
     str = Right(str, Len(str) - m)
     str1 = Right(str1, Len(str1) - n)
     str2 = Right(str2, Len(str2) - l)
     m = InStr(1, str, ",")
     n = InStr(1, str1, ",")
     l = InStr(1, str2, ",")
     i = i + 1
     count = count + 1                                 '得到要检查的列数
    Wend
    a(i) = str + 0                                     ' str+0 转换成数值型
    type1(i) = str2
    If UCase(type1(i)) = "STR" Then
      b(i) = str1
      If b(i) = "" Then
        b(i) = strlen
      End If
    Else
      If UCase(type1(i)) = "NUM" Then
         l1 = InStr(1, str1, "-")
         If l1 = 0 Or l1 = 1 Then
             area(i, 0) = startValue
             If Not VBA.IsNumeric(Right(str1, Len(str1) - l1)) Then
               area(i, 1) = endValue
             Else
               area(i, 1) = Right(str1, Len(str1) - l1)
             End If
         Else
             area(i, 0) = Left(str1, l1 - 1)            '该列的范围
             If Not VBA.IsNumeric(Right(str1, Len(str1) - l1)) Then
               area(i, 1) = endValue
             Else
               area(i, 1) = Right(str1, Len(str1) - l1)
             End If
         End If
      End If
    End If
    count = count + 1                        ' 判断共有几列数据要检查
    flag = True                              ' 设定相同值条件的初始值
    s = 0                                    ' s=0 是否有相同值的条件 0为没有相同的值
    m = 0                                    ' m=0 外循环条件
    n = 0                                    ' n=0 内循环条件
    i = 1                                    ' 设定从第一行开始判断
    Do While line <= count1 And m = 0
         For y = 0 To count - 1              ' 循环获取第y行 count 列的值
             c(y) = Cells(line, a(y))           ' 第i行 a(y) 列的值
             If c(y) = "" Then
                Cells(line, a(y)).Select
                strMsg = "no"
                MsgBox "该单元格的值不能为空值"
                Exit Do
             End If
             If UCase(type1(y)) = "STR" Then
                If Len(c(y)) > b(y) + 0 Then
                  Cells(line, a(y)).Select
                  strMsg = "no"
                  MsgBox "该单元格值的长度不能大于" + CStr(b(y))
                  Exit Do
                End If
             Else
                If UCase(type1(y)) = "NUM" Then
                  If VBA.IsNumeric(c(y)) Then
                     If c(y) < area(y, 0) + 0 Or c(y) > area(y, 1) + 0 Then
                       Cells(line, a(y)).Select
                       strMsg = "no"
                       MsgBox "该单元格的值不能小于 " + CStr(area(y, 0)) + " 或大于 " + CStr(area(y, 1))
                       Exit Do
                     End If
                  Else
                     Cells(line, a(y)).Select
                     strMsg = "no"
                     MsgBox "该单元格值的类型不正确,应该为数值型"
                     Exit Do
                  End If
                Else
                  If UCase(type1(y)) = "DATE" Then
                     If Not VBA.IsDate(c(y)) Then
                        Cells(line, a(y)).Select
                        strMsg = "no"
                        MsgBox "该单元格日期格式不正确!格式为 年/月/日"
                        Exit Do
                     End If
                  End If
                End If
             End If
          Next y
          y = 0
          j = line + 1
          Do While j <= count1
             For y = 0 To count - 1         ' 循环获取 count 列的值
                s = 1
                sum(y) = Cells(j, a(y))     ' 第J行 a(y) 列的值
                If sum(y) = "" Then         ' 如果该列值为空退出循环
                   m = 1                    ' m=1退出外循环
                   Cells(j, a(y)).Select
                   strMsg = "no"
                   MsgBox "该单元格的值不能为空值"
                   Exit Do
                End If
                If UCase(type1(y)) = "STR" Then
                   If Len(sum(y)) > b(y) + 0 Then
                     m = 1                        ' m=1退出外循环
                     Cells(j, a(y)).Select
                     strMsg = "no"
                     MsgBox "该单元格的值长度不能大于" + CStr(b(y))
                     Exit Do
                   End If
                Else
                   If UCase(type1(y)) = "NUM" Then
                     If VBA.IsNumeric(sum(y)) Then
                       If sum(y) < area(y, 0) + 0 Or sum(y) > area(y, 1) + 0 Then
                         m = 1
                         Cells(j, a(y)).Select
                         strMsg = "no"
                         MsgBox "该单元格的值不能小于 " + CStr(area(y, 0)) + " 或大于 " + CStr(area(y, 1))
游客,本贴VIP会员可浏览,点击这里“自动升级VIP会员”,可快速永久浏览本站所有内容!
会赚钱的人用钱买时间,不会赚钱的人用时间换钱;
主动学习,提升自己,战胜对手,梦想才能变为现实!

版权声明:

1、在本站内发表的内容仅代表作者本人观点,与本网站立场无关。

2、转载或引用本网站中的署名文章,请按规定向原作者获得授权。

3、对于不当转载或引用本网站内容而引起的民事纷争、行政处理或其他损失,本网站不承担责任。

4、对不遵守本声明或其他违法、恶意使用本网站内容者,本网站保留追究其法律责任的权利。

5、免责声明:PMC资源网所发布的一切文章和资料仅限用于学习和研究目的,不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负。本站信息来自网络,版权争议与本站无关。您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。如果您喜欢或需要该内容,请联系作者,购买授权,得到更好的服务。如有侵权请邮件与我们联系处理(邮件地址:admin@pmczy.com)。

关闭

站长推荐上一条 /2 下一条

工厂管理秘籍


QQ|手机版|PMC资源网 ( 本站部分资源来自网络,仅供个人学习,如侵犯了您的权益,请联系我们删除(admin@pmczy.com) 。蜀ICP备14016815号-2 )

GMT+8, 2024-5-4 00:52 , Processed in 0.069239 second(s), 28 queries .

Powered by Discuz! X3.4

© 2007-2024 PMCZY.COM

快速回复 返回顶部 返回列表