Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It looks like you have an additional <code>|&gt;</code> operator at the end of the line with <code>Seq.sort</code> - this means that the list is sorted and then, the compiler tries to pass it to the expression that performs the assignment (which does not take any parameter and has a type <code>unit</code>).</p> <p>Something like this should compile (though there may be some other runtime issues):</p> <pre><code>for i in 1 .. rows do let row = inSheet.Cells.Rows.[i] :?&gt; Range let sorted = row |&gt; toSeq |&gt; Seq.map (fun x -&gt; x.Value2.ToString()) |&gt; Seq.sort (outSheet.Cells.Rows.[i] :?&gt; Range).Value2 &lt;- Array.ofSeq sorted </code></pre> <p>Note that you do not need to mark <code>row</code> as mutable, because the code creates a copy (and - in my version - assigns it to a new variable <code>sorted</code>).</p> <p>I also use <code>Array.ofSeq</code> to convert the sorted sequence to an array, because I think the Excel interop works better with arrays. </p> <p>When setting the <code>Value2</code> property on a range, the size of the range should be the same as the size of the array that you're assigning to it. Also, depending on the range you want to set, you might need a 2D array.</p> <p><strong>EDIT</strong> Regarding runtime errors, I'm not entirely sure what is wrong with your code, but here is how I would do the sorting (assuming you have just one column with string values and you want to sort the rows):</p> <pre><code>// Get the row count and calculate the name of the last cell e.g. "A13" let rows = inSheet.UsedRange.Rows.Count let rangeEnd = sprintf "A%d" rows // Get values in the range A1:A13 as 2D object array of size 13x1 let values = inSheet.Range("A1", rangeEnd).Value2 :?&gt; obj[,] // Read values from the first (and only) column into 1D string array let data = [| for i in 1 .. rows -&gt; values.[i, 1] :?&gt; string |] // Sort the array and get a new sorted 1D array let sorted1D = data |&gt; Array.sort // Turn the 1D array into 2D array (13x1), so that we can write it back let sorted2D = Array2D.init rows 1 (fun i _ -&gt; data.[i]) // Write the data to the output sheet in Excel outSheet.Range("A1", rangeEnd).Value2 &lt;- sorted </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload