I am trying to delete entire row if duplicates are found together. If not found together I want to keep it without deleting.
For an example Column A:
Apple,
Apple,
Orange,
Orange,
Apple,
Apple,
I need to have the output as;
Apple,
Orange,
Apple,
I'm using the following code but have not achieved the desired output yet (only get Apple, Orange).
Sub FindDuplicates()
Dim LastRow, matchFoundIndex, iCntr As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For iCntr = 1 To LastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & LastRow), 0)
If iCntr <> matchFoundIndex Then
Cells(iCntr, 10) = "Duplicate"
End If
End If
Next
last = Cells(Rows.Count, "J").End(xlUp).Row ' check results col for values
For i = last To 2 Step -1
If (Cells(i, "J").Value) = "" Then
Else
Cells(i, "J").EntireRow.Delete ' if values then delete
End If
Next i
End Sub
Doesn't something simple like
Dim LastRow As Long
Application.screenUpdating=False
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For i = LastRow To 2 Step -1
If Cells(i, 1).Value = Cells(i - 1, 1).Value Then
Cells(i, 1).EntireRow.Delete
End If
Next i
Application.screenUpdating=True
solve this?
Work from the bottom up and only delete if the cell above is the same value.
dim r as long
with worksheets("sheet1")
for r = .cells(.rows.count, "A").end(xlup).row to 2 step -1
if lcase(.cells(r, "A").value2) = lcase(.cells(r - 1, "A").value2) then
.rows(r).entirerow.delete
end if
next r
end with
If you do not want the comparison to be case-insensitive then remove the lcase functions.
Related
I have a problem that I hope you can help me with.
The below code adds 1 day to todays day in Column B if it finds a repeated value in column A. However I want it to add 2 days if it gets repeated again and so on.
I have tried to illustrate how the codes work in the attached picture. So what I want is cell B10 in the picture to be 20/03/2021. I need to make it automatic so it can run for any number of repeated values.
Sub Add_date2()
Dim lastRow As Long
Dim matchFoundIndex As Long
Dim iCntr As Long
lastRow = Range("A65000").End(xlUp).Row
For iCntr = 2 To lastRow
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & lastRow), 0)
If iCntr <> matchFoundIndex Then
Cells(iCntr, 2) = Date + 1
Else
Cells(iCntr, 2) = Date
End If
End If
Next
End Sub
Use Application.Countifs:
Sub Add_date2()
Dim ws As Worksheet
Set ws = ActiveSheet 'better to set the actual sheet WorkSheets("Sheet1")
With ws
Dim lastRow As Long
lastRow = .Cells(Rows.Count, 1).End(xlUp).Row
Dim iCntr As Long
For iCntr = 2 To lastRow
If .Cells(iCntr, 1) <> "" Then
.Cells(iCntr, 2) = Date + Application.CountIfs(.Range(.Cells(2, 1), .Cells(iCntr, 1)), .Cells(iCntr, 1)) - 1
End If
Next
End With
End Sub
What I'm trying to do is remove any rows where a cell value in a specific column matches what is defined to remove. After that is done re-sequence the value in another column by group.
Using the example below:
I want to look at column B and remove any rows that have a value of A or C. Then I want to basically renumber after the dot (.) in column A to reset itself.
Before Macro Code Fig. 1
After value A and C are removed Fig. 2
Final list after column A is renumbered Fig. 3
I figured out how to remove the rows using this code, but stuck on what to do next:
Sub DeleteRowBasedOnCriteriga()
Dim RowToTest As Long
For RowToTest = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 2)
If .Value = "A" Or .Value = "C" _
Then _
Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest
End Sub
This will be easier to do looping from the top down (using step 1 instead of step -1). I've tried to stay true to your original coding and made this:
Sub DeleteRowBasedOnCriteriga()
Dim RowToTest As Long
Dim startRow As Long
Dim i As Integer
startRow = 2
'Clear the rows that have "A" or "C" in column B
For RowToTest = Cells(Rows.Count, 1).End(xlUp).Row to startRow To Step -1
With Cells(RowToTest, 2)
If .Value = "A" Or .Value = "C" _
Then _
Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest
'If the left 3 characters of the cell above it are the same,_
'then increment the renumbering scheme
For RowToTest = startRow To Cells(Rows.Count, 1).End(xlUp).Row
If Left(Cells(RowToTest, 1).Value, InStr(1, Cells(RowToTest, 1), "\")) = Left(Cells(RowToTest, 1).Offset(-1, 0).Value, InStr(1, Cells(RowToTest, 1), "\")) Then
i = i + 1
Cells(RowToTest, 1).Value = Left(Cells(RowToTest, 1).Value, InStr(1, Cells(RowToTest, 1), ".")) & i
Else
i = 0
Cells(RowToTest, 1).Value = Left(Cells(RowToTest, 1).Value, InStr(1, Cells(RowToTest, 1), ".")) & i
End If
Next RowToTest
End Sub
EDIT: I've updated it to compare all of the string before the backslash and compare using that.
EDIT++: It has been brought to my attention that when deleting rows it is better to work from the bottom up (step -1) to ensure every row is accounted for. I've re-implemented the original steps in the first code.
Admittedly, this isn't probably the most efficient, but it should work.
Sub DeleteRowBasedOnCriteriga()
Dim RowToTest As Long, i As Long
Application.ScreenUpdating = False
For RowToTest = Cells(Rows.Count, 2).End(xlUp).Row To 2 Step -1
With Cells(RowToTest, 2)
If .Value = "A" Or .Value = "C" Then Rows(RowToTest).EntireRow.Delete
End With
Next RowToTest
Dim totalRows As Long
totalRows = Cells(Rows.Count, 1).End(xlUp).Row
Dim curCelTxt As String, aboveCelTxt As String
For i = totalRows To i Step -1
If i = 1 Then Exit For
curCelTxt = Left(Cells(i, 1), WorksheetFunction.Search("\", Cells(i, 1)))
aboveCelTxt = Left(Cells(i - 1, 1), WorksheetFunction.Search("\", Cells(i - 1, 1)))
If curCelTxt = aboveCelTxt Then
Cells(i, 1).Value = ""
Else
Cells(i, 1).Value = WorksheetFunction.Substitute(Cells(i, 1), Right(Cells(i, 1), Len(Cells(i, 1)) - WorksheetFunction.Search(".", Cells(i, 1))), "0")
End If
Next i
Dim rng As Range, cel As Range
Dim tempLastRow As Long
Set rng = Range("A1:A" & Cells(Rows.Count, 2).End(xlUp).Row)
For Each cel In rng
If cel.Offset(1, 0).Value = "" Then
tempLastRow = cel.End(xlDown).Offset(-1, 0).Row
If tempLastRow = Rows.Count - 1 Then
tempLastRow = Cells(Rows.Count, 2).End(xlUp).Row
cel.AutoFill Destination:=Range(cel, Cells(tempLastRow, 1))
Exit For
Else
cel.AutoFill Destination:=Range(cel, Cells(tempLastRow, 1))
End If
End If
Next cel
Application.ScreenUpdating = True
End Sub
Mainly, I discovered that you can use AutoFill to fix the last number in the string. Meaning if you AutoFill this text, CAT\Definitions.0 down, you get the number updating as you drag/fill.
I have will have following values in Column A;
A1: 11/5, A2: 11/5R,A3:11/6, A4:11/7, A5:11/5$, A6: 11/5
I want to mark these cells as duplicates in Column 10 if the numbers are same without considering the end symbol: Ex: A1, A2, A5, A6 are duplicates and should mark in Col J1, J2, J5, and J6.
Following code is working only for numbers. it is not accounting the end symbols of the cell value. Any suggestions to modify the code to get the desired output are appreciated.
Sub FindDuplicates()
Dim LastRow As Long, matchFoundIndex As Long, iCntr As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For iCntr = 1 To LastRow ' 1 set the start of the dup looks
If Cells(iCntr, 1) <> "" Then
matchFoundIndex = WorksheetFunction.Match(Cells(iCntr, 1), Range("A1:A" & LastRow), 0)
If iCntr <> matchFoundIndex Then
Cells(iCntr, 10) = "Duplicate"
Else
Cells(iCntr, 10) = "E"
End If
End If
Next
End Sub
Thank you
Assuming that the last character in the cell should be ignored if it is not a number:
Sub FindDuplicates()
Dim v As Variant
Dim LastRow As Long, x As Long
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
For x = 1 To LastRow ' 1 set the start of the dup looks
If Cells(x, 1) <> "" Then
v = IIf(IsNumeric(Right(Cells(x, 1), 1)), Cells(x, 1), Left(Cells(x, 1), Len(Cells(x, 1)) - 1))
Cells(x, "J") = IIf(Application.Match(v, Range("A1:A" & LastRow), 0) > 1, "E", "Duplicate")
End If
Next
End Sub
This is what my data looks like. Its a summary from another sheet.
The code appears to run and do what I need, scan Co B and delete if there is a 0 value, scan Col I and insert row if row below is different, then repeat for Col H. However I get an "runtime 1004 application-defined or object defined error"message instead of the macro just ending. Open to any edits or suggestions
Range("A100000").End(xlUp).Activate
Range("N1") = ActiveCell.Row
For lRow = Cells(Cells.Rows.Count, "b").End(xlUp).Row To 1 Step -1
If Cells(lRow, "b") = 0 Then
Rows(lRow).EntireRow.Delete
End If
Next lRow
For lRow = Cells(Cells.Rows.Count, "I").End(xlUp).Row To 1 Step -1
If Cells(lRow, "I") <> Cells(lRow - 1, "I") Then '<~~ debugger highlights this line or the other version of this eq below
Rows(lRow).EntireRow.Insert
End If
Next lRow
For lRow = Cells(Cells.Rows.Count, "H").End(xlUp).Row To 1 Step -1
If Cells(lRow, "H") <> Cells(lRow - 1, "H") Then
Rows(lRow).EntireRow.Insert
End If
Next lRow
Range("A1").Activate
Application.ScreenUpdating = True
End Sub
If Cells(lRow, "I") <> Cells(lRow - 1, "I") Then
When lRow inevitably reached the value of 1 then this is going to cause an error, because
lRow - 1
is 0 - and there isn't a row number of 0.
You need to code for this possibility:
For lRow = Cells(Cells.Rows.Count, "I").End(xlUp).Row To 1 Step -1
If Cells(lRow, "I") <> Cells(lRow - IIf(lRow = 1, 0, 1), "I") Then
Rows(lRow).EntireRow.Insert
End If
Next lRow
Should do the trick.
I am trying to insert one row when the cell is column 4 doesn't have the same value. For some reason it is inserting 4 rows. It only happens at the start. What could be wrong?
Thanks for your help!
If Cells(j, 4) <> Cells(j - 1, 4) Then
Cells(j, 1).EntireRow.Insert
j = j + 1
End If
Is this what you are trying?
Sub Sample()
Dim ws As Worksheet
Dim lRow As Long, i As Long
'~~> Change this to the relevant sheet
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
lRow = .Range("D" & .Rows.Count).End(xlUp).Row
For i = lRow To 2 Step -1
If .Cells(i, 4) <> Cells(i - 1, 4) Then .Cells(i, 1).EntireRow.Insert
Next i
End With
End Sub
ScreenShot: