Hi,
I've got a novice question on arrays.
Here is a snippet of code that is embedded in a greater procedure I have. This part of the code does the following:
But try as I might I can't figure out how to write it out the way I want.
Goal: Have the write out look like the mini-sheet below (right side)
I've got a novice question on arrays.
Here is a snippet of code that is embedded in a greater procedure I have. This part of the code does the following:
- Loop calls FirstCalc procedure, passing an input data array (inarr), output array (outarr) and the Loop x value (x)
- Calls FirstCalc procedure which returns its output (outarr)
- Assigns outarr to another array (firstarr) to hold it to use in SecondCalc procedure
- Calls SecondCalc procedure, passing original input data array (inarr), the array output from the SecondCalc (now in firstarr) and Loop x value (x)
But try as I might I can't figure out how to write it out the way I want.
Goal: Have the write out look like the mini-sheet below (right side)
Book2 | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | |||
1 | Input | A | B | Input | A | B | ||||
2 | 1 | 15.58 | 14.02 | 1 | 15.58 | 14.02 | ||||
3 | 2 | 17.29 | 14.18 | 2 | 17.29 | 14.18 | ||||
4 | 3 | 14.50 | 14.39 | 3 | 14.50 | 14.39 | ||||
5 | 4 | 12.14 | 19.84 | 4 | 12.14 | 19.84 | ||||
6 | 5 | 12.60 | 16.50 | 5 | 12.60 | 16.50 | ||||
7 | ||||||||||
8 | ||||||||||
9 | First Calc x=2 | 31.17 | 28.04 | First Calc x=2 | 31.17 | 28.04 | ||||
10 | 34.59 | 28.36 | 34.59 | 28.36 | ||||||
11 | 29.00 | 28.77 | 29.00 | 28.77 | ||||||
12 | 24.27 | 39.69 | 24.27 | 39.69 | ||||||
13 | 25.21 | 33.00 | 25.21 | 33.00 | ||||||
14 | ||||||||||
15 | ||||||||||
16 | Second Calc x =2 | 485.6583 | 393.0168 | First Calc x=4 | 62.33191 | 56.07258 | ||||
17 | 598.1501 | 402.1612 | 69.17515 | 56.72116 | ||||||
18 | 420.6 | 413.9062 | 58.0069 | 57.54346 | ||||||
19 | 294.5934 | 787.627 | 48.54634 | 79.37894 | ||||||
20 | 317.7357 | 544.492 | 50.41712 | 65.99951 | ||||||
21 | ||||||||||
22 | ||||||||||
23 | First Calc x=4 | 62.33191 | 56.07258 | Second Calc x =2 | 485.6583 | 393.0168 | ||||
24 | 69.17515 | 56.72116 | 598.1501 | 402.1612 | ||||||
25 | 58.0069 | 57.54346 | 420.6 | 413.9062 | ||||||
26 | 48.54634 | 79.37894 | 294.5934 | 787.627 | ||||||
27 | 50.41712 | 65.99951 | 317.7357 | 544.492 | ||||||
28 | ||||||||||
29 | ||||||||||
30 | Second Calc x=4 | 971.3166 | 786.0337 | Second Calc x=4 | 971.3166 | 786.0337 | ||||
31 | 1196.3 | 804.3225 | 1196.3 | 804.3225 | ||||||
32 | 841.2 | 827.8124 | 841.2 | 827.8124 | ||||||
33 | 589.1867 | 1575.254 | 589.1867 | 1575.254 | ||||||
34 | 635.4715 | 1088.984 | 635.4715 | 1088.984 | ||||||
35 | ||||||||||
36 | This is what current code writes out | This is how I want it… | ||||||||
Sheet1 |
VBA Code:
Option Explicit
'*************************************************************************
'* Main - assigns input data, calls FirstCalc Procedure
'*************************************************************************
Sub Main()
Dim inarr, outarr, firstarr As Variant
Dim lc, lr, x As Long
Dim wSh As Worksheet
Set wSh = Worksheets("Sheet1")
With wSh
lc = .Cells(1, Columns.Count).End(xlToLeft).Column
lr = .Cells(Rows.Count, 1).End(xlUp).Row
inarr = Range(Cells(2, 2), Cells(lr, lc))
End With
ReDim outarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
ReDim firstarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
ReDim outarr2(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
For x = 2 To 4 Step 2
Call FirstCalc(inarr, outarr, x)
firstarr = outarr
Call SecondCalc(inarr, firstarr, outarr2, x)
With wSh
.Range("B" & lr + 3).Resize(UBound(outarr, 1), _
UBound(outarr, 2)).Value = outarr
End With
lr = wSh.Cells(Rows.Count, 2).End(xlUp).Row
ReDim outarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
With wSh
.Range("B" & lr + 3).Resize(UBound(outarr2, 1), _
UBound(outarr2, 2)).Value = outarr2
End With
lr = wSh.Cells(Rows.Count, 2).End(xlUp).Row
ReDim outarr2(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
Next x