title: VBA's .removeDuplicates does not appear to work... Why?
body: <p>I have a column vector, let's say</p>
  
  <p>1 2 1 2 1 1 1 1 1</p>
  
  <p>and I need to programmatically remove duplicates.  So after the removal of duplicates the output should be just:</p>
  
  <p>1 2</p>
  
  <p>However, VBA's .removeDuplicates method fails, triggering Error 1004 (application defined error).<br>
  My setup as follows:
  I have one workbook that contains the actual data.  Second workbook contains the vba code that operates on the first workbook.  </p>
  
  <p>Observation #1:</p>
  
  <p>When I execute the .removeDuplicates method from within the data-containing workbook and therefore data containing worksheet, the code works like a charm.</p>
  
  <pre><code>sub test  
   ActiveSheet.Range("$a$2:$a$20").RemoveDuplicates Columns:=1, Header:=xlYes
  end sub
  </code></pre>
  
  <p>Observation #2:</p>
  
  <p>However the moment that I try to run the very same code from a another workbook (with slight modification to address the fact the code is external to the first workbook), the .removeDuplicates fails:</p>
  
  <pre><code>Sub test()
      Dim wb As Workbook
      Dim sh As Worksheet
  
      'get a handle to data containing workbook and sheet
      Set wb = Workbooks(1) 'change wb index as needed
      Set sh = wb.Sheets("s2") ' change sheet name  as needed
      sh.Range("$a$2:$a$20").RemoveDuplicates Columns:=Array(1), Header:=xlYes
  End Sub
  </code></pre>
  
  <p>Anyone had to deal with this before?  Can I not use .removeDuplicates from another workbook?  I look at the MSDN's API and there is no mention on limitations... </p>