Sort multidimensional array in VBScript - arrays

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.

Related

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

Resorting 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

send array to csv file - simulation too slow

I automated a montecarlo simulation for stock and option prices that runs 10 thousand paths for 606 trading days. All and all it works fine, the problem is that is slow. The slow part of the code is when it has to write an array of say 400 x 10000 to the spreadsheet, then export it into a new CSV file and save it.
It does this for a 25 option portfolio (it takes 1 minute per option.) So overall it takes 25 minutes, which is unacceptable. How can I write directly to CSV the arrays of data that I am filling with the simulation? I need to keep all stock prices, option prices and total portfolio value. Below is the code that I have created for this.
Sub Background_simulation()
0 Application.ScreenUpdating = False
1 Windows("Option Portfolio Simulation v7.xlsm").Activate
2 Sheets("Export").Activate
3 Dim Arr() As Variant
4 ArrStock = Range("F11:NTU616")
6 ArrOption = Range("F621:NTU1226")
7 Sheets("Export").Activate
8 Dim DestinationStock As Range
9 Set DestinationStock = Range("F11")
10 Dim DestinationOption As Range
11 Set DestinationOption = Range("F621")
12 Dim RAND(1 To 606, 1 To 10000) As Variant
13 Paths = Range("D6").Value
15 Dim Option_Paths(1 To 606, 1 To 10000) As Variant
16 Dim St_Paths(1 To 606, 1 To 10000) As Variant
17 Dim Options_Total(1 To 606, 1 To 10000) As Variant
18 FromOption = Range("D7").Value
19 UpToOption = Range("D8").Value
20 Sheets("Portfolio").Select
21 Let MaxExpirationOfSet = Application.WorksheetFunction.Max(Range("BC2:BC26").Value)
22 Sheets("Export").Select
23 For a = 2 To 606
24 For b = 1 To Paths
25 Randomize
26 RAND(a, b) = Application.WorksheetFunction.NormInv(RND(), 0, 1)
27 Next
28 Next
31 For Option_Nr = FromOption To UpToOption
32 Sheets("Export").Select
33 Range("F11:NTU616").Select
34 Selection.ClearContents
35 Range("F621:NTU1226").Select
36 Selection.ClearContents
38 Range("B1").Select
39 ActiveCell.FormulaR1C1 = Option_Nr
42 Calls = Range("L3").Value
43 If Calls = 0 Then GoTo 2010
44 St = Range("G2").Value
45 Rf = Range("G3").Value
46 Sigma = Range("G4").Value
47 dt = 1 / 250
48 mrn = Rf - (1 / 2) * Sigma ^ 2
49 X = Range("L5").Value
50 Expiration_Date = Range("D4").Value
120 For b = 1 To Paths
130 RAND(1, b) = 1
140 St_Paths(1, b) = St
150 Option_Paths(1, b) = Application.WorksheetFunction.Max(St - X, 0) * Calls
160 Options_Total(1, b) = Options_Total(1, b) + Option_Paths(1, b)
170 Next
190 For a = 2 To Expiration_Date
200 For b = 1 To Paths
600 St_Paths(a, b) = St_Paths(a - 1, b) * Exp(mrn * dt + Sigma * (dt) ^ (1 / 2) * RAND(a, b))
700 Option_Paths(a, b) = Application.WorksheetFunction.Max(St_Paths(a, b) - X, 0) * Calls
701 Options_Total(a, b) = Options_Total(a, b) + Option_Paths(a, b)
703 Next
704 Next
705 For a = (Expiration_Date + 1) To MaxExpirationOfSet
706 For b = 1 To Paths
707 St_Paths(a, b) = St_Paths(a - 1, b)
710 Option_Paths(a, b) = Option_Paths(a - 1, b)
711 Options_Total(a, b) = Options_Total(a - 1, b)
712 Next
730 Next
740 Sheets("Export").Select
750 Range("F11:NTU616").Select
760 Selection.ClearContents
770 Range("F621:NTU1226").Select
780 Selection.ClearContents
790 Range("A1").Select
800 DestinationStock.Resize(UBound(ArrStock, 1), UBound(ArrStock, 2)).Value = St_Paths
810 DestinationOption.Resize(UBound(ArrOption, 1), UBound(ArrOption, 2)).Value = Option_Paths
830 Sheets("Export").Copy
840 Range("F620").Select
845 ActiveWorkbook.BreakLink Name:= _
"C:\Users\pmesples\Desktop\Options\Option Portfolio Simulation v7.xlsm", Type _
:=xlExcelLinks
850 ChDir "C:\Users\pmesples\Desktop\Options"
860 ActiveWorkbook.SaveAs Filename:="C:\Users\pmesples\Desktop\Options\" & Option_Nr & ".csv", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
900 ActiveWindow.Close
1000 Sheets("Export").Select
1001 Range("F" & 620 + MaxExpirationOfSet & ":NTU" & 620 + MaxExpirationOfSet).Select
1002 Selection.Copy
1003 Sheets("Results").Select
1004 Cells(5, Option_Nr + 3).Select
1005 Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
1006 Sheets("Export").Select
1010 Range("F11").Select
1020 Range(Selection, Selection.End(xlToRight)).Select
1030 Range(Selection, Selection.End(xlDown)).Select
1040 Selection.ClearContents
1050 Selection.End(xlDown).Select
1060 Range("F621").Select
1080 Range(Selection, Selection.End(xlDown)).Select
1090 Range(Selection, Selection.End(xlToRight)).Select
2000 Selection.ClearContents
2005 Range("A1").Select
2010 Next
2015 Range("B1").Select
2016 ActiveCell.FormulaR1C1 = 26
2020 DestinationOption.Resize(UBound(ArrOption, 1), UBound(ArrOption, 2)).Value = Options_Total
2030 Sheets("Export").Copy
2040 Range("F620").Select
2045 ActiveWorkbook.BreakLink Name:= _
"C:\Users\pmesples\Desktop\Options\Option Portfolio Simulation v7.xlsm", Type _
:=xlExcelLinks
2050 ChDir "C:\Users\pmesples\Desktop\Options"
2060 ActiveWorkbook.SaveAs Filename:="C:\Users\pmesples\Desktop\Options\26.csv", _
FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
2100 ActiveWindow.Close
2110 Sheets("Export").Select
2215 Range("B1").Select
2120 ActiveCell.FormulaR1C1 = 1
End Sub
You could probably get rid of selects and selections to speed up the macro, like:
32 Sheets("Export").Select
33 Range("F11:NTU616").Select
34 Selection.ClearContents
could be replaces by
32 Sheets("Export").Range("F11:NTU616").ClearContents
but you probably lost most of valuable seconds while saving file, as you already have noticed. Indeed, you could try export to file, see my subroutine:
Private Sub PrintToCSV(sFileName As String, rng As Range)
Dim TextFile As Integer
Dim FilePath As String
Dim FileContent As String
Dim cl As Range
FilePath = ThisWorkbook.Path & "\something something\" & sFileName & ".csv"
TextFile = FreeFile
Open FilePath For Output As TextFile
For Each cl In rng
Print #TextFile, cl.Value
Next cl
Close TextFile
End Sub
But, generally speaking, VBA is scripting language, so it is slow. I am not sure if writing to file will be faster than saving as, but you may try.

VB.Net - Populating Arrays

So, I have this code below that populates an array.
Module Module1
Sub Main()
Dim n(10) As Integer
Dim i, j As Integer
For i = 0 to 10 step 3
n(i) = i + 100
Next i
For j = 0 to 10
Console.WriteLine("Element({0}) = {1}", j, n(j))
Next j
Console.ReadLine()
End Sub
End Module
I' am wondering why does the output of the code
is
Element({0}) = 100
Element({1}) = 0
Element({2}) = 0
Element({3}) = 103
Element({4}) = 0
Element({5}) = 0
Element({6}) = 106
Element({7}) = 0
Element({8}) = 0
Element({9}) = 109
What I intented to is this
Element({0}) = 100
Element({1}) = 103
Element({2}) = 106
Element({3}) = 109
Element({4}) = 112
Element({5}) = 115
Element({6}) = 118
Element({7}) = 121
Element({8}) = 124
Element({9}) = 127
Thank you in advance!!
Instead of using Step 3, move 3 to next line:
For i = 0 to 10
n(i) = 3 * i + 100
Next i
If anything is unclear, feel free to ask me.
(Edit: I see this was answered in comments, I did not read them before.)

How to sum values in array from different columns + EXCEL VBA

I need to calculate this in excel vba ,using array loop only :
round 0 round 1
9 28
65 84
28 47
84 103
41 60
66 85
115 134
I need to sum values in round 0 in loop so the sum result (408) must be divided by 7 , if not I WANT to sum one value from the round 1 (in this case 84 instead of 65 ) to the rest of values in round 0 so the sum result can divided by 7 . There will be so many round up to 7 . I need VBA code to accomplish this..
Notes :
round 0 and round 1 all in one two-dimensional array
My Question is : is there a way to sum values from different columns in multi-dimensional array ??
there is an image attached .
I appreciate any help or idea .
Thanks in advance
Excel VBA Array Model:
http://im56.gulfup.com/8rDErI.png
Here an example file contains macro "Question1.xlsm"
http://www.gulfup.com/?TKAAYM
Notes : click the link under the big green down arrow to download the file.
UPDATE :
here another macro to the file "Question1.xlsm" :
Sub A1()
Dim arrTemp1() As Integer
Dim sum1 As Integer
arrblkTable1 = Sheets("Sheet1").Range("blkTable1").Value
ReDim Preserve arrTemp1(0 To 1, 1 To 7)
For a = 0 To 1
sum1 = 0
For c = 1 To 7
arrTemp1(a, c) = arrblkTable1(c, 1) + (a * 19)
text6 = text6 & arrTemp1(a, c) & vbCrLf
Worksheets("TEST3").Cells(a + 1, c).Value = arrTemp1(a, c)
sum1 = sum1 + arrTemp1(a, c)
Next c
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1
Else
MsgBox "No " & sum1
End If
Next a
MsgBox text6
End Sub
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
UPDATE : here a new update to the previous macro :
Sub A1()
Dim arrTemp1(), arrTemp2(), arrSUMs() As Integer
Dim sum1 As Integer
arrblkTable1 = Sheets("Sheet1").Range("blkTable1").Value
arrblkTable2 = Sheets("Sheet1").Range("blkTable2").Value
'-------------------------------- arrTemp1 ------------------------------
ReDim Preserve arrTemp1(0 To 1, 1 To 7)
For a = 0 To 1
sum1 = 0
For c = 1 To 7
arrTemp1(a, c) = arrblkTable1(c, 1) + (a * 19)
text6 = text6 & arrTemp1(a, c) & vbCrLf
Worksheets("TEST3").Cells(a + 1, c).Value = arrTemp1(a, c)
sum1 = sum1 + arrTemp1(a, c)
Next c
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1
Else
MsgBox "No " & sum1
For c = 1 To 7
sum1 = sum1 - arrTemp1(a, c)
arrTemp1(a, c) = arrblkTable1(c, 1) + ((a + 1) * 19)
sum1 = sum1 + arrTemp1(a, c)
If XLMod(sum1, 7) = 0 Then
MsgBox "Yes " & sum1 & " " & arrTemp1(a, c)
End If
Next c
End If
Next a
For x = 0 To UBound(arrTemp1)
For y = 1 To UBound(arrTemp1)
text7 = text7 & arrTemp1(x, y) & vbCrLf
Next y
Next x
MsgBox text7
End Sub
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
I need now to put each sum1 in one array , how I can do that ??
If I understood it correctly you want something similar to this:
Sub p()
v = Range("A2:A8")
v1 = Sheets("Sheet1").Range("B2:B8")
s = Application.WorksheetFunction.Sum(v)
b = False
Count = 0
For i = 1 To 7
temp = v
temp(i, 1) = v1(i, 1)
s = Application.WorksheetFunction.Sum(temp)
b = s Mod 7 = 0
If b = True Then
Count = Count + 1
End If
Next
MsgBox Count
End Sub
It may help tremendously if you can give more detail about what problem you're trying to solve, instead of focusing on how to solve it this way. There is a possibility that there's another way of doing it that hasn't occurred to you that will be much simpler.
This isn't an answer. Yet. But it will take more space than is allowed in a comment to ensure we've got this right.
For your sample data:
round 0 round 1
9 28
65 84
28 47
84 103
41 60
66 85
115 134
You want to:
Sum all the values in Round 0 (9 + 65 + 28 + 84 + 41 + 66 + 115) = 408
Take that sum (408) mod 7 and see if the result is 0
408 / 7 = 58.28, so (408 mod 7) <> 0
If the result isn't 0 (as in this case)
Start substituting numbers from round 1 for numbers in Round 0
Sum (28 + 65 + 28 + 84 + 41 + 66 + 115) = 427
427 / 7 = 61 (427 mod 7) = 0
This is now your valid result set.
Had the first number in Round 1 been 29
Sum (29 + 65 + 28 + 84 + 41 + 66 + 115) = 428
428 / 7 = 61.14 so (428 mod 7) <> 0
Substitute the next number from round 1 for the next number from round 0
Sum (9 + 84 + 28 + 84 + 41 + 66 + 115) = 427
This is now your valid result set.
Is that the logic you're after?
What happens if you get to the end of round 1 and you don't find a total that (mod 7 = 0)?

Resources