Hello friends, I'm new to the forum but always try to get references for code here.
I'm currently trying to develop a VBA code to loop through a dataset, comparing the values from some rows, and if I find that all of them are equal, I have to sum values from some last rows to the first match, and delete the duplicate.
I have done it, but it's extremely slow, and considering the size of the dataset, it could that up to 7 hours to loop. I have seem some examples using COUNTIF or some kind of Find value. What would you guys recommend?
I'm posting my sample code below:
I'm currently trying to develop a VBA code to loop through a dataset, comparing the values from some rows, and if I find that all of them are equal, I have to sum values from some last rows to the first match, and delete the duplicate.
I have done it, but it's extremely slow, and considering the size of the dataset, it could that up to 7 hours to loop. I have seem some examples using COUNTIF or some kind of Find value. What would you guys recommend?
I'm posting my sample code below:
VBA Code:
Dim n As Long, j As Long, k As Long, count As Long, LastRow As Long, x As Long, y As Long
Dim Arryk(), Arrnum() As Variant
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Arryk = Array(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32)
Arrnum = Array(33, 34, 35, 37)
For x = 2 To LastRow
LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
For y = LastRow To x Step -1
If (Not x = y) And (Not IsEmpty(Cells(y, Arryk(0)))) And (Not IsEmpty(Cells(x, Arryk(0)))) Then
count = 0
For k = 0 To 27
If Cells(x, Arryk(k)) = Cells(y, Arryk(k)) Then
count = count + 1
End If
Next
If count = 28 Then
If IsEmpty(Cells(x, 2)) Then
Cells(x, 2).Value = Cells(x, 1).Value & ", " & Cells(y, 1).Value
Else
Cells(x, 2).Value = Cells(x, 2).Value & ", " & Cells(y, 1).Value
End If
For j = 0 To 3
Cells(x, Arrnum(j)).Value = Round(WorksheetFunction.Sum(Cells(x, Arrnum(j)).Value, Cells(y, Arrnum(j)).Value), 2)
Next
Rows(y).Delete Shift:=xlUp
End If
End If
Next y
Next x