I'm trying to use the RemoveDuplicates
function using Excel.Interop
, but I'm stuck as to how to pass it the column array. I already know that I cannot pass it as a simple int[]
array, as it gives an exception at runtime, and that I can pass a single integer and it works, but I want to be able to select which columns to use at runtime.
My current code in C# looks like this:
using Excel = Microsoft.Office.Interop.Excel;
private void removeDuplicates(Excel.Application excelApp, Excel.Range range, int[] columns)
{
range.RemoveDuplicates(excelApp.Evaluate(columns),
Excel.XlYesNoGuess.xlNo);
}
And it works fine if using only one column, but if the columns
array has more than one value, only the first one is used.
In VBA, the equivalent function would be:
Sub RemoveBadExample()
Dim colsToUse
colsToUse = Array(1, 2)
Selection.RemoveDuplicates Columns:=Evaluate(colsToUse), Header:=xlYes
End Sub
Which also fails to use both columns. however, if I change it to this:
Selection.RemoveDuplicates Columns:=(colsToUse), Header:=xlYes
It works just fine. I guess my question then is what is the equivalent in C#?