Hi Gary and Peter, thanks so much for the assistance. Sorry for the late reply since I couldn't access my work files from home over the weekend.
Anyway I realized I was unclear in the description of my problem here.
Let me rephrase my data set again for better clarity:
No.
| Code
| Name
| Amount
|
1
| S12345678C
| John
| 1000
|
2
| S12345678E
| John
| -300
|
3
| S12345678A
| John
| -500
|
4
| S12345678B
| John
| 500
|
5
| 54321
| John
| -1000
|
<tbody>
</tbody>
The positive and negative values of the same absolute value are NOT in adjacent rows, i.e. They can be anywhere in the data sheet, some rows may be pairs, while others may not, such as row 2 above.
Anyway I also realized that I was wrong in my earlier request: when two rows have opposite but equal amount in the cells of the fourth column, but one do not start with S in the cells of the second column, delete that row, and leave the one with S in the cell of second column untouched.
Hence the data should look like (after running the code):
No.
| Code
| Name
| Amount
|
1
| S12345678C
| John
| 1000
|
2
| S12345678E
| John
| -300
|
<tbody>
</tbody>
Just to address Peter's question, it is highly unlikely for three same rows to have opposite values, so it should not be a problem. There is also no two rows with opposite values in column 4 but do not start with S in column 2.
I searched through some old threads and found this code to be useful:
Code:
Sub DeleteZeroSumRows()
CC = "K" 'column to check
With ActiveSheet
LR = .Cells(Rows.Count, CC).End(xlUp).Row 'last row in Column to check
For r = LR To 2 Step -1
pr = r - 1
For rr = pr To 1 Step -1
If .Cells(r, CC) = -(.Cells(rr, CC)) And UCase(Left(Cells(r, "E").Text, 1)) = "S" Then
.Rows(r).Delete
.Rows(rr).Delete
Exit For
End If
Next rr
Next r
End With
End Sub
However it showed the
Run-time error '13': Type mismatch error when I tried to run it and this line
Code:
If .Cells(r, CC) = -(.Cells(rr, CC)) And UCase(Left(Cells(r, "E").Text, 1)) = "S" Then
was highlighted yellow.
I suspect that this may be due to the heading of the data being non-numerical, so is there anyway to get over this error? In addition it doesn't address my criteria regarding the presence of "S" in the second column.
Sorry for the wall of words as I'm trying to be as clear to avoid any confusion.
Again, thanks so much for the help and assistance in advance!