vba 判断excel中数据是否重复
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
vba计算字符串长度 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 ' 设定相同值条件的初始值
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论