Dim item(), itemrow(), itemcostA(), itemCostB(), itemCostC(), itemdup()
Dim arraycnt
Sub sbFindDuplicatesInColumn()
Dim lastrow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastrow = findlastrow("Sheet1")
arraycnt = 0
For iCntr = 1 To lastrow
arraycnt = arraycnt + 1
MsgBox (arraycnt & " the counter")
ReDim Preserve item(arraycnt)
ReDim Preserve itemrow(arraycnt)
ReDim Preserve itemcostA(arraycnt)
ReDim Preserve itemCostB(arraycnt)
ReDim Preserve itemCostC(arraycnt)
ReDim Preserve itemdup(arraycnt)
itemdup(arraycnt) = 0
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastrow), 0)
If iCntr <> matchFoundIndex Then
'Cells(iCntr, 2) = "Duplicate " & iCntr
getlen = UBound(item) - LBound(item) + 1
' MsgBox (getlen & " is the size of the array")
For t = 1 To getlen - 1
If Cells(arraycnt, 1).Value = item(t) Then
MsgBox ("this item would be added into " & item(t))
itemdup(t) = 1
Cells(arraycnt, 2).Value = "combined"
itemcostA(t) = itemcostA(t) + Cells(arraycnt, 3).Value
itemCostB(t) = itemCostB(t) + Cells(arraycnt, 4).Value
itemCostC(t) = itemCostC(t) + Cells(arraycnt, 5).Value
'MsgBox ("new total is: " & itemcostA(t) & " for array " & item(t))
End If
'itemdup(arraycnt) = 0
Next
Else
'add the item into the array
itemrow(arraycnt) = iCntr
item(arraycnt) = Cells(iCntr, 1).Value
itemcostA(arraycnt) = Cells(iCntr, 3).Value
itemCostB(arraycnt) = Cells(iCntr, 4).Value
itemCostC(arraycnt) = Cells(iCntr, 5).Value
'itemdup(arraycnt) = 0
'MsgBox (item(arraycnt) & " is set for arraycnt " & arraycnt)
End If
End If
Next
ThisWorkbook.Worksheets("Sheet2").Cells(1, 1).Value = "A"
ThisWorkbook.Worksheets("Sheet2").Cells(1, 3).Value = "C"
For Xcntr = 1 To lastrow
'MsgBox (itemdup(Xcntr) & " is set for " & item(Xcntr))
If itemdup(Xcntr) = 1 Then
'Cells(Xcntr, 2).Value = "*"
thisistherow = findlastrow("Sheet2") + 1
MsgBox ("this is the row: " & thisistherow)
MsgBox ("moving it to: " & thisistherow )
ThisWorkbook.Worksheets("Sheet2").Cells(thisistherow, 1).Value = item(Xcntr)
ThisWorkbook.Worksheets("Sheet2").Cells(thisistherow, 3).Value = itemcostA(Xcntr)
End If
Next
End Sub
Function findlastrow(whatsheet)
findlastrow = ThisWorkbook.Worksheets(whatsheet).Cells(Rows.Count, 1).End(xlUp).Row
End Function
Dim item(), itemrow(), itemcostA(), itemCostB(), itemCostC(), itemdup()
Dim arraycnt
Sub FindDuplicates()
Dim lastrow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastrow = findlastrow("Sheet1")
arraycnt = 0
For iCntr = 1 To lastrow
arraycnt = arraycnt + 1
'MsgBox (arraycnt & " the counter")
'This expands the array to hold the same number of data elements as
'there are rows of parts
ReDim Preserve item(arraycnt)
ReDim Preserve itemrow(arraycnt)
ReDim Preserve itemcostA(arraycnt)
ReDim Preserve itemCostB(arraycnt)
ReDim Preserve itemCostC(arraycnt)
ReDim Preserve itemdup(arraycnt)
'here we set up each element with a boolean of whether it
'is duplicated or not. 0=no 1=yes
'all elements start as not duplicated, so we mark them 0
itemdup(arraycnt) = 0
If Cells(iCntr, 1) <> "" Then ' looks to see if Cell A rownumber has a value
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastrow), 0)
If iCntr <> matchFoundIndex Then
getlen = UBound(item) - LBound(item) + 1 'measures the size of the array and inits getlen
' MsgBox (getlen & " is the size of the array")
For t = 1 To getlen - 1
'we loop through the array to check if the element has already
'been loaded into the array
If Cells(arraycnt, 1).Value = item(t) Then 'this is the comparison of the array and the cell value
'MsgBox ("this item would be added into " & item(t))
'the below is the totaling of the parts totals.
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'Change the numbers in cells(arraycnt,X) to fit your sheet!!!
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
itemcostA(t) = itemcostA(t) + Cells(arraycnt, 3).Value
itemCostB(t) = itemCostB(t) + Cells(arraycnt, 4).Value
itemCostC(t) = itemCostC(t) + Cells(arraycnt, 5).Value
'MsgBox ("new total is: " & itemcostA(t) & " for array " & item(t))
itemdup(t) = 1 'now we mark the original element that it has a duplicate
itemdup(arraycnt) = 1 'now we mark the new element that it is a duplicate
'Cells(t, 10).Value = 1
'Cells(arraycnt, 10).Value = 1
End If
Next
Else
'add the item into the array, as it has not been encountered yet, and is not
'in the array element index yet.
itemrow(arraycnt) = iCntr
item(arraycnt) = Cells(iCntr, 1).Value
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
'Change the numbers in cells(arraycnt,X) to fit your sheet!!!
'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
itemcostA(arraycnt) = Cells(iCntr, 3).Value
itemCostB(arraycnt) = Cells(iCntr, 4).Value
itemCostC(arraycnt) = Cells(iCntr, 5).Value
'MsgBox (item(arraycnt) & " is set for arraycnt " & arraycnt)
End If
End If
Next
'this sets up the sheet2 for headers. Change A-E to whatever headers you wish.
'make certain that the numbers in this setting matches the comparison above
with ThisWorkbook.Worksheets("Sheet2")
.Cells(1, 1).Value = "A"
.Cells(1, 3).Value = "C"
.Cells(1, 4).Value = "D"
.Cells(1, 5).Value = "E"
End With
'now we loop through the array to copy the array elements that are marked as a duplicate
'and move them to the second sheet
For Xcntr = 1 To lastrow
'MsgBox (itemdup(Xcntr) & " is set for " & item(Xcntr))
'MsgBox ("item duplicate marked: " & itemdup(Xcntr) & " cell is " & Cells(Xcntr, 10))
If itemdup(Xcntr) = 1 Then
'Cells(Xcntr, 2).Value = "*"
thisistherow = findlastrow("Sheet2") + 1
'MsgBox ("this is the row: " & thisistherow)
'MsgBox ("moving it to: " & thisistherow)
'this is the copy VVVVVVVVVVV
With ThisWorkbook.Worksheets("Sheet2")
.Cells(thisistherow, 1).Value = item(Xcntr)
.Cells(thisistherow, 3).Value = itemcostA(Xcntr)
End With
End If
Next
deleterow ' this runs the delete row function to remove all marked as a duplicate
End Sub
Function findlastrow(whatsheet)
findlastrow = ThisWorkbook.Worksheets(whatsheet).Cells(Rows.Count, 1).End(xlUp).Row
End Function
Function deleterow()
therow = 1
For tr = 1 To findlastrow("Sheet1")
thetest = itemdup(tr)
If thetest = 1 Then
ThisWorkbook.Worksheets("Sheet1").Rows(therow).EntireRow.Delete
Else
therow = therow + 1
End If
Next
End Function
one,,10
two,,10
three,,10
orange,,10
Five,,10
seven,,10
peas,,10
here,,10
four,,10
there,,10
five,,10
six,,10
banana,,10
peas,,10
six,,10
banana,,10
- Posts
- 12936
- Registration date
- Monday June 3, 2013
- Status
- Moderator
- Last seen
- September 3, 2019
1342 -Activesheet.range ("$ g $1:$ g $10").removeduplicates Columns:=1, Header:==xlNo
Range ("g1:g10").select
Selection.copy
Sheets.add after:=Activesheet
Activesheet.paste
give it a try