Sorting Spreadsheet with Duplicate Values Using VBA

Sorting Spreadsheet with Duplicate Values Using VBA



I'm trying to create a macro that will check the values of Column A (Client ID number), identify duplicate values, then, once a duplicate value is found, perform a nested If/Then check that will return values into certain cells based on what it finds. If Column F (Program Description) of the row corresponding to the duplicate value contains the substring "UPGRADE", it should make the text of Column J of the row corresponding to the original value equal to the text of Column F in the duplicate value. Then, the duplicate row should be deleted, but I have not gotten that far yet.



Here's what I have so far-


Dim lastrow As Long

lastrow = Cells(Rows.Count, "A").End(xlUp).Row 'find last row in column A

For x = 1 To lastrow
If Cells(x, 1).Value <> ActiveCell.Value Then 'Check if cell in column A contains the same value as the activated cell

For y = 1 To lastrow
If Cells(y, 1).Value = Cells(x, 1).Value Then 'Compares cell against each value in column A. If there is a match, the do the following:

If Cells(y,6).Value <> "UPGRADE" Then 'Checks if duplicate value contains "UPGRADE"

Cells (x,10).Value= Cells(y,10).Value 'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.

Else Cells(x,12).Value=Cells(y,12).Value 'If the value is not found, copy the program type into a separate column for that program type in row x.
End If
Next y
End If
Next x



I tried running this program and received a "Next without For" error, but I'm not sure how to resolve it, or if the code would work if I did. Any help would be greatly appreciated.





Count your Ifs and End Ifs - you're missing an End If. Indentation would show this right away, too. Though I'm sure there's a more efficient way to do what you want.
– BigBen
Aug 21 at 18:52



If


End If


End If





You were right about the Next without For error, so thank you very much for that. Looks like I was missing an End If at the very end. The macro now runs, but it's not returning any values. I think the problem is on the fourth line. I set it to ActiveCell.Value because I've used that property before, but it may not have been the best one to use. Any thoughts?
– Learner04390
Aug 21 at 19:02





2 Answers
2



It is not perfectly clear what you want to do, but is the following working for you?


Public Sub checkDup()
Dim lastrow As Long, x As Long, y As Long

lastrow = Cells(rows.Count, "A").End(xlUp).Row 'find last row in column A
For x = 1 To lastrow
'If Cells(x, 1).Value <> ActiveCell.Value Then 'Check if cell in column A contains the same value as the activated cell
For y = x + 1 To lastrow
If Cells(y, 1).Value = Cells(x, 1).Value Then 'Compares cell against each value in column A. If there is a match, the do the following:
If Cells(y, 6).Value <> "UPGRADE" Then 'Checks if duplicate value contains "UPGRADE"
Cells(x, 10).Value = Cells(y, 6).Value 'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.
'Else
'Cells(x, 12).Value = Cells(y, 12).Value 'If the value is not found, copy the program type into a separate column for that program type in row x.
End If
End If
Next y
'End If
Next x

End Sub





Unfortunately not. I'll try to explain in a bit more detail. I have to check the values in column A for duplicates. When a duplicate value is found, I need to do an If/Then check on column 6 of the duplicate row (y in the code) to see if it contains a given value ("UPGRADE"). If it does, I need the contents of that cell to be copied and pasted (or simply mirrored) in the 10th column of the row that the original value was found in (x in the code). Did that help?
– Learner04390
Aug 21 at 19:46





Perhaps this revised code.
– Guest
Aug 21 at 20:07





We're getting closer, but not quite there yet. Your revised code worked correctly, but it also pulled one result that did not have the "UPGRADE" substring, which shouldn't be possible. You hid the "Else" condition behind a note, so if the value was not found, nothing should have happened. I'm trying to examine the code to see if I can evaluate why this happened. I will try to put some pictures together to give you a clearer idea of what happened.
– Learner04390
Aug 21 at 20:16






I did not encounter this behavior in my testing. However, if you have the same value in Column A more than twice and at least one does not have the UPGRADE value in the 6th column, then all the previous duplicates will see their 10th column value populated with the value UPGRADE.
– Guest
Aug 21 at 20:20






I just read your request again, and saw that you said equal to UPGRADE while the code all along was about different than UPGRADE. If you want to check if the 6th column of the duplicate is equal to UPGRADE and only change the 10th column value if the value of the previous duplicate in the 6th column is not UPGRADE, then you need to change the line for : If Cells(y, 6).Value = "UPGRADE" And Not Cells(x, 6).Value = "UPGRADE" Then
– Guest
Aug 21 at 20:30



If Cells(y, 6).Value = "UPGRADE" And Not Cells(x, 6).Value = "UPGRADE" Then



The next without for is because of the if then count not matching the end if count
Here, indentation is really useful.


Dim lastrow As Long
lastrow = Cells(Rows.Count, "A").End(xlUp).Row
'find last row in column A
For x = 1 To lastrow
If Cells(x, 1).Value <> ActiveCell.Value Then
'Check if cell in column A contains the same value as the activated cell
For y = 1 To lastrow
If Cells(y, 1).Value = Cells(x, 1).Value Then
'Compares cell against each value in column A. If there is a match, the do the following:
If Cells(y,6).Value <> "UPGRADE" Then
'Checks if duplicate value contains "UPGRADE"
Cells (x,10).Value= Cells(y,10).Value
'If this value is found, copy the value of the duplicate program name into a specified column for that program type in row x.
Else Cells(x,12).Value=Cells(y,12).Value
'If the value is not found, copy the program type into a separate column for that program type in row x.
End If
'I inserted another end if here
end if
Next y
End If
Next x





Much appreciated on the syntax tip. I tried running this code but it still did not work. I think the problem is in the fourth line that specifies the value of x that y is seeking a duplicate for.
– Learner04390
Aug 21 at 19:06





I personally never use activecell. I always specify a range, ( namedsheet.cells(1,1) or even namedsheet.[A1] )
– Kurt VonOhlen
yesterday






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)