Note that there are some explanatory texts on larger screens.

plurals
  1. POPassing Arrays and a Range to Excel's .sort Algorithm
    primarykey
    data
    text
    <p>There are essentially 2 questions: </p> <p><strong>For excel's .sort function: How do I add keys when I do not know how many keys (headers) a given sheet is going to have?</strong> (See Below)</p> <p><strong>I want to pass a number of predefined arrays, containing information on how to sort a given selection, to excel's .sort algorithm... How do I do this correctly?</strong></p> <p><strong>Problem Description:</strong></p> <p>I have a sheet that lists headers in the first row. The headers are different names and there can be a variable number of headers present in the first row. The headers describe different details of metrics listed in the rows below on the sheet (one metric/row).</p> <p>I need to write a code such that I can sort certain bound-together rows, not the entire sheet. For instance, I might need to sort the 28th-35th row. This will be passed to this program by the variable <strong>inRange</strong>. However, I also want to be able to configure the sort priority and how each column gets sorted <strong>everytime I run the macro.</strong> (note that a row can only change position as a whole, individual cells within the row cannot change position)</p> <p>I have the following Global arrays which describe the headers:</p> <p><strong>headRow</strong> - contains an array of the header names as strings listed in order of the column</p> <p><strong>prLst</strong> - contains an array of integers (input as strings) that determine priority of each of the headers. The position of the integers in the array is the column number which they describe.</p> <p><strong>colIsString</strong> - contains Boolean determining if items listed in a given column are strings or integers. True = string, False = Integer. Again, position position of booleans in the array is the column number which they describe.</p> <p><strong>sortOrder</strong> - containing booleans specifying orientation of sort. "True" - ascending. "False" - descending. Yet again, position position of booleans in the array is the column number which they describe.***</p> <p><strong>With the data in the arrays already etablished,</strong> I have the following code to feed these arrays to .sort:</p> <pre><code>Dim numHdrs, finalNumHdrs, count As Integer, newArray As Variant For numHdrs = 1 To UBound(headRow) If colIsString(numHdrs) = True Then sortOrder(numHdrs) = xlAscending ElseIf colIsString(numHdrs) = False Then sortOrder(numHdrs) = xlDescending End If Next numHdrs newArray = CombineArrays(headRow, prLst, sortOrder) For finalNumHdrs = 1 To UBound(headRow) If headRow(finalNumHdrs) And prLst(finalNumHdrs) And sortOrder(finalNumHdrs) &lt;&gt; "N/A" Then 'ActiveSheet.Sort.SortFields.Add Key(headRow(finalNumHdrs)):= finalNumHdrs 'ActiveSheet.Sort.SortFields.Add Order(finalNumHdrs):=sortOrder(finalNumHdrs) End If Next finalNumHdrs With ActiveSheet.Sort .SetRange inRange .Apply End With </code></pre> <p>I am having trouble adding the sort fields appropriately, using the data I have in the array:</p> <pre><code>'ActiveSheet.Sort.SortFields.Add Key(headRow(finalNumHdrs)):= finalNumHdrs 'ActiveSheet.Sort.SortFields.Add Order(finalNumHdrs):=sortOrder(finalNumHdrs) </code></pre> <p>This is obviously not correct. So I created a function to concatenate <strong>headRow, prLst,</strong> and <strong>sortOrder</strong> to make it easier to feed into .sort:</p> <pre><code>Function CombineArrays(arr1 As Variant, arr2 As Variant, arr3 As Variant) Dim arr4 As Variant ReDim arr4(1 To UBound(arr2), 1 To 2) Dim i, j As Integer For i = 0 To UBound(arr1) arr4(arr2(i), 1) = arr1(i) arr4(arr2(i), 2) = arr3(i) Next i CombineArrays = arr4 End Function </code></pre> <p>Since arr2 is specifying the sort priority of a given column, I need to feed it to .sort in such a way that arr2 specifies the key number. As in if arr2 = 3, key3:= arr4(3,1), order3:= arr4(3,2). I am assuming that the order in which I elements to arr4 will not matter. (as in if I add arr4(4,1) before arr4(3,1) they will still be listed in the appropriate order)</p> <p>This boils down to two questions:</p> <p><strong>For excel's .sort function: How do I add keys when I do not know how many keys (headers) a given sheet is going to have?</strong></p> <p><strong>Am I doing this correctly?</strong></p> <p>*** Note I would have used collection objects had I heard about them earlier. However, since I am new to VBA, this is how I established the program. It would be too difficult given the code I have already written to go back and change all the arrays to collection objects.</p> <p>NOTE: This is part of a larger program, whose description can be found here: <a href="https://stackoverflow.com/questions/6671273/excel-vba-macro-sorting-with-groups-or-linked-rows">Sorting Groups of Rows Excel VBA Macro</a></p>
    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.
 

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