Stringgridで特定データの個数をカウントする方法について


たなはし  2014-07-12 04:32:12  No: 46446

Excelのcountif関数のような事は出来ますでしょうか?

例えば値が100以下のセル数を取得するといった感じです。


igy  2014-07-12 06:36:21  No: 46447

TStringGridには、その関数のようなメソッドは、ありませんので、
該当する列のセルが、条件に合うかどうかを、ループで1つ1つ確認するのがよいかと思います。


RAD命  2014-08-01 08:04:13  No: 46448

相当久しぶりに書き込みます。
あると便利そうなので、それらしいのを作ってみました。

※何分まだ未熟者ですので、この程度の簡易なものが限界ですが・・・。

count_if_int(
  StringList;
  '='or'>'or'<'or'>='or'<='or'<>'の何れかを指定;
  比較対象の数字
):返値(見つかった個数)
使用例:showmessage( count_if_int( st,'<',100 ) );
と言った具合に使います。

function count_if_int(st:tstringlist;s:string;set_no:integer):integer;
var
  i,i1,i2:integer;
  st1:TStringList;
  s1:string;
  function choice_if:string;
  begin
    choice_if := '';
    if s = '>' then begin
      if set_no > i2 then begin
        choice_if := st[i];
      end;
    end else if s = '>=' then begin
      if set_no >= i2 then begin
        choice_if := st[i];
      end;
    end else if s = '<' then begin
      if set_no < i2 then begin
        choice_if := st[i];
      end;
    end else if s = '<=' then begin
      if set_no <= i2 then begin
        choice_if := st[i];
      end;
    end else if s = '=' then begin
      if set_no = i2 then begin
        choice_if := st[i];
      end;
    end else if s = '<>' then begin
      if set_no <> i2 then begin
        choice_if := st[i];
      end;
    end else begin
     //
    end;
  end;
begin
   st1 := TStringList.Create;
   st1.Clear;
   for i := 0 to st.Count -1 do begin
     i2 := strtoint(st[i]);
     if '' <> choice_if then
       st1.Add(s1)
   end;
   count_if_int := st1.Count;
   st1.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  st:TStringList;
  i:integer;
begin
  st := TStringList.Create;
  st.Text:= memo1.Text;
  i := count_if_int(st,'=',8);
  showmessage(inttostr(i));
  st.Free;
end;


※返信する前に利用規約をご確認ください。

※Google reCAPTCHA認証からCloudflare Turnstile認証へ変更しました。






  このエントリーをはてなブックマークに追加