|
本帖最后由 WANT-T 于 2023-3-29 19:08 编辑
想要比对出是哪个字段有问题,得转成纵表或者一位表才好处理,或者用数组循环加字典,结合条件格式;用sql直接比较很麻烦,结果表再left join 数据表,把所有的字段都拉出来,一个一个对比,打标,输出到表格里,使用条件格式来标记颜色!工作量不小!
类似这样
代码:
- Sub 查找没有的编码()
- Set Cnn = CreateObject("ADODB.connection")
- Set rst = CreateObject("ADODB.Recordset")
- 'Cnn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source=" & ThisWorkbook.FullName
- Cnn.Open "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & ThisWorkbook.FullName & ";Extended Properties=""Excel 12.0;HDR=YES"";"
- Sql = " select a.学生编号,a.姓名,a.性别,a.籍贯,a.年级 " & _
- "from [录入表$A1:E] a " & _
- "INNER Join [数据$] b " & _
- "on a.学生编号 = b.学生编号 " & _
- "and a.姓名=b.姓名 " & _
- "and a.性别=b.性别 " & _
- "and a.籍贯=b.籍贯 " & _
- "and a.年级=b.年级"
-
- Sql1 = "SELECT a.学生编号,a.姓名,a.性别,a.籍贯,a.年级 FROM [录入表$] A " & _
- "LEFT JOIN (" & Sql & ") B ON A.学生编号=B.学生编号 " & _
- "WHERE B.学生编号 IS NULL"
-
- Sql2 = "SELECT A.*," & _
- "IIF(A.姓名=B.姓名,1,0) AS IS_SAME_1," & _
- "IIF(A.性别=B.性别,1,0) AS IS_SAME_2," & _
- "IIF(A.籍贯=B.籍贯,1,0) AS IS_SAME_3," & _
- "IIF(A.年级=B.年级,1,0) AS IS_SAME_4 " & _
- "FROM (" & Sql1 & ") A LEFT JOIN [数据$] B ON A.学生编号=B.学生编号"
- With Range("H2")
- .CurrentRegion.Offset(1).Clear '清除数据
- .CopyFromRecordset Cnn.Execute(Sql2)
- With .Resize(.CurrentRegion.Rows.Count - 1, .CurrentRegion.Columns.Count - 4)
- .Select
- With .FormatConditions
- .Delete '清楚条件格式
- With .Add(Type:=xlExpression, Formula1:="=RC[4]=0") '添加条件格式
- .Interior.Color = 255
- End With
- End With
- End With
- End With
- Cnn.Close
- Set Cnn = Nothing
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|