-1

I want to compare values from two columns at once and I created wrote this code: It not works, first of all the second for loop (x1) uses different cells, but my intention was to use a break to get out from the for -loop after each step. I tried with Exit For but is not working. Any idea how I could compare 2 cells from 2 columns at once (with 2 for loops or not)? Thanks!

For x2 = x2Row To 2 Step -1
    For xl = xRow To 2 Step -1
          If ((Cells(xl, xCol) = Cells(xl - 1, xCol)) And (Cells(x2, x2Col) = Cells(x2 - 1, x2Col))) Then

        Cells(xl, xCol) = ""

    End If

    Next xl
Next x2
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
  • `I want to compare values from two columns at once` Have you seen @TimWilliams answer [Here](https://stackoverflow.com/questions/19395633/how-to-compare-two-entire-rows-in-a-sheet) – Siddharth Rout Jan 28 '19 at 09:43
  • Actually is not the same because my for goes "To 2 Step -1".. because I always need to compare 2 cells from a column and only then I switch to the second column – Catalin Vasilescu Jan 28 '19 at 09:48
  • I tried something like this with exist for but is not working For x2 = x2Row To 2 Step -1 For xl = xRow To 2 Step -1 If ((Cells(xl, xCol) = Cells(xl - 1, xCol)) And (Cells(x2, x2Col) = Cells(x2 - 1, x2Col))) Then Cells(xl, xCol) = "" End If Next xl Exit For Next x2 – Catalin Vasilescu Jan 28 '19 at 09:53
  • Have you got an example data set you’re working with? – Skin Jan 28 '19 at 10:08

1 Answers1

1

Do you want this: Loop over all rows and check if there is a vertical pair. If yes, then check if its direct neighbor is also a vertical pair?

Then please try this

For x1 = xRow To 2 Step -1
    If Cells(x1, xCol) = Cells(x1 - 1, xCol) Then
        If Cells(x1, x2Col) = Cells(x1 - 1, x2Col) Then
            Cells(x1, xCol) = ""
        End If
    End If
Next x1

(Edit: xl vs. x1)

Asger
  • 3,822
  • 3
  • 12
  • 37