Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 1895|回复: 3

[已解决]怎么实现同行快速对比?

[复制链接]
发表于 2017-11-8 18:24 | 显示全部楼层 |阅读模式
怎么实现同行快速对比?


Data, 是原始资料
Compare,是模拟结果


代码需求: 是对比每一, 行两列(ABCD四列), 6个组合, 判断内容是否相同?
如果是相同=Y,不相同=N,  (如有空白,不处理).


因为数据N, 10W, 请老师, 大神帮帮忙, 谢谢…!

最佳答案
2017-11-9 08:18
简化一下
Private Sub CommandButton1_Click()
'A与B、C、D比对
For i1 = 1 To Range("A65536").End(3).Row
   For j = 2 To 4
    If Cells(i1, 1) = Cells(i1, j) Then
     Cells(i1, 5 + j) = "Y"
    End If
    If Cells(i1, 1) <> Cells(i1, j) Then
     Cells(i1, 5 + j) = "N"
    End If
    If Cells(i1, 1) = "" Or Cells(i1, j) = "" Then
    Cells(i1, 5 + j) = ""
    End If
  Next
Next
  'B与C、D比对
For i1 = 1 To Range("A65536").End(3).Row
   For j = 3 To 4
      If Cells(i1, 2) = Cells(i1, j) Then
        Cells(i1, 7 + j) = "Y"
      End If
      If Cells(i1, 2) <> Cells(i1, j) Then
        Cells(i1, 7 + j) = "N"
      End If
      If Cells(i1, 2) = "" Or Cells(i1, j) = "" Then
        Cells(i1, 7 + j) = ""
      End If
    Next
Next

'C与D比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 3) = Cells(i1, 4) Then
       Cells(i1, 12) = "Y"
     End If
     If Cells(i1, 3) <> Cells(i1, 4) Then
       Cells(i1, 12) = "N"
     End If
     If Cells(i1, 3) = "" Or Cells(i1, 4) = "" Then
       Cells(i1, 12) = ""
     End If
Next
End Sub
Compare.jpg

Compare.zip

6.22 KB, 下载次数: 6

发表于 2017-11-8 20:19 | 显示全部楼层
Private Sub CommandButton1_Click()
'A与B比对
For i1 = 1 To Range("A65536").End(3).Row
    If Cells(i1, 1) = Cells(i1, 2) Then
     Cells(i1, 7) = "Y"
    End If
    If Cells(i1, 1) <> Cells(i1, 2) Then
     Cells(i1, 7) = "N"
    End If
      
    If Cells(i1, 1) = "" Or Cells(i1, 2) = "" Then
    Cells(i1, 7) = ""
    End If
Next

'A与C比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 1) = Cells(i1, 3) Then
     Cells(i1, 8) = "Y"
    End If
    If Cells(i1, 1) <> Cells(i1, 3) Then
     Cells(i1, 8) = "N"
    End If
      
    If Cells(i1, 1) = "" Or Cells(i1, 3) = "" Then
    Cells(i1, 8) = ""
    End If
Next

  'A与D比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 1) = Cells(i1, 4) Then
      Cells(i1, 9) = "Y"
     End If
   
     If Cells(i1, 1) <> Cells(i1, 4) Then
      Cells(i1, 9) = "N"
     End If
      
     If Cells(i1, 1) = "" Or Cells(i1, 4) = "" Then
      Cells(i1, 9) = ""
     End If
Next

  'B与C比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 2) = Cells(i1, 3) Then
      Cells(i1, 10) = "Y"
     End If
   
     If Cells(i1, 2) <> Cells(i1, 3) Then
      Cells(i1, 10) = "N"
     End If
      
     If Cells(i1, 2) = "" Or Cells(i1, 3) = "" Then
      Cells(i1, 10) = ""
    End If
Next

  'B与D比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 2) = Cells(i1, 4) Then
      Cells(i1, 11) = "Y"
     End If
   
     If Cells(i1, 2) <> Cells(i1, 4) Then
      Cells(i1, 11) = "N"
     End If
      
     If Cells(i1, 2) = "" Or Cells(i1, 4) = "" Then
      Cells(i1, 11) = ""
    End If
Next

'C与D比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 3) = Cells(i1, 4) Then
      Cells(i1, 12) = "Y"
     End If
   
     If Cells(i1, 3) <> Cells(i1, 4) Then
      Cells(i1, 12) = "N"
     End If
      
     If Cells(i1, 3) = "" Or Cells(i1, 4) = "" Then
      Cells(i1, 12) = ""
    End If
Next
End Sub
回复

使用道具 举报

发表于 2017-11-9 08:18 | 显示全部楼层    本楼为最佳答案   
简化一下
Private Sub CommandButton1_Click()
'A与B、C、D比对
For i1 = 1 To Range("A65536").End(3).Row
   For j = 2 To 4
    If Cells(i1, 1) = Cells(i1, j) Then
     Cells(i1, 5 + j) = "Y"
    End If
    If Cells(i1, 1) <> Cells(i1, j) Then
     Cells(i1, 5 + j) = "N"
    End If
    If Cells(i1, 1) = "" Or Cells(i1, j) = "" Then
    Cells(i1, 5 + j) = ""
    End If
  Next
Next
  'B与C、D比对
For i1 = 1 To Range("A65536").End(3).Row
   For j = 3 To 4
      If Cells(i1, 2) = Cells(i1, j) Then
        Cells(i1, 7 + j) = "Y"
      End If
      If Cells(i1, 2) <> Cells(i1, j) Then
        Cells(i1, 7 + j) = "N"
      End If
      If Cells(i1, 2) = "" Or Cells(i1, j) = "" Then
        Cells(i1, 7 + j) = ""
      End If
    Next
Next

'C与D比对
For i1 = 1 To Range("A65536").End(3).Row
     If Cells(i1, 3) = Cells(i1, 4) Then
       Cells(i1, 12) = "Y"
     End If
     If Cells(i1, 3) <> Cells(i1, 4) Then
       Cells(i1, 12) = "N"
     End If
     If Cells(i1, 3) = "" Or Cells(i1, 4) = "" Then
       Cells(i1, 12) = ""
     End If
Next
End Sub
回复

使用道具 举报

 楼主| 发表于 2017-11-9 10:26 | 显示全部楼层
yanhj 发表于 2017-11-9 08:18
简化一下
Private Sub CommandButton1_Click()
'A与B、C、D比对

谢谢老师

原来是这样, 简单就是美, 我一开始的思路好乱

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-25 08:01 , Processed in 0.364326 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

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