Hello,
I am looking to increase my code's efficiency to maximize the speed, to minimize the calculation time. I have two ideas to increase the efficiency/decrease calc time.
1) Currently I am using a Do Until Loop that references various worksheets in various workbooks based on workbook names and worksheet names in the cell range A1:F11 of wb"Cntrl".ws"Cntrl2." These worksheets are set as array1 through array6. These arrays are summed in my For Next Loop within the Do Until Loop. Each of the Do Until Loops changes the values within each of the arrays. So each of the Do Until Loops changes the range that each of the arrays are set to. If it is possible, I think it would be faster to set the arrays from the beginning and instead of referencing the workbook names and worksheet names in the cell range A1:F11 I could reference the combination of arrays to sum. In other words does VBA allow
this sort of referencing. So for example in my For Next Loop I invision something like this;
arrSum(x, y) = RngRef1(x, y) + RngRef2(x, y) + RngRef3(x, y) + RngRef4(x, y) + RngRef5(x, y) + RngRef6(x, y)
2) I am passing the If arrSum = 6, the countPos values, to ws"Pos1." I think would be faster if I could pass the values to another array, for example arrPos(x, y). How can I do this? I tried this to no success;
For x = 1 To UBound(arr1, 1)
countPos = 0
For y = 1 To UBound(arr1, 2)
arrSum(x, y) = arr1(x, y) + arr2(x, y) + arr3(x, y) + arr4(x, y) + arr5(x, y) + arr6(x, y)
If arrSum(x, y) = "6" Then countPos = countPos + 1
Next y
arrPos(x, y) = countPos
Next x
Here is my complete code without the above possible changes.
Thanks for any input!!
Zacharia
I am looking to increase my code's efficiency to maximize the speed, to minimize the calculation time. I have two ideas to increase the efficiency/decrease calc time.
1) Currently I am using a Do Until Loop that references various worksheets in various workbooks based on workbook names and worksheet names in the cell range A1:F11 of wb"Cntrl".ws"Cntrl2." These worksheets are set as array1 through array6. These arrays are summed in my For Next Loop within the Do Until Loop. Each of the Do Until Loops changes the values within each of the arrays. So each of the Do Until Loops changes the range that each of the arrays are set to. If it is possible, I think it would be faster to set the arrays from the beginning and instead of referencing the workbook names and worksheet names in the cell range A1:F11 I could reference the combination of arrays to sum. In other words does VBA allow
this sort of referencing. So for example in my For Next Loop I invision something like this;
arrSum(x, y) = RngRef1(x, y) + RngRef2(x, y) + RngRef3(x, y) + RngRef4(x, y) + RngRef5(x, y) + RngRef6(x, y)
2) I am passing the If arrSum = 6, the countPos values, to ws"Pos1." I think would be faster if I could pass the values to another array, for example arrPos(x, y). How can I do this? I tried this to no success;
For x = 1 To UBound(arr1, 1)
countPos = 0
For y = 1 To UBound(arr1, 2)
arrSum(x, y) = arr1(x, y) + arr2(x, y) + arr3(x, y) + arr4(x, y) + arr5(x, y) + arr6(x, y)
If arrSum(x, y) = "6" Then countPos = countPos + 1
Next y
arrPos(x, y) = countPos
Next x
Here is my complete code without the above possible changes.
Thanks for any input!!
Zacharia
Code:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' To Define and Set for the Do Until Loops '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ws0 As Worksheet
Set ws0 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2")
Dim rng1 As Range, rng2 As Range, rng3 As Range
Dim rng4 As Range, rng5 As Range, rng6 As Range
Dim rngSum As Range
Set rngSum = Workbooks("Cntrl.xlsm").Sheets("Cntrl3").Range("B5:BDD587")
Dim countPos1 As Range, countNeg1 As Range
Set countPos1 = Workbooks("Cntrl.xlsm").Sheets("Pos1").Range("A1")
Set countNeg1 = Workbooks("Cntrl.xlsm").Sheets("Neg1").Range("A1")
Dim arr1 As Variant, arr2 As Variant, arr3 As Variant
Dim arr4 As Variant, arr5 As Variant, arr6 As Variant
Dim arrSum As Variant
Dim RngRef1 As Range, RngRef2 As Range, RngRef3 As Range
Dim RngRef4 As Range, RngRef5 As Range, RngRef6 As Range
Set RngRef1 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("A2")
Set RngRef2 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("B2")
Set RngRef3 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("C2")
Set RngRef4 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("D2")
Set RngRef5 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("E2")
Set RngRef6 = Workbooks("Cntrl.xlsm").Sheets("Cntrl2").Range("F2")
iOffset = 0
jOffset = 0
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' To Loop Through Each Combination '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Do Until IsEmpty(RngRef1)
sTmp1 = RngRef1.Value: sTmp2 = RngRef2.Value: sTmp3 = RngRef3.Value
sTmp4 = RngRef4.Value: sTmp5 = RngRef5.Value: sTmp6 = RngRef6.Value
Set rng1 = Workbooks(ws0.Range("A1").Value).Sheets(sTmp1).Range("B5:BDD587")
Set rng2 = Workbooks(ws0.Range("B1").Value).Sheets(sTmp2).Range("B5:BDD587")
Set rng3 = Workbooks(ws0.Range("C1").Value).Sheets(sTmp3).Range("B5:BDD587")
Set rng4 = Workbooks(ws0.Range("D1").Value).Sheets(sTmp4).Range("B5:BDD587")
Set rng5 = Workbooks(ws0.Range("E1").Value).Sheets(sTmp5).Range("B5:BDD587")
Set rng6 = Workbooks(ws0.Range("F1").Value).Sheets(sTmp6).Range("B5:BDD587")
arr1 = rng1.Value: arr2 = rng2.Value: arr3 = rng3.Value
arr4 = rng4.Value: arr5 = rng5.Value: arr6 = rng6.Value
arrSum = rngSum.Value
Workbooks("Cntrl.xlsm").Sheets("Pos1").Activate
countPos1.Select
For x = 1 To UBound(arr1, 1)
countPos = 0
For y = 1 To UBound(arr1, 2)
arrSum(x, y) = arr1(x, y) + arr2(x, y) + arr3(x, y) + arr4(x, y) + arr5(x, y) + arr6(x, y)
If arrSum(x, y) = "6" Then countPos = countPos + 1
Next y
ActiveCell.Range("A1").Value = countPos
ActiveCell.Offset(1, 0).Range("A1").Select
Next x
Workbooks("Cntrl.xlsm").Sheets("Neg1").Activate
countNeg1.Select
For x = 1 To UBound(arr1, 1)
countNeg = 0
For y = 1 To UBound(arr1, 2)
If arrSum(x, y) = "-6" Then countNeg = countNeg + 1
Next y
ActiveCell.Range("A1").Value = countNeg
ActiveCell.Offset(1, 0).Range("A1").Select
Next x
iOffset = iOffset + 1
jOffset = jOffset + 1
Set RngRef1 = ws0.Range("A2").Offset(iOffset, 0)
Set RngRef2 = ws0.Range("B2").Offset(iOffset, 0)
Set RngRef3 = ws0.Range("C2").Offset(iOffset, 0)
Set RngRef4 = ws0.Range("D2").Offset(iOffset, 0)
Set RngRef5 = ws0.Range("E2").Offset(iOffset, 0)
Set RngRef6 = ws0.Range("F2").Offset(iOffset, 0)
Set countPos1 = Sheets("Pos1").Range("A1").Offset(0, jOffset)
Set countNeg1 = Sheets("Neg1").Range("A1").Offset(0, jOffset)
Loop
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' To Create Analytical Data '
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Workbooks("Cntrl.xlsm").Sheets("Percentile").Activate
Range("A1").Select
ActiveCell.FormulaR1C1 = "=IF(ISERROR(Pos1!RC/(Pos1!RC+Neg1!RC)),0,Pos1!RC/(Pos1!RC+Neg1!RC))"
Selection.Copy
ActiveCell.Range("A1:P583").Select
ActiveSheet.Paste