Resorting Arrays - arrays

I receive data that I need to output in a string of the format:
123 - A, B, C
234 - A
345 - B
567 - B, C
789 - C
The data I get is sorted by letter (A, B, or C) and then given to me by number. So I have three dynamic arrays like:
ArrayA(1) = 123
ArrayA(2) = 234
ArrayB(1) = 345
ArrayB(2) = 123
ArrayB(3) = 567
ArrayC(1) = 123
ArrayC(2) = 789
ArrayC(3) = 567
Notice that the index corresponding to a particular 3-digit number in a given array does not necessarily correspond to the same 3-digit number, e.g. ArrayA(1)=123=ArrayB(2).
The arrays are of arbitrary length (there could be any number of numbers in A, B, or C) but there are only three arrays.
This makes it easy to output something such as:
123 - A
234 - A
345 - B
123 - B
567 - B
123 - C
789 - C
567 - C
but this is NOT my desired result.
I need it in this format:
123 - A, B, C
234 - A
345 - B
567 - B, C
789 - C
To play with this problem directly, here's some code that generates the "easy" string:
Dim ArrayA(2), ArrayB(3), ArrayC(3) As Integer, Output As String
ArrayA(1) = 123
ArrayA(2) = 234
ArrayB(1) = 345
ArrayB(2) = 123
ArrayB(3) = 567
ArrayC(1) = 123
ArrayC(2) = 789
ArrayC(3) = 567
For i=1 to 2
Output = Output & ArrayA(i) & " - A" & vbNewLine
Next i
For i=1 to 3
Output = Output & ArrayB(i) & " - B" & vbNewLine
Next i
For i=1 to 3
Output = Output & ArrayC(i) & " - C" & vbNewLine
Next i
MsgBox(Output)
As mentioned above, I'm hoping to move the format such that it is organized by three-digit number, rather than by letter.
My best attempt at a solution would be to try to write the data into an excel sheet, sort it appropriately, and pull it back into VBA, which seems unnecessarily ugly. For example:
For i=1 to Len(ArrayA)+Len(ArrayB)+Len(ArrayC)
If i < Len(ArrayA) Then
Range("A:"&i).Value = ArrayA(i)
Range("B:"&i).Value = "A,"
End If
If i > Len(ArrayA) And i <= Len(ArrayA) + Len(ArrayB) Then
Range("A:"&i).Value = ArrayB(i)
Range("B:"&i).Value = Range("B:"&i).Value & "B,"
End If
if i >= Len(ArrayA)+Len(ArrayB) Then
Range("A:"&i).Value = ArrayC(i)
Range("B:"&i).Value = Range("B:"&i).Value & "C,"
Next i
Then I could sort this, search out duplicates, and properly combine them, and finally get to the correct output of:
123 - A, B, C
234 - A
345 - B
567 - B, C
789 - C

Try the following:
Sub PopulateFromArrays()
Call WriteArray(ArrayA, "A")
Call WriteArray(ArrayB, "B")
Call WriteArray(ArrayC, "C")
End Sub
Function WriteArray(MyArray, MyString)
i = 2
For j = LBound(MyArray) To UBound(MyArray)
ValueFound = False
k = ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To k
If Range("A" & i).Value = MyArray(j) Then
Range("B" & i).Value = Range("B" & i).Value & ", " & MyString
ValueFound = True
Exit For
End If
Next i
If ValueFound = False Then
Range("A" & k + 1).Value = MyArray(j)
Range("B" & k + 1).Value = MyString
End If
Next j
End Function
FYI for testing I populated the arrays with the following:
ArrayA = Array(123, 456, 789)
ArrayB = Array(123, 567, 912)
ArrayC = Array(456, 789, 567)
And the result was:

Seems like a good use case for dictionaries:
ArrayA(1) = 123
ArrayA(2) = 234
ArrayB(1) = 345
ArrayB(2) = 123
ArrayB(3) = 567
ArrayC(1) = 123
ArrayC(2) = 789
ArrayC(3) = 567
'...
Dim e, dictArrays, dictOut, k
Set dictArrays = Createobject("scripting.dictionary")
Set dictOut = Createobject("scripting.dictionary")
dictArrays.Add "A", ArrayA
dictArrays.Add "B", ArrayB
dictArrays.Add "C", ArrayC
For Each k in dictArrays.Keys
For Each e in dictArrays(k)
If dictOut.Exists(e) then
dictOut(e) = dictOut(e) & "," & k
Else
dictOut.Add e, k
End If
Next e
Next k
'output the result
For Each k in dictOut.Keys
Debug.Print k, dictOut(k)
Next k

Related

vba loop through array, store values to arrayi

I have some data, stored in arrays like
Dim arrA, arrB, arrC, arrAi, arrBi
Dim i as integer, x as integer
for i = 1 to 100
if cells(i,1).value = "criteria" then ' just add value to array when it meets some criteria
x = x + 1
arrA(x) = cells(i,1).value
arrB(x) = cells(i,2).value
arrC(x) = cells(i,3).value
end if
next i
redim preserve arrA(1 to x)
redim preserve arrB(1 to x)
redim preserve arrC(1 to x)
And the data looks like
arrA: 26.1 40.2 80.3 26.0 41.3 78.7 25.8 40.8 80.0
arrB: 10 11 10 66 67 64 32 32 33
arrC: 1 2 3 1 2 3 1 2 3
Since the values in arrA 26.1, 26.0, 25.8 (position 1, 4, 7) belong to group 1 (referencing to values in arrC at same position), I would like to store 26.1 26.0 25.8 to arrAi and 10 66 32 to arrBi for subsequent calculations.
How can I loop through the 3 arrays and store values to another array as described above?
Thanks in advance.
Try the next way, please:
Sub handleArraysFromArrays()
'your existing code...
'but you fistly must declare
Dim arrA(1 To 100), arrB(1 To 100), arrC(1 To 100)
'....
'your existing code
'...
Dim k As Long, kk As Long
ReDim arrAi(1 To UBound(arrA))
ReDim arrBi(1 To UBound(arrA))
For i = 1 To UBound(arrC)
If arrC(i, 1) = 1 Then k = k + 1: arrAi(k, 1) = arrA(i, 1)
If arrC(i, 1) = 2 Then kk = kk + 1: arrBi(kk, 1) = arrA(i, 1)
Next i
ReDim Preserve arrAi(1 To k): ReDim Preserve arrBi(1 To kk)
Debug.Print UBound(arrAi), UBound(arrBi)
End Sub

VBA Count multiple duplicates in array

I have the same question as here: VBA counting multiple duplicates in array , but I haven't found an answer and with my reputation can't leave comment there.
I have an array with 150 numbers which could contain repetitive numbers from 1 to 50. Not always there are all 50 numbers in the array. Example of output of what I need:
- 10 times: 1, 2;
- 20 times: 3, 4 etc;
- 0 times: 5, 6, 7 etc.
I need to count how many combinations of duplicate numbers it has and what numbers are in those combinations including zero occurrence - which numbers are not in the array.
On mentioned above post there are solutions - but only when you know how many combinations of duplicates there are - and I don't know it - there could be 1 (all 150 numbers are equal) - ... - 20 ... up to 50 combinations if it contains all numbers from 1 to 50 three times each.
Appreciate any help and advice how to store output - finally it should be written to worksheet in the above mentioned format: [times] - [numbers] (here could be a string, example "5 - 6 - 7").
Here is what I've made for 5 combinations, but do 50 cases and then check 50 strings if they are empty or contain something to write to output is not very good option...
For i = 1 To totalNumbers 'my numbers from 1 to 50 or any other number
numberCount = 0
For j = 0 To UBound(friendsArray) 'my array of any size (in question said 150)
If i = friendsArray(j) Then
numberCount = numberCount + 1
End If
Next j
Select Case numberCount
Case 0
zeroString = zeroString & i & " - "
Case 1
oneString = oneString & i & " - "
Case 2
twoString = twoString & i & " - "
Case 3
threeString = threeString & i & " - "
Case 4
fourString = fourString & i & " - "
Case 5
fiveString = fiveString & i & " - "
Case Else
End Select
Next i
I have found possible option using Collection (but have got an headache with getting keys of collection...):
Dim col As New Collection
For i = 1 To totalNumbers
numberCount = 0
For j = 0 To UBound(friendsArray)
If i = friendsArray(j) Then
numberCount = numberCount + 1
End If
Next j
colValue = CStr(numberCount) & "> " & CStr(i) & " - " 'store current combination [key] and number as String
If IsMissing(col, CStr(numberCount)) Then
col.Add colValue, CStr(numberCount) 'if current combination of duplicates [key] is missing - add it to collection
Else 'if current combination [key] is already here - update the value [item]
oldValue = col(CStr(numberCount))
newValue = Replace(oldValue & colValue, CStr(numberCount) & "> ", "") 'delete combinations count
newValue = CStr(numberCount) & "> " & newValue
col.Remove CStr(numberCount) 'delete old value
col.Add newValue, CStr(numberCount) 'write new value with the same key
End If
Next i
For i = 1 To col.Count
Debug.Print col(i)
Next i
and IsMissing function (found here How to check the key is exists in collection or not)
Private Function IsMissing(col As Collection, field As String)
On Error GoTo IsMissingError
Dim val As Variant
val = col(field)
IsMissing = False
Exit Function
IsMissingError:
IsMissing = True
End Function
Output is like this [times]> [numbers]:
(array of 570 numbers)
114> 2 -
5> 6 -
17> 10 -
10> 3 - 8 - 19 - 21 - 30 -
6> 1 - 29 - 33 -
8> 5 - 9 - 13 - 23 - 25 - 28 - 37 - 40 -
4> 12 - 16 - 41 -
13> 43 -
12> 15 - 20 - 22 - 27 - 36 - 38 - 42 - 44 - 45 - 46 -
9> 4 - 7 - 11 - 14 - 34 - 47 - 48 -
7> 17 - 18 - 35 - 49 -
11> 24 - 26 - 31 - 32 - 39 - 50 -
Creating new array and count the number is more simple.
Sub test()
Dim friendsArray(0 To 50)
Dim vTable()
Dim iMax As Long
Dim a As Variant, b As Variant
Dim i As Long, s As Integer, n As Long
dim c As Integer
'Create Sample array to Test
n = UBound(friendsArray)
For i = 0 To n
friendsArray(i) = WorksheetFunction.RandBetween(0, 50)
Next i
'Your code
iMax = WorksheetFunction.Max(friendsArray)
ReDim vTable(0 To iMax) 'create new Array to count
For i = 0 To n
c = friendsArray(i)
vTable(c) = vTable(c) + 1
Next i
Dim dic As Object
Set dic = CreateObject("Scripting.Dictionary")
For i = 0 To iMax
If IsEmpty(vTable(i)) Then
s = 0
Else
s = vTable(i)
End If
If dic.Exists(s) Then
dic.Item(s) = dic.Item(s) & " - " & i
Else
dic.Add s, i
End If
Next i
a = dic.Keys
b = dic.Items
Range("a1").CurrentRegion.Clear
Range("B:B").NumberFormatLocal = "#"
Range("a1").Resize(UBound(a) + 1) = WorksheetFunction.Transpose(a)
Range("b1").Resize(UBound(b) + 1) = WorksheetFunction.Transpose(b)
Range("a1").CurrentRegion.Sort Range("a1"), xlAscending
End Sub

How to add a counter column to existing matrix in VBA?

How to get a new matrix in VBA with a counter value in the first "column". Suppose we have a VBA matrix which values we get from cells. The value of A1 cell is simply "A1".
Dim matrix As Variant
matrix = Range("A1:C5").value
Input matrix:
+----+----+----+
| A1 | B1 | C1 |
+----+----+----+
| A2 | B2 | C2 |
+----+----+----+
| A3 | B3 | C3 |
+----+----+----+
| A4 | B4 | C4 |
+----+----+----+
| A5 | B5 | C5 |
+----+----+----+
I would like to get new matrix with the counter value in the first column of VBA matrix.
Here are desired results:
+----+----+----+----+
| 1 | A1 | B1 | C1 |
+----+----+----+----+
| 2 | A2 | B2 | C2 |
+----+----+----+----+
| 3 | A3 | B3 | C3 |
+----+----+----+----+
| 4 | A4 | B4 | C4 |
+----+----+----+----+
| 5 | A5 | B5 | C5 |
+----+----+----+----+
One way to do it is looping. Would there be any other more elegant way to do it? We are dealing here with large data sets, so please mind the performance.
If your main concern is the performance, then use Redim Preserve to add a new column at the end and use the OS API to shift each column directly in the memory:
Private Declare PtrSafe Sub MemCpy Lib "kernel32" Alias "RtlMoveMemory" ( _
ByRef dst As Any, ByRef src As Any, ByVal size As LongPtr)
Private Declare PtrSafe Sub MemClr Lib "kernel32" Alias "RtlZeroMemory" ( _
ByRef src As Any, ByVal size As LongPtr)
Sub AddIndexColumn()
Dim arr(), r&, c&
arr = [A1:F1000000].Value
' add a column at the end
ReDim Preserve arr(LBound(arr) To UBound(arr), LBound(arr, 2) To UBound(arr, 2) + 1)
' shift the columns by 1 to the right
For c = UBound(arr, 2) - 1 To LBound(arr, 2) Step -1
MemCpy arr(LBound(arr), c + 1), arr(LBound(arr), c), (UBound(arr) - LBound(arr) + 1) * 16
Next
MemClr arr(LBound(arr), LBound(arr, 2)), (UBound(arr) - LBound(arr) + 1) * 16
' add an index in the first column
For r = LBound(arr) To UBound(arr)
arr(r, LBound(arr, 2)) = r
Next
End Sub
Method 1
This method inserts cells to the left of the range and set the new cells formula to calculate the counter =ROWS($A$1:$A5). Note: this pattern is also used to calculate a running total.
Usage
InsertCounter Worksheets("Sheet1").Range("A1:C5")
Sub InsertCounter(Target As Range)
Dim counterCells As Range
Target.Columns(1).Insert Shift:=xlToRight
Set counterCells = Target.Columns(1).Offset(0, -1)
counterCells.Formula = "=Rows(" & counterCells.Cells(1, 1).Address(True, True) & ":" & counterCells.Cells(1, 1).Address(False, True) & ")"
End Sub
Method 2
This method copies the Ranges' Values into an array, creates a new array with 1 extra column and then copies the data and a counter over to the new array. The difference in this Method is that it doesn't insert any cells.
Usage
AddCounterToMatrix Worksheets("Sheet1").Range("A1:C5")
Sub AddCounterToMatrix(Target As Range)
Dim x As Long, y As Long
Dim Matrix1 As Variant, NewMatrix1 As Variant
Matrix1 = Target.Value
ReDim NewMatrix1(LBound(Matrix1) To UBound(Matrix1), LBound(Matrix1, 2) To UBound(Matrix1, 2) + 1)
For x = LBound(Matrix1) To UBound(Matrix1)
NewMatrix1(x, 1) = x - LBound(Matrix1) + 1
For y = LBound(Matrix1, 2) To UBound(Matrix1, 2)
NewMatrix1(x, y + 1) = Matrix1(x, y)
Next
Next
Target.Resize(UBound(NewMatrix1) - LBound(Matrix1) + 1, UBound(NewMatrix1, 2) - LBound(NewMatrix1, 2) + 1).Value = NewMatrix1
End Sub
using a Dynamic variant is fast.
Sub test()
Dim matrix As Variant, newMatrix()
Dim i As Long, n As Long, c As Long, j As Long
matrix = Range("A1:C5").Value
n = UBound(matrix, 1)
c = UBound(matrix, 2)
ReDim newMatrix(1 To n, 1 To c + 1)
For i = 1 To n
newMatrix(i, 1) = i
For j = 2 To c + 1
newMatrix(i, j) = matrix(i, j - 1)
Next j
Next i
Range("a1").Resize(n, c + 1) = newMatrix
End Sub
excel based solution are ok for u?
Columns("A:A").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1") = "1"
Range("A2") = "2"
Range("A1:A2").Select
Selection.AutoFill Destination:=Range("A1:A5")
Dim matrix As Variant
matrix = Range("A1:D5").Value
Why not a compromise between household remedies and pure array scripting by inserting a temporary column and doing the rest within the array's first column.
Code
Option Explicit
Public Sub test_CounterCol2()
Dim matrix As Variant, newMatrix()
Dim i As Long, n As Long
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("CounterCol") ' <== user defined sheet
' a) insert column temporarily]
ws.Columns("A:A").Insert Shift:=xlToRight
' b) get values
matrix = ws.Range("A1:D5").value
' c) only loop within array counter column
n = UBound(matrix, 1)
For i = 1 To n
matrix(i, 1) = i
Next i
' d) delete temporary insertion
ws.Columns("A:A").Delete (xlShiftToLeft)
End Sub
Additional note: Maybe you can find something via API (CopyMemory).

Copy-paste loop with skipped values in VBA

I am fairly new to code-writing in general and VBA in particular.
I have tried to write a fairly simple macro that copies values from one cell to another on a daily basis, however I am wondering if there is a way to have fewer variables for the loop counters, in other words, can a loop counter skip certain values?
Private Sub YesButton_Click()
Dim z As Integer
Dim z1 As Integer
Dim z2 As Integer
Dim z3 As Integer
Dim z4 As Integer
Dim z5 As Integer
Dim z6 As Integer
Dim z7 As Integer
Dim z8 As Integer
Dim z9 As Integer
Dim z10 As Integer
Dim z11 As Integer
Dim z12 As Integer
Dim z13 As Integer
Application.Calculation = xlCalculationManual 'turn off autocalc to speed up copy paste process
For z = 5 To 16
Sheet68.Range("H" & z) = Sheet68.Range("D" & z).Value
Next z
For z1 = 21 To 33
Sheet68.Range("H" & z1) = Sheet68.Range("D" & z1).Value
Next z1
For z2 = 38 To 51
Sheet68.Range("H" & z2) = Sheet68.Range("D" & z2).Value
Next z2
For z3 = 73 To 86
Sheet68.Range("H" & z3) = Sheet68.Range("D" & z3).Value
Next z3
For z4 = 92 To 94
Sheet68.Range("G" & z4) = Sheet68.Range("D" & z4).Value
Next z4
For z5 = 100 To 110
Sheet68.Range("G" & z5) = Sheet68.Range("D" & z5).Value
Next z5
For z6 = 115 To 126
Sheet68.Range("G" & z6) = Sheet68.Range("D" & z6).Value
Next z6
For z7 = 131 To 142
Sheet68.Range("G" & z7) = Sheet68.Range("D" & z7).Value
Next z7
For z8 = 149 To 151
Sheet68.Range("G" & z8) = Sheet68.Range("D" & z8).Value
Next z8
For z11 = 157 To 164
Sheet68.Range("G" & z11) = Sheet68.Range("D" & z11).Value
Next z11
For z9 = 169 To 175
Sheet68.Range("G" & z9) = Sheet68.Range("D" & z9).Value
Next z9
For z10 = 180 To 186
Sheet68.Range("G" & z10) = Sheet68.Range("D" & z10).Value
Next z10
For z12 = 191 To 203
Sheet68.Range("H" & z12) = Sheet68.Range("D" & z12).Value
Next z12
Application.Calculation = xlCalculationAutomatic 'turn autocalc back on
Unload Me
End Sub
Thanks in advance
Here's an example of how you could re-think your code. You will clearly need to readapt the sample to your own data.
Declare a vector of ranges
The size of it, as many as your intervals are (I counted 14 in your case, but I might be wrong).
Dim ranges(1 To 5)
Dim j As Integer '<-- counter of the ranges
Dim k As Long '<-- counter of your loop
Define your ranges
Here you define, as strings, your specific ranges. In my example I've put random numbers, but in your case should be "5-16", "21-23" etc.
ranges(1) = "1-2"
ranges(2) = "5-10"
ranges(3) = "15-20"
ranges(4) = "25-30"
ranges(5) = "35-40"
Nest two loops
The outside one will loop through the ranges, the inside one will split the ranges and use the lower and upper bounds to loop through your cells
For j = 1 To 5
For k = Split(ranges(j), "-")(0) To Split(ranges(j), "-")(1)
'your code here
'test it with a msgbox:
MsgBox "k is now equal to " & k
Next k
Next j
To sum up
Your code should look something like this:
Dim ranges(1 To 14) '<-- not sure, please check it first!
Dim j As Integer, k As Long
ranges(1) = "5-16"
ranges(2) = "21-33"
'....
ranges(14) = "191-203"
For j = 1 To 14
For k = Split(ranges(j),"-")(0) To Split(ranges(j),"-")(1)
Sheet68.Range("G" & k) = Sheet68.Range("D" & k).Value
Next k
Next j
You can just declare 1 int and use it for every loop. U give it a new value at the start of the loop anyway!
So:
Dim z As Integer
For z = 0 To 10 Step 1
//Do something
Next
For z = 11 To 21 Step 1
//Do something
Next
Sub YesButton_Click()
Dim rngTemp As Range
For Each rngTemp In Range("H5:H16, H21:H33, H38:H51, H73:H86, H191:H203")
rngTemp.Value = rngTemp.Offset(, -4).Value
Next rngTemp
For Each rngTemp In Range("G92:G94, G100:G110, G115:G126, G131:G142, G149:G151, G157:G164, G169:G175, G180:G186")
rngTemp.Value = rngTemp.Offset(, -3).Value
Next rngTemp
End Sub

Sort multidimensional array in VBScript

How can I "Sort" the multidimensional arrays based on the hole size parameter please?
eg: A simple example would be (Loaded from Text file):
> Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
> Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
> Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55
must then result in :
> Liv1.HoleSize[0] = 14 Liv1.HoleX[0] = 750 Liv1.HoleY[0] = 0
> Liv1.HoleSize[1] = 22 Liv1.HoleX[1] = 250 Liv1.HoleY[1] = -55
> Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55
As VBScript has no native sort, you'll have to roll your own sort, or to get a little help from friends.
If your task is to sort your input file (verbatim as given) to an output file in the specified order, sort.exe is your friend:
Dim sIn : sIn = "..\data\in00.txt"
WScript.Echo readAllFromFile(sIn)
WScript.Echo "-----------"
Dim sCmd : sCmd = "sort /+19 " & qq(resolvePath(sIn))
Dim aRet : aRet = goWSLib.Run(sCmd)
If aRet(0) Then
' handle error
Else
WScript.Echo aRet(2)
End If
output:
================================================================
Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55
-----------
Liv1.HoleSize[1] = 14 Liv1.HoleX[1] = 750 Liv1.HoleY[1] = 0
Liv1.HoleSize[0] = 22 Liv1.HoleX[0] = 250 Liv1.HoleY[0] = -55
Liv1.HoleSize[2] = 22 Liv1.HoleX[2] = 900 Liv1.HoleY[2] = -55
================================================================
If something like that solves your problem, just say so, and we can talk the support code in the library functions.
If, however, you have (to) parse(d) the input file into a two-dimensional array, the best friend you can get is a disconnectes ADODB recordset:
Dim aData : aData = Split(Join(Array( _
"22 250 -55" _
, "14 750 0" _
, "22 900 -55" _
, "11 222 333" _
)))
Dim afData(3, 2)
Dim nRows : nRows = UBound(afData, 1)
Dim nCols : nCols = UBound(afData, 2)
Dim i, r, c
For i = 0 TO UBound(aData)
r = i \ nRows
c = i Mod (nCols + 1)
afData(r, c) = aData(i)
' WScript.Echo i, r, c, aData(i)
Next
For r = 0 To nRows
For c = 0 To nCols
WScript.StdOut.Write vbTab & afData(r, c)
Next
WScript.Echo
Next
WScript.Echo "-----------------"
Dim oRS : Set oRS = CreateObject("ADODB.Recordset")
For c = 0 To nCols
oRS.Fields.Append "Fld" & c, adInteger
Next
oRS.Open
For r = 0 To nRows
oRS.AddNew
For c = 0 To nCols
oRS.Fields(c).value = afData(r, c)
Next
oRS.UpDate
Next
oRS.Sort = "Fld0"
WScript.Echo oRS.GetString(adClipString, , vbTab, vbCrLf)
WScript.Echo "-----------------"
oRS.Sort = "Fld2"
WScript.Echo oRS.GetString(adClipString, , vbTab, vbCrLf)
output:
========================================
22 250 -55
14 750 0
22 900 -55
11 222 333
-----------------
11 222 333
14 750 0
22 250 -55
22 900 -55
-----------------
22 250 -55
22 900 -55
14 750 0
11 222 333
========================================
Again: if that looks promising, we can discuss how to adapt/streamline this proof of concept code to your needs.

Resources