SOLVED. Copy and paste from Excel into a stringgrid lazarus / 21-General ---------------------------------------------------------------------- heavyconcrete · 2023-07-03 06:00 Is it possible to copy cells from Excel into a Stringgrid so that the rows of the stringgrid update automatically, without using a Rowcount? Anyone have an example? Thanx. ---------------------------------------------------------------------- wp · 2023-07-03 10:28 Move the string pasted from the clipboard into a stream and load that by means of the LoadFromStream method of the grid: 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 uses  ClipBrd; procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);var  s: String;  stream: TStringStream;begin  if (Shift = [ssCtrl]) and (Key = VK_V) then  begin    if (goEditing in StringGrid1.Options) and Clipboard.HasFormat(CF_TEXT) then    begin      s := Clipboard.AsText;      stream := TStringStream.Create(s);      try      StringGrid1.LoadFromCSVStream(stream, #9);      // Excel uses tabs as field delimiters        StringGrid1.FixedRows := 0;        StringGrid1.FixedCols := 0;      finally        stream.Free;      end;    end;  end;end; ---------------------------------------------------------------------- Jurassic Pork · 2023-07-03 11:29 Hello, example of what is inside the clipboard when you copy a range of cells from excel ( in attachment copy of the range A1:C2). in the CF_TEXT of the clipboard : 09 (tab) separator for columns 0D 0A (crlf) separator for rows The code of WP use this. friendly, J.P ---------------------------------------------------------------------- heavyconcrete · 2023-07-03 11:38 Awesome and how should I modify the code if I want cells copied from Excel to be pasted into the stringgrid starting from the selected cell? ---------------------------------------------------------------------- heavyconcrete · 2023-07-03 12:42 I wanted to copy a portion of cells from Excel and paste them into the stringgrid starting from a cell chosen arbitrarily with the command ctrl+v ---------------------------------------------------------------------- heavyconcrete · 2023-07-03 13:02 It would only suffice for me to add rows automatically to the stringgrid when cells are inserted from Excel ---------------------------------------------------------------------- wp · 2023-07-03 13:30 This was not clear from the first post. In order to place the pasted Excel cell range at an arbitrary location in the grid we should parse the csv text by ourselves. For very complex csv structures, the best way to go is to use the TCSVParser in the FCL csvreadwrite unit. A bit easier to understand is to use a TStringGrid which contains each row of the Excel range as a line, and each line can be split at the positions of the tab character to get the individual cells. At first we decide whether the current size of the grid must be extended. If the number of the read lines plus the index of the currently selected cell in the stringgrid exceeds the number of preallocated rows then we must redefine the number of rows: StringGrid.Rowcount := StringGrid.Row + (rows_in_clipboard). In the same way we take care of the columns, but here it must also be considered that the number of cells per row may change. Therefore we iterate over all rows and determine the number of cells in each row. The maximum count of the cells per row is the total number of columns needed for the clipboard values. 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 uses  ClipBrd; procedure TForm1.StringGrid1KeyDown(Sender: TObject; var Key: Word;  Shift: TShiftState);var  L: TStrings;  i, j, r, c, nc: Integer;  sa: TStringArray;begin  if (ssCtrl in Shift) and (Key = VK_V) then  begin    if Clipboard.HasFormat(CF_TEXT) and (goEditing in StringGrid1.Options) then    begin      L := TStringList.Create;      try        L.Text := Clipboard.AsText;        // Determine number of columns in clipboard        nc := 0;        for i := 0 to L.Count-1 do        begin          sa := L[i].Split(#9);          if Length(sa) > nc then nc := Length(sa);        end;        // If required, extend the number of columns        if StringGrid1.Col + nc >= StringGrid1.ColCount then          StringGrid1.ColCount := StringGrid1.Col + nc;        // If required, extend the number of rows        if StringGrid1.Row + L.Count >= StringGrid1.RowCount then          StringGrid1.RowCount := StringGrid1.Row + L.Count;         // Iterate over items in string list. Each item is a tab-separated      // list of fields per row.        r := StringGrid1.Row;        for i := 0 to L.Count-1 do        begin          c := StringGrid1.Col;        // Split stringlist item at tab.          sa := L[i].Split(#9);        // Copy strings into grid.          for j := 0 to Length(sa)-1 do            StringGrid1.Cells[c + j, r] := sa[j];          inc(r);      end;      finally        L.Free;      end;    end;  end;end; ---------------------------------------------------------------------- heavyconcrete · 2023-07-03 14:04 Exactly what I needed. Thank you!!! You have been very kind!