title: How to remove duplicates correctly using FOR loop
body: <p>My question might seem too easy but unfortunately i am just a beginner in VBA. </p>
  
  <p>I have a table with three columns where 1st contains company names (Column B), second- Directors' names (Column C), 3rd - Directors' IDs (column D) and the list of all companies (Column F). One company has a number of different directors so that in Column B a company name is repeated as many times as a number of directors in this company. Some companies can share the same director so that in Column C and D there are duplicates of names and IDs. However, according to the data some names are repeated several times within one company(kinda redundant information).</p>
  
  <p>Here is an example of my table</p>
  
  <p><img src="https://i.stack.imgur.com/viCop.jpg" alt="enter image description here"></p>
  
  <p>So my target is to remove (delete) this redundant info, i.e. to remove Director duplicates for a particular company WHILE keeping those duplicates that are caused by the fact that several companies are sharing one or more directors. Technically I need to select the range of directors and their IDs corresponding to a certain company and then remove duplicates by IDs. And then repeat it for each company. Since I have 500 companies I would like to automate it somehow</p>
  
  <p>here is the code i built in VBA (Remove duplicates function+for loop):</p>
  
  <pre><code>Sub test()
  
      Dim rcompany As Variant
      For X = 0 To 500
      rcompany = Range("F2").Offset(X, 0)
  
      Dim c As Range, r As Range
  
  
      For Each c In Range("B1:B23500")
          If c.Value = rcompany Then
              If r Is Nothing Then
                  Set r = c.Offset
                        Else
                  Set r = Union(r, c)
              End If
          End If
  
      Next c
      Union(r, r.Offset(, 1), r.Offset(, 2)).RemoveDuplicates Columns:=3, Header:=xlNo
  
      Next
  End Sub
  </code></pre>
  
  <p>The problem with this code is that if I run it without a loop (for a particular company) it works well. But as soon as i add for loop it works well for 1st step but then it starts removing all duplicates within the whole Column D but not within a range according to a company name.</p>
  
  <p>Could you please help me with it? how to improve it?</p>