Find Same Row/Different Column Duplicates

Viewed 15

I want to VBA loop and highlight transactions (rows) where the bought and sold date entered in the same row are identical. The scenario is determining a percentage of day trades in a list of investments. Seems simple, but I can't find a search syntax that doesn't send me somewhere else.

1 Answers

Simple example code to your problem. Hope you will be able to modify it so suit your needs

Sub test()
    Dim Sheet1 As Worksheet
    Dim bought_range As Range
    Dim bought_cell As Object
    Dim sold_cell As Object
    
    Set Sheet1 = ThisWorkbook.Worksheets(1)
    Set bought_range = Sheet1.Range("B4:B20")
    
    For i = 1 To bought_range.Rows.Count Step 1
        Set bought_cell = bought_range.Cells(i, 1)
        Set sold_cell = bought_range.Cells(i, 2)
        
        If sold_cell.Value = bought_cell.Value Then
            bought_cell.Interior.Color = RGB(0, 255, 0)
            sold_cell.Interior.Color = RGB(0, 255, 0)
        End If
    Next i
End Sub

enter image description here

Related