Filling a single dimension array, using values from a two dimensional array

Vantom

Board Regular
Joined
Feb 28, 2014
Messages
65
I have these numbers that I put into Array1. I then want to put 16 '11s' into Array2, then add 3 '20s', then 40 '9s' etc. And at last paste Array2 into C1.

14uuhl3.jpg


This code gives me 91 '11s' in column C.
Code:
Sub Test()
    Dim Number1 As Integer, Number2 As Integer, i As Integer, j As Integer, Start As Integer, intStop As Integer, Counter As Integer, Array2(), vArray1(): vArray1 = Range("A1:B5").Value
        For i = LBound(vArray1) To UBound(vArray1)
            Number1 = vArray1(i, 1)
            Number2 = vArray1(i, 2)
            intStop = intStop + Number2
            For j = Start To intStop
                ReDim Preserve Array2(Counter)
                Array2(Counter) = Number1
                Counter = Counter + 1
            Next j
            Start = intStop + 1
        Next i
        Range("C1").Resize(UBound(Array2)) = Array2
End Sub
 
Not sure what your question is.
This code puts 91 "11"s in column C

Code:
Sub Resize()
[C1].Resize(91).Value = "11"
End Sub
 
Upvote 0
This should do what you want
Code:
Sub test()
    Dim Array1 As Variant
    Dim Array2() As Variant
    Dim Pointer As Long, i As Long, Size As Long
    
    Array1 = Range("A1:B3")
    
    Size = 0
    For i = 1 To 3
        ReDim Preserve Array2(1 To Size + Array1(i, 2))
        Size = UBound(Array2, 1)
        Do
            Pointer = Pointer + 1
            Array2(Pointer) = Array1(i, 1)
        Loop Until Pointer = Size
    Next i
    
    Range("C1").Resize(UBound(Array2), 1).Value = Application.Transpose(Array2)
End Sub
 
Upvote 0
This code gives me 91 '11s' in column C.

The key problem is: Array2 must be two-dimensional. So you cannot use Redim Preserve. Try the following:
Rich (BB code):
Sub doit()
    Dim v1 As Variant, v2 As Variant
    Dim i As Long, j As Long, n1 As Long, n2 As Long
    v1 = Range("a1:b5")  ' lbound(v1,1)=1 always
    n1 = UBound(v1, 1)   ' or n1 = 5
    For i = 1 To n1: n2 = n2 + v1(i, 2): Next
    ReDim v2(1 To n2, 1 To 1) As Long
    j = 1
    For i = 1 To n1
        For j = j To j + v1(i, 2) - 1
            v2(j, 1) = v1(i, 1)
        Next j
    Next i
    Range("c1:c" & n2) = v2
End Sub
 
Upvote 0
Try this:
Code:
Sub ResizeMe()
Dim i As Integer
Dim LastrowC As Long
Dim LastrowA As Long
LastrowC = Cells(Rows.Count, "C").End(xlUp).Row
LastrowA = Cells(Rows.Count, "A").End(xlUp).Row
    For i = 1 To LastrowA
        Cells(LastrowC, 3).Resize(Cells(i, 2).Value).Value = Cells(i, 1).Value
        LastrowC = Cells(Rows.Count, "C").End(xlUp).Row + 1
    Next
End Sub
 
Upvote 0
One more for you to try (notice, no loops)...
Code:
[SIZE=1]Sub Test()
  Dim Total As Long, LastRow As Long
  LastRow = Cells(Rows.Count, "A").End(xlUp).Row
  Total = WorksheetFunction.Sum(Range("B1:B" & LastRow))
  Range("C1:C" & Total) = Application.Transpose(Split(Application.Trim(Join(Evaluate(Replace( _
                          "TRANSPOSE(IF(B1:B#="""","""",REPT(a1:A#&"" "",B1:B#)))", "#", LastRow))))))
End Sub[/SIZE]
 
Upvote 0

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