I am using Delphi 7 to write a program to read data from Excel. The Excel file contains two columns of data. Now I need to remove duplicates from range Excel via Delphi.
If it helps any, the VBA macro code is below:
ActiveSheet.Range("$A$1:$B$42").RemoveDuplicates Columns:=Array(1, 2), Header _
:=xlYes
Excel_TLB procedure is below:
procedure RemoveDuplicates(Columns: OleVariant; Header: XlYesNoGuess);
Notations from VBA help:
Columns - Variant Array of indexes of the columns that contain the duplicate information.
My code is below:
procedure TForm1.PuskBClick(Sender: TObject);
var
FExcel, FSheet, FBook: Variant;
MyColumns: OleVariant;
begin
// open file
if OpenDialog.Execute then
begin
// create Excel
FExcel := CreateOleObject('Excel.Application');
FExcel.Visible := true;
if not VarIsEmpty(FExcel) then // if not empty then
begin
// open real file Excel and define workbook
FBook:=FExcel.Workbooks.Open(OpenDialog.FileName);
// off any Alerts
FExcel.DisplayAlerts := false;
// define WorkSheet
FSheet := FBook.WorkSheets[1];
// create array for define columns numbers
MyColumns:= VarArrayCreate([1,1,1,2], varVariant);
MyColumns[1,1]:=1; // column 1
MyColumns[1,2]:=2; // column 2
//call the procedure
FSheet.Range['A1','B42'].RemoveDuplicates(MyColumns[1,1], xlYes);
end;
end;
end;
This code works fine, but the calculation is only done on the first column. If I change the index, then the calculation is only done on the second column.
FSheet.Range['A1','B42'].RemoveDuplicates(MyColumns[1,**2**], xlYes);
How should I specify an array for two columns at once?
I got the answer.
Need to use procedure so:
FSheet.Range['A1','B42'].RemoveDuplicates(VarArrayOf([1,2]), xlYes);
or so:
FSheet.UsedRange.RemoveDuplicates(VarArrayOf([1,2]), xlYes);
Where [1,2] - are indexes of the columns that contain the duplicate information in the Excel worksheet.
That's right.
Thank you for help me!
I'm understanding so:
If need to use columns 7 and 20 then you should put
VarArrayOf([7, 20])
etc.