如果这是重复的,我深表歉意,因为我一直在搜索但没有找到答案。我对 VBA 以及它们如何构造循环很陌生。我正在尝试进行搜索和比较。我需要比较第一行中的值,看看它们是否与第二行匹配,如果不匹配,则继续移动到下一行。请参阅下面的我的代码(它运行时没有错误,只是找不到任何存在的值,因为我可以手动搜索并找到它们)
这个数据集可能非常大,所以我想尽可能高效地编写它,并且不确定哪种循环结构会执行得更快。我需要比较第 21 列中的值,看看是否有重复的值,如果有,那么我需要查看相应行的第 22 列中的值是否相同,如果相同,那么我想去RowB 中的下一行,否则如果它们不是相同的值,那么我想检查第 4 行中都是日期的值,看看它们是否在 2 个月内。如果他们没有继续寻找。
Dim RowsCount As Integer
Dim ColCount As Integer
RowsCount = Cells(Rows.Count, 1).End(xlUp).Row
ColCount = Cells(1, Columns.Count).End(xlToLeft).Column
Dim RowA As Integer
Dim RowB As Integer
Dim GroupA As Variant
Dim GroupB As Variant
Dim CounterA As Variant
Dim CounterB As Variant
Dim RevDateA As Date
Dim RevDateB As Date
Dim RevDateDiff As Variant
RowA = 2
RowB = 3
Do While RowA <= RowsCount
GroupA = Cells(RowA, 21).Value
CounterA = Cells(RowA, 22).Value
RevDateA = Cells(RowA, 4).Value
Do While RowB <= RowsCount
GroupB = Cells(RowB, 21).Value
CounterB = Cells(RowB, 22).Value
RevDateB = Cells(RowB, 4).Value
If GroupA = GroupB Then
If CounterA = CounterB Then 'go down 1 row in B and repeat
Else
If RevDateB - RevDateA < 62 Then
'highlight row b and move on
Rows(RowB).Select
Application.CommandBars.ExecuteMso "CellFillColorPicker"
Else
End If
End If
Else 'go down 1 row in B and repeat check
End If
RowB = RowB + 1
Loop
RowA = RowA + 1
Loop
这是查找行对行重复项的好方法
Private Sub findit()
Dim bringIn As Variant
bringIn = ThisWorkbook.Sheets("Sheet1").UsedRange
rowC = ThisWorkbook.Sheets("Sheet1").UsedRange.Rows.Count
For i = LBound(bringIn, 1) To UBound(bringIn, 1)
If i = rowC Then
'nothing
Else
If bringIn(i, 1) = bringIn(i + 1, 1) Then
ThisWorkbook.Sheets("Sheet1").Cells(i, 1).Interior.ColorIndex = 37
End If
End If
Next i
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)