I am trying to compare the values in two columns for consecutive rows. Specifically, I would like to check the value under Column B and Column C of each row with the one directly above it. And if it matches, perform some XYZ action.
I have the code below which I tried to use but it keeps throwing up errors.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub SortAndMergeDupes()
With ActiveSheet
Dim lngRow As Long
Dim ws As Worksheet
Dim columnToMatch As Integer: columnToMatch =2
Dim column2ToMatch As Integer: column2ToMatch =3
Set ws = ThisWorkbook("Sheet1")
Set lngRow =.Cells(65536, columnToMatch).End(xlUp).Row
Do
If ws.Cells(lngRow, columnToMatch).Value = ws.Cells(lngRow -1, columnToMatch).Value And Cells(lngRow, column2ToMatch).Value = Cells(lngRow -1, column2ToMatch).Value Then
For i =4 To 50
If.Cells(lngRow -1, i).Value ="" Then
.Cells(lngRow -1, i).Value =.Cells(lngRow, i).Value
End If
Next i
.Rows(lngRow).Delete
End If
lngRow = lngRow -1
Loop Until lngRow =1
End With
End Sub
</code>Specifically, the error shows up in this line: (Usually - Error method of '_Default' if Object Range failed)
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">If ws.Cells(lngRow, columnToMatch).Value = ws.Cells(lngRow -1, columnToMatch).Value And Cells(lngRow, column2ToMatch).Value = Cells(lngRow -1, column2ToMatch).Value Then
</code>And now, as well as:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">If.Cells(lngRow -1, i).Value ="" Then
.Cells(lngRow -1, i).Value =.Cells(lngRow, i).Value
End If
</code>Any pointers?
I have the code below which I tried to use but it keeps throwing up errors.
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub SortAndMergeDupes()
With ActiveSheet
Dim lngRow As Long
Dim ws As Worksheet
Dim columnToMatch As Integer: columnToMatch =2
Dim column2ToMatch As Integer: column2ToMatch =3
Set ws = ThisWorkbook("Sheet1")
Set lngRow =.Cells(65536, columnToMatch).End(xlUp).Row
Do
If ws.Cells(lngRow, columnToMatch).Value = ws.Cells(lngRow -1, columnToMatch).Value And Cells(lngRow, column2ToMatch).Value = Cells(lngRow -1, column2ToMatch).Value Then
For i =4 To 50
If.Cells(lngRow -1, i).Value ="" Then
.Cells(lngRow -1, i).Value =.Cells(lngRow, i).Value
End If
Next i
.Rows(lngRow).Delete
End If
lngRow = lngRow -1
Loop Until lngRow =1
End With
End Sub
</code>Specifically, the error shows up in this line: (Usually - Error method of '_Default' if Object Range failed)
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">If ws.Cells(lngRow, columnToMatch).Value = ws.Cells(lngRow -1, columnToMatch).Value And Cells(lngRow, column2ToMatch).Value = Cells(lngRow -1, column2ToMatch).Value Then
</code>And now, as well as:
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">If.Cells(lngRow -1, i).Value ="" Then
.Cells(lngRow -1, i).Value =.Cells(lngRow, i).Value
End If
</code>Any pointers?
Last edited by a moderator: