Strip Formulas, Just Leave Values

Questions and answers on how to use XLSReadWriteII 3/4/5.
Post Reply
bcrooker
Posts: 3
Joined: Wed Dec 07, 2005 1:42 pm

Strip Formulas, Just Leave Values

Post by bcrooker »

I need to essentially copy the first sheet of a multi-sheet workbook to a new sheet and just take along the values, not formulas. Then delete all of the previous sheets.

So the workbook has sheets A, B and C. A shows summary calculations based on B and C. I want to add sheet D, copy the values from A to it and then delete sheets A, B and C.

My end result is that I want to have an XLS file that only has the Summary sheet.

I tried this but the formulas seem to be coming along:

XLS.Sheets.Add;

XLS.CopyCells(0, 0, 0, 100, 100, XLS.Sheets.Count - 1, 0, 0, [ccoCopyValues]);

while XLS.Sheets.count > 1 do
XLS.Sheets.Delete(0);

XLS.WriteToStream(aSummaryReportData);
larsa
Site Admin
Posts: 926
Joined: Mon Jun 27, 2005 9:30 pm

Re: Strip Formulas, Just Leave Values

Post by larsa »

Hello

CopyCells copies all cells, including formulas. If you want to strip formulas, you can use a code like this:

uses XLSReadWriteII4, SheetData4, CellFormats4, Cell4;

procedure TForm1.Button1Click(Sender: TObject);
var
Col,Row: integer;
begin
FXLS.Sheet[0].CalcDimensions;

for Row := FXLS.Sheet[0].FirstRow to FXLS.Sheet[0].LastRow do begin
for Col := FXLS.Sheet[0].FirstCol to FXLS.Sheet[0].LastCol do begin
if FXLS.Sheet[0].Cell[Col,Row].CellType in [ctNumberFormula,ctStringFormula,
ctBooleanFormula,ctErrorFormula,ctNumberArrayFormula,ctStringArrayFormula]
then
FXLS.Sheet[0].DeleteCell(Col,Row);
end;
end;
end;
Lars Arvidsson, Axolot Data
Post Reply