[SOLVED] search "while FindNext", does not return the row column correctly lazarus / 49-FPSpreadsheet ---------------------------------------------------------------------- rca · 2024-08-08 19:15 I need to find the first and the last, for that I have performed the following functions: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.FindLastTxt(searchtxt:string) : boolean;var  foundWorksheet: TsWorksheet;  MySearchParams: TsSearchParams;  tmpfind: Boolean; begin  foundRow:=0;  foundCol:=0;   tmpfind:=False;   MySearchParams.SearchText := searchtxt;  MySearchParams.Options := [soCompareEntireCell,soEntireDocument];  MySearchParams.Within := swWorksheet;   with TsSearchEngine.Create(sWGrid.Workbook) do begin  if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then    begin      while FindNext(MySearchParams, foundWorksheet, foundRow, foundCol) do      begin        tmpfind:=True;      end;    end;    Free;  end;   Result:=tmpfind;end;  Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.FindFirstTxt(searchtxt:string) : boolean;var  foundWorksheet: TsWorksheet;  MySearchParams: TsSearchParams;  tmpfind: Boolean; begin  foundRow:=0;  foundCol:=0;   tmpfind:=False;   MySearchParams.SearchText := searchtxt;  MySearchParams.Options := [soCompareEntireCell,soEntireDocument];  MySearchParams.Within := swWorksheet;   with TsSearchEngine.Create(sWGrid.Workbook) do begin  if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then    begin      tmpfind:=True;    end;    Free;  end;   Result:=tmpfind;end;  Where, Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var  Form1: TForm1;  foundRow, foundCol: Cardinal; With FindFirstTxt: Successfully selects the first cell found and returns foundRow and foundCol successfully. With FindLastTxt: selects the last found cell correctly, but returns foundRow and foundCol with incorrect values. I attach an image and the code of the test program. I appreciate the help you can give me. ---------------------------------------------------------------------- paweld · 2024-08-09 06:05 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.FindLastTxt(searchtxt: String): Boolean;var  foundWorksheet: TsWorksheet;  MySearchParams: TsSearchParams;  tmprow, tmpcol: Cardinal;begin  foundRow := 0;  foundCol := 0;   MySearchParams.SearchText := searchtxt;  MySearchParams.Options := [soCompareEntireCell, soEntireDocument];  MySearchParams.Within := swWorksheet;   with TsSearchEngine.Create(sWGrid.Workbook) do  begin  Result := FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol);    tmprow := foundRow;    tmpcol := foundCol;    while Result and FindNext(MySearchParams, foundWorksheet, tmprow, tmpcol) do    begin      foundRow := tmprow;      foundCol := tmpcol;    end;    Free;  end;end; ---------------------------------------------------------------------- rca · 2024-08-09 09:31 @paweld Now it works. Thanks for your help. ---------------------------------------------------------------------- wp · 2024-08-11 06:45 The other solution, built into FPSpreadsheet, is: Add option "boBackward" to MySearchParam.Options of the "FindLastTxt" procedure Remove the "while FindNext" loop from "FindLastTxt" so that the Searchengine does the same as in "FindFirstTxt"Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function TForm1.FindLastTxt(searchtxt:string) : boolean;var  foundWorksheet: TsWorksheet;  MySearchParams: TsSearchParams;  tmpfind: Boolean; begin  foundRow:=0;  foundCol:=0;   tmpfind:=False;   MySearchParams.SearchText := searchtxt;  MySearchParams.Options := [soBackward, soCompareEntireCell,soEntireDocument];  MySearchParams.Within := swWorksheet;   with TsSearchEngine.Create(sWGrid.Workbook) do begin    if FindFirst(MySearchParams, foundWorksheet, foundRow, foundCol) then    begin      tmpfind:=True;    end;    Free;  end;   Result:=tmpfind;end;Your issue probably is that SearchEngine.FindFirst does not mean "Find first (top,left) cell in search range", but "Find first cell which meets the search criteria."; the search direction is determined by the options, ---------------------------------------------------------------------- rca · 2024-08-12 15:19 @wp Tested and it works too! Thanks for your explanation and help.