Excel VBA固定行进行比较

时间:2018-07-04 16:18:19

标签: excel-vba vba excel

由于这个站点,我为自己的需要修改了代码,该宏使我可以将X行的特定单元格与Y行的另一个单元格进行比较,并且所有单元格之间不仅有1个或2个单元格,而且所有该行的单元格,如果找不到匹配项,则给出 1 0

  • 宏正在将X行与Y进行比较的问题,但我没有将X与Y + 1或Y + 2进行比较,而是直接传递给X + 1并将其与Y + 1进行比较

为更好地了解我的问题,请参见下文:

在此图像中,黄色的两行相同,并且在AO列中,第一行的前面是1,第二行的前面是1 enter image description here

那么,如何将每一行与其他行(X与Y,Y + 1,Y + 2)进行比较,然后传递到下一行,当有匹配项突出显示或在“ AO”列中返回1时

代码:

Sub check()
 Dim check1, check2 As Variant
 Dim l As Long
 Dim ElementsSame As Boolean
   For i = 2 To 74
     check1 = Array(Range("F" & i), Range("G" & i), Range("H" & i), Range("I" 
       & i), Range("J" & i), Range("K" & i), Range("L" & i), Range("M" & i), 
      Range("N" & i), Range("O" & i), Range("P" & i), Range("Q" & i), 
      Range("R" & i), Range("S" & i), Range("T" & i), Range("U" & i), 
      Range("V" & i), Range("W" & i), Range("X" & i), Range("Y" & i), 
      Range("Z" & i), Range("AA" & i), Range("AB" & i), Range("AC" & i), 
      Range("AD" & i), Range("AE" & i), Range("AF" & i), Range("AG" & i), 
       Range("AH" & i), Range("AI" & i), Range("AJ" & i), Range("AL" & i), 
      Range("AM" & i))
For D = 3 To 74
     check2 = Array(Range("F" & D), Range("G" & D), Range("H" & D), Range("I" 
     & D), Range("J" & D), Range("K" & D), Range("L" & D), Range("M" & D), 
     Range("N" & D), Range("O" & D), Range("P" & D), Range("Q" & D), 
     Range("R" & D), Range("S" & D), Range("T" & D), Range("U" & D), 
      Range("V" & D), Range("W" & D), Range("X" & D), Range("Y" & D), 
       Range("Z" & D), Range("AA" & D), Range("AB" & D), Range("AC" & D), 
       Range("AD" & D), Range("AE" & D), Range("AF" & D), Range("AG" & D), 
      Range("AH" & D), Range("AI" & D), Range("AJ" & D), Range("AL" & D), 
       Range("AM" & D))
 ElementsSame = True
 For l = 0 To 32 
   If check1(l) <> check2(l) Then
    ElementsSame = False

    Exit For
 End If
 Next l

If ElementsSame = False Then

  Range("AO" & i) = 1
  Range("AO" & D) = 1 
Else
   Range("AN" & D) = 0 
End If
Next D

Next i
End Sub

任何人都可以向我介绍解决此问题的方法,如果我的问题尚不清楚,请随时提出问题

波兰人

1 个答案:

答案 0 :(得分:0)

您可以尝试一下吗(未经我测试)?

Option Explicit


Sub check()
 Dim check1, check2 As Variant
 Dim i, d As Long

For i = 2 To 74
    check1 = Array(Range("F" & i), Range("G" & i), Range("H" & i), Range("I" _
    & i), Range("J" & i), Range("K" & i), Range("L" & i), Range("M" & i), _
    Range("N" & i), Range("O" & i), Range("P" & i), Range("Q" & i), _
    Range("R" & i), Range("S" & i), Range("T" & i), Range("U" & i), _
    Range("V" & i), Range("W" & i), Range("X" & i), Range("Y" & i), _
    Range("Z" & i), Range("AA" & i), Range("AB" & i), Range("AC" & i), _
    Range("AD" & i), Range("AE" & i), Range("AF" & i), Range("AG" & i), _
    Range("AH" & i), Range("AI" & i), Range("AJ" & i), Range("AL" & i), _
    Range("AM" & i))
        For d = 3 To 74
            check2 = Array(Range("F" & d), Range("G" & d), Range("H" & d), Range("I" _
            & d), Range("J" & d), Range("K" & d), Range("L" & d), Range("M" & d), _
            Range("N" & d), Range("O" & d), Range("P" & d), Range("Q" & d), _
            Range("R" & d), Range("S" & d), Range("T" & d), Range("U" & d), _
            Range("V" & d), Range("W" & d), Range("X" & d), Range("Y" & d), _
            Range("Z" & d), Range("AA" & d), Range("AB" & d), Range("AC" & d), _
            Range("AD" & d), Range("AE" & d), Range("AF" & d), Range("AG" & d), _
            Range("AH" & d), Range("AI" & d), Range("AJ" & d), Range("AL" & d), _
            Range("AM" & d))

            If Join(check1, Chr(0)) = Join(check2, Chr(0)) Then
                Range("AO" & i) = 1
                Range("AO" & d) = 1
            Else
                Range("AN" & d) = 0
            End If
        Next d
Next i

End Sub