procedure TForm1.Button24Click(Sender: TObject);
var
Src,Des, Tmp,Dic,Arr,Rng: OleVariant;
i, j,n,n1,lcol,lRows,dongcuoi,x: Longint;
TG: Double;
temp,key: string;
Start: Cardinal;
begin
Start := GetTickCount;
try
E := GetActiveOleObject('Excel.Application');
except
ShowMessage('khong lay duoc excel ?');
end;
Dic := CreateOleObject('Scripting.Dictionary') ;
j := 0;
Src := E.Range['A4:E20'].Value;
Des := E.Range['H4'];
lcol := VarArrayHighBound(Src, 2); //Cot
lRows := VarArrayHighBound(Src, 1); //dong
Arr := VarArrayCreate([1, lRows + 1, 1, lcol + 1],varVariant);
for i:=VarArrayLowBound(Src, 1) to VarArrayHighBound(Src,1) do begin
if Src[i, 2]<>'' then begin
Tmp := Src[i, 2];
if not dic.Exists(Tmp) then begin
j := j+1;
Dic.Add(Tmp, j);
Arr[j, 1] := Src[i, 1];
Arr[j, 2] := Src[i, 2];
Arr[j, 3] := Src[i, 3];
Arr[j, 4] := Src[i, 4];
Arr[j, 5] := Src[i, 5];
end else begin
x := Dic.Item[Src[i, 2]];
Arr[x, 5] := IntToStr(StrToIntDef(Trim(VarToStr(Arr[x, 5])), 0) + StrToIntDef(Trim(VarToStr(Src[i, 5])), 0));
end;
end;
end;
if j<>0 then begin
Des.Resize[j, VarArrayHighBound(Arr, 2)].Value:=Arr;
end;
ShowMessage(Format('Thời gian lọc là : %d ms', [GetTickCount - Start]));
end;