0
procedure TForm1.Button1Click(Sender: TObject);    
var    
   xlap,xlbook,xlsheet:variant;
   y:integer;
begin
   xlap:=createoleobject('excel.application');
   xlbook:=xlap.workbooks.add;
   xlap.visible:=true;
   xlsheet:=xlbook.worksheets.add;

   for y:=1 to 50 do
   begin 
    xlsheet.cells[y,2].value:= concat('=IF(A',y,'>6,"Good","Bad")')
    inc(y);
  end;
end;

That's my code to make an Excel through Delphi. I want to input the IF formula into column B of the Excel, but there is error when I use the concat('=IF(A',y,'>6,"Good","Bad")').

May be I need another way to include y between those strings.

Any suggestion? Thanks before

6
  • 1
    Maybe IntToStr(y)? Delphi is strongly typed... you can't mix strings and integers like that. Commented May 21, 2014 at 16:09
  • Which delphi unit has a concat function? And if the complete error text is not top secert please add this to your question Commented May 21, 2014 at 16:13
  • Do you mean this? xlsheet.cells[y,2].value:= '=IF(A',IntToStr(y),'>6,"Good","Bad")' Commented May 21, 2014 at 16:14
  • the error message: [Error] USimpanMysql.pas(190): '.' expected but ';' found. this is when I use xlsheet.cells[y,2].value:= concat('=IF(A',y,'>6,"Good","Bad")') Commented May 21, 2014 at 16:18
  • @SirRufo, Concat is a magic function, it's been in the language since the beginning. Commented May 21, 2014 at 16:18

2 Answers 2

6

Delphi has a format statement bit like sprintf printf in c, well nearly

xlsheet.cells[y,2].value:= format('=IF(A%d>6,"Good", "Bad")',[y])

%d is a place holder for an int. Look it up for loads of other stuff. NB the variables you want to interpolate are assed in an array.

Sign up to request clarification or add additional context in comments.

1 Comment

It works. Thanks so much Mr Tony. By the way, there is no comma between A and %d
3

In addition to Tony's answer about Format, here are a couple of other approaches. (Format is great if you have mixed types or many values, but it carries some overhead that might not be needed.)

You can concatenate strings with a simple + - in fact, the Concat documentation says it does the same thing but is faster:

Temp := 'ing';
Str := 'Test' +  Temp;   // Str = 'Testing'

As your y variable is an integer, you'll need to convert it to a string first (note you don't need to Inc(y);, as the loop will do that already - it's automatically incremented from the starting value to the ending value on each pass through the loop):

for y:=1 to 50 do
begin 
  xlsheet.cells[y,2].value:= '=IF(A' + IntToStr(y) + '>6,"Good","Bad")';
end;

2 Comments

It works. Thanks very much Mr Ken White. But there is no comma between A and '
@Ray: Yes, I caught that and fixed it. Thanks. :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.