Variant Array - Last Row and Placement

GeeWhiz7

Board Regular
Joined
Nov 22, 2021
Messages
214
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi folks,
Yet another thing I seem to be stuck on. Code and mini-sheet below to illustrate what I'm trying to do.

Issue:
I have an input array that is not always a fixed size. I would like to do some match on the items in that array and write out the result below the previous array with a space between.
This works fine if I calculate the last row and use it +2 to position the first output array by itself. But, I'd like to keep doing this with multiple arrays and then my approach starts to mess up and I can't quite spot why.

Goal:
Be able to do as many new arrays as I want, each stacked neatly below the previous with a space or two while still allowing for dynamic sizes of the input array.

Example Desired Result just using formulas in sheet
Book2
BCDEFGHIJKLMNO
1ABCDE
210.716.07.219.311.3Original Inputs
34.314.15.015.819.1These data array can change size by column and row
43.19.66.72.64.8
58.710.915.76.616.1
614.66.07.13.510.3
714.912.23.97.39.9
811.915.812.22.319.4
97.98.711.619.27.7
1010.27.87.612.715.6
11
12ABCDE1st Calculation
1321.332.014.438.722.7Would like to have this calculation with one row space from original
148.628.110.031.738.1=Original Array X 2
156.219.213.45.29.5
1617.321.831.513.332.2
1729.212.014.17.120.6
1829.724.37.814.719.7
1923.931.624.34.638.7
2015.817.423.138.415.5
2120.315.715.125.331.1
22
23ABCDE2nd Calculation
2432.048.021.658.034.0Same with each subsequent calculation would like one row space
2512.942.215.047.557.2=Original Array X 3
269.228.920.17.814.3
2726.032.747.219.948.4
2843.818.121.210.630.9
2944.636.511.622.029.6
3035.847.436.56.858.1
3123.726.234.757.723.2
3230.523.522.738.046.7
33
34ABCDE2nd Calculation
3542.764.028.877.345.3Same with each subsequent calculation would like one row space
3617.256.220.063.376.3=Original Array X 4
3712.338.526.710.419.0
3834.743.662.926.664.5
3958.424.128.214.141.2
4059.548.615.529.439.4
4147.863.248.79.177.4
4231.734.946.276.931.0
4340.731.330.350.662.3
Sheet1
Cell Formulas
RangeFormula
B13:F21B13=B2*2
B24:F32B24=B2*3
B35:F43B35=B2*4


My current code to replace the formula approach...although I think I've messed up more than just the spacing at this point with numCalcs.
VBA Code:
Option Explicit
'*************************************************************************
'*Calc from Originals                                  *
'*************************************************************************
Sub FirstCalc()
    
    Dim inarr, outaar As Variant
    Dim lastCol, lastRow, i, j, numCalcs As Long
    Dim row, col As Range

    Dim iSh As Worksheet
    Dim oSh As Worksheet
    
    Set iSh = Worksheets("Sheet2")
    Set oSh = Worksheets("Sheet2")
    
    With iSh
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(Rows.Count, 2).End(xlUp).row
        inarr = Range(Cells(2, 2), Cells(lastRow, lastCol))
    End With
    
    ReDim outarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
    
        For numCalcs = 2 To 5
            
            For i = 1 To UBound(inarr, 1)
                For j = 1 To UBound(inarr, 2)
                    outarr(i, j) = inarr(i, j) * numCalcs
                Next j
            Next i
            
                With oSh
                    .Range("B" & lastRow + 2).Resize(UBound(outarr, 1), UBound(outarr, 2)).Value = outarr
                End With
            
            numCalcs = numCalcs + 1
            
        Next numCalcs
    
End Sub
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Hi folks,
Yet another thing I seem to be stuck on. Code and mini-sheet below to illustrate what I'm trying to do.

Issue:
I have an input array that is not always a fixed size. I would like to do some match on the items in that array and write out the result below the previous array with a space between.
This works fine if I calculate the last row and use it +2 to position the first output array by itself. But, I'd like to keep doing this with multiple arrays and then my approach starts to mess up and I can't quite spot why.

Goal:
Be able to do as many new arrays as I want, each stacked neatly below the previous with a space or two while still allowing for dynamic sizes of the input array.

Example Desired Result just using formulas in sheet
Book2
BCDEFGHIJKLMNO
1ABCDE
210.716.07.219.311.3Original Inputs
34.314.15.015.819.1These data array can change size by column and row
43.19.66.72.64.8
58.710.915.76.616.1
614.66.07.13.510.3
714.912.23.97.39.9
811.915.812.22.319.4
97.98.711.619.27.7
1010.27.87.612.715.6
11
12ABCDE1st Calculation
1321.332.014.438.722.7Would like to have this calculation with one row space from original
148.628.110.031.738.1=Original Array X 2
156.219.213.45.29.5
1617.321.831.513.332.2
1729.212.014.17.120.6
1829.724.37.814.719.7
1923.931.624.34.638.7
2015.817.423.138.415.5
2120.315.715.125.331.1
22
23ABCDE2nd Calculation
2432.048.021.658.034.0Same with each subsequent calculation would like one row space
2512.942.215.047.557.2=Original Array X 3
269.228.920.17.814.3
2726.032.747.219.948.4
2843.818.121.210.630.9
2944.636.511.622.029.6
3035.847.436.56.858.1
3123.726.234.757.723.2
3230.523.522.738.046.7
33
34ABCDE2nd Calculation
3542.764.028.877.345.3Same with each subsequent calculation would like one row space
3617.256.220.063.376.3=Original Array X 4
3712.338.526.710.419.0
3834.743.662.926.664.5
3958.424.128.214.141.2
4059.548.615.529.439.4
4147.863.248.79.177.4
4231.734.946.276.931.0
4340.731.330.350.662.3
Sheet1
Cell Formulas
RangeFormula
B13:F21B13=B2*2
B24:F32B24=B2*3
B35:F43B35=B2*4


My current code to replace the formula approach...although I think I've messed up more than just the spacing at this point with numCalcs.
VBA Code:
Option Explicit
'*************************************************************************
'*Calc from Originals                                  *
'*************************************************************************
Sub FirstCalc()
   
    Dim inarr, outaar As Variant
    Dim lastCol, lastRow, i, j, numCalcs As Long
    Dim row, col As Range

    Dim iSh As Worksheet
    Dim oSh As Worksheet
   
    Set iSh = Worksheets("Sheet2")
    Set oSh = Worksheets("Sheet2")
   
    With iSh
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(Rows.Count, 2).End(xlUp).row
        inarr = Range(Cells(2, 2), Cells(lastRow, lastCol))
    End With
   
    ReDim outarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
   
        For numCalcs = 2 To 5
           
            For i = 1 To UBound(inarr, 1)
                For j = 1 To UBound(inarr, 2)
                    outarr(i, j) = inarr(i, j) * numCalcs
                Next j
            Next i
           
                With oSh
                    .Range("B" & lastRow + 2).Resize(UBound(outarr, 1), UBound(outarr, 2)).Value = outarr
                End With
           
            numCalcs = numCalcs + 1
           
        Next numCalcs
   
End Sub
This change makes it work. Turns out I needed to re assign the lastRow variable after each array iteration.
VBA Code:
Option Explicit
'*************************************************************************
'*Calc from Originals                                  *
'*************************************************************************
Sub FirstCalc()
    
    Dim inarr, outaar As Variant
    Dim lastCol, lastRow, i, j, numCalcs As Long
    Dim row, col As Range

    Dim iSh As Worksheet
    Dim oSh As Worksheet
    
    Set iSh = Worksheets("Sheet2")
    Set oSh = Worksheets("Sheet2")
    
    With iSh
        lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
        lastRow = .Cells(Rows.Count, 2).End(xlUp).row
        inarr = Range(Cells(2, 2), Cells(lastRow, lastCol))
    End With
    
     ReDim outarr(1 To UBound(inarr, 1), 1 To UBound(inarr, 2))
     For numCalcs = 2 To 5
     
            For i = 1 To UBound(inarr, 1)
                For j = 1 To UBound(inarr, 2)
                    outarr(i, j) = inarr(i, j) * numCalcs
                Next j
            Next i
            
            With oSh
                .Range("B" & lastRow + 2).Resize(UBound(outarr, 1), UBound(outarr, 2)).Value = outarr
            End With
            
            lastRow = iSh.Cells(Rows.Count, 2).End(xlUp).row  '##Added this line so after each new array is written out the next "last Row" would be updated.

        Next numCalcs
    
End Sub
 
Upvote 0
Solution
i don't understand what you want
VBA Code:
Sub multiply()

     Set iSh = Worksheets("Sheet2")
     With iSh
          lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
          lastRow = .Cells(Rows.Count, 2).End(xlUp).row
          Set c = .Range(.Cells(2, 2), .Cells(lastRow, lastCol))
     End With

     For i = 2 To 5
          With c.Offset((c.Rows.Count + 2) * (i - 1))
               .Formula = "=" & c.Cells(1, 1).Address(0, 0) & "*" & i
               .Value = .Value
          End With
     Next

End Sub
 
Upvote 0
i don't understand what you want
VBA Code:
Sub multiply()

     Set iSh = Worksheets("Sheet2")
     With iSh
          lastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
          lastRow = .Cells(Rows.Count, 2).End(xlUp).row
          Set c = .Range(.Cells(2, 2), .Cells(lastRow, lastCol))
     End With

     For i = 2 To 5
          With c.Offset((c.Rows.Count + 2) * (i - 1))
               .Formula = "=" & c.Cells(1, 1).Address(0, 0) & "*" & i
               .Value = .Value
          End With
     Next

End Sub
Sometimes neither do I! In this case I was wanting each successive array that was coming out of the For loop to be placed directly beneath the previous one with one blank row in between. I finally figured out that I was not updating the lastRow variable properly after each loop iteration so it was doing weird things by using the previous value. Actually there were two errors, that one and I was double incrementing my numCalcs variable by using numCalcs +1 and Next numCalcs. Still getting used to VBA...

Your code works too so I will bank that as well for another use. thank you for taking a look!
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top