function complexformula(gg:string):string;
 var s1,s2:string;
 s,e,w:integer;
begin
   s1:=gg;
   while Pos('BB(',s1)> 0 do      //找到第一个BB(出现的位置
   begin
    w:=length(s1);
    s:=Pos('BB(',s1);
    e:=pos(')',copy(s1,Pos('BB(',s1),length(s1)));     //截取出的BB公式
    s2:=copy(gg,s,e);
    Frmcell_design.cell2.SetFormula(1,1,0,s2);
    showmessage('欲删去公式为'+copy(s1,s,e));
    delete(s1,s,e);
s2:=trim(copy(s1,1,s-1))+trim(gs)+trim(copy(s1,s,w-s));
    if Pos('BB(',s2)>0 then 
    begin 
     showmessage('剃归');
     Result:=complexformula(s2) 
    end else begin Result:='';break;end;
  end;
end;

解决方案 »

  1.   

    试试:function complexformula(gg:string):string;
     var s1,s2:string;
     s,e,w:integer;
    begin
       s1:=gg;
       while Pos('BB(',s1)> 0 do      //找到第一个BB(出现的位置
       begin
        w:=length(s1);
        s:=Pos('BB(',s1);
        e:=pos(')',copy(s1,Pos('BB(',s1),length(s1)));     //截取出的BB公式
    //////////////////////
    e:=pos(')',copy(s1,Pos('BB(',s1),length(s1)+1-Pos('BB(',s1),));
    /////////////////////
        s2:=copy(gg,s,e);
        Frmcell_design.cell2.SetFormula(1,1,0,s2);
        showmessage('欲删去公式为'+copy(s1,s,e));
        delete(s1,s,e);
        s2:=trim(copy(s1,1,s-1))+trim(gs)+trim(copy(s1,s,w-s));
    ////////////////////////////
    s2写成s1?!
    gs是什么?不需要吧?
    w-s 应该改成:w-length(s2)吧?
    ////////////////////////////
        if Pos('BB(',s2)>0 then begin showmessage('剃归');complexformula(s2) end   
         else
         begin
          result:=s2;
          break;  //s2中已没有了BB,但又再次调用了complexformul,不可思议
         end;
       end;
    ////////////////////////////
    s2写成s1?!
    ////////////////////////////
    end;
    调用
    procedure TFrmcell_design.Button2Click(Sender: TObject);
    var
    ddd:string;
    begin
      inherited;
    ddd:='zw("a16","2001","101")+(BB( "a2","1-天","C" )-3)/(BB( "a2","1-天","C" )+zw("a16","2001","101"))';
    ddd:=complexformula(ddd);
    end;
      

  2.   

    试试:function complexformula(gg:string):string;
     var s1,s2:string;
     s,e,w:integer;
    begin
       s1:=gg;
       while Pos('BB(',s1)> 0 do      //找到第一个BB(出现的位置
       begin
        w:=length(s1);
        s:=Pos('BB(',s1);
        e:=pos(')',copy(s1,Pos('BB(',s1),length(s1)));     //截取出的BB公式
    //////////////////////
    e:=pos(')',copy(s1,Pos('BB(',s1),length(s1)+1-Pos('BB(',s1),));
    /////////////////////
        s2:=copy(gg,s,e);
        Frmcell_design.cell2.SetFormula(1,1,0,s2);
        showmessage('欲删去公式为'+copy(s1,s,e));
        delete(s1,s,e);
        s2:=trim(copy(s1,1,s-1))+trim(gs)+trim(copy(s1,s,w-s));
    ////////////////////////////
    s2写成s1?!
    gs是什么?不需要吧?
    w-s 应该改成:w-length(s2)吧?
    ////////////////////////////
        if Pos('BB(',s2)>0 then begin showmessage('剃归');complexformula(s2) end   
         else
         begin
          result:=s2;
          break;  //s2中已没有了BB,但又再次调用了complexformul,不可思议
         end;
       end;
    ////////////////////////////
    s2写成s1?!
    ////////////////////////////
    end;
    调用
    procedure TFrmcell_design.Button2Click(Sender: TObject);
    var
    ddd:string;
    begin
      inherited;
    ddd:='zw("a16","2001","101")+(BB( "a2","1-天","C" )-3)/(BB( "a2","1-天","C" )+zw("a16","2001","101"))';
    ddd:=complexformula(ddd);
    end;
      

  3.   

    没这么复杂吧
    function insteadstr(source,replaced,inserted:string):string;
    var
        apos:integer;
    begin
        apos:=pos(replaced,source);
        if apos=0 then result:=Source
        else
            result:=copy(source,1,apos-1)+inserted+insteadstr(copy(source,apos+length(replaced),length(source)-apos-length(replaced)+1),replaced,inserted);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
        k:string;
    begin
         k:='1112223445666878888';
         showmessage(insteadstr(k,'23','9'));
    end;