Results 1 to 3 of 3

Thread: Create list with arrays

  1. #1
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13

    Create list with arrays

    Hi,

    I have a list in column "A" data based on whole number and this number should I add a few lines numbered.
    Use this routine and I was wondering if the syntax is correct

    In column "A", i for example these values​​:

    12
    15
    7
    11
    128

    I get the list in column "F" has:

    Repeats 12 times the value = 1
    Repeats 15 times the value = 2
    Repeats 07 times the value = 3
    Repeats 11 times the value = 4
    Repeats 128 times the value = 5

    Code:
    Sub Lista_Con_Array()
        Dim r   As Long
        Dim i   As Long
        Dim Riga As Variant
        Dim Data As Variant
        Dim Num As Integer
        i = 0
        r = Range("A" & Rows.Count).End(xlUp).Row
        Data = Range("A1:A" & r)            'Original List
        Dim varData As Variant
        ReDim varData(1)
        For Each Riga In Data
            Num = Num + 1
            For NumI = 1 To Riga
                ReDim Preserve varData(i)
                varData(i) = Num
                i = i + 1
            Next
        Next
        Range("F1:F" & UBound(varData) + 1) = Application.Transpose(varData)
    End Sub
    I would like if possible to get a better performance than having to operate with 1000 values ​​other than repeating. I refer to a range extended data Excel 2007.

    Thanks in advance

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,122
    Rep Power
    10
    Hi

    how about this ?

    Code:
    Sub kTest()
        
        Dim k(), ka, i As Long, j As Long, n As Long
        
        ka = Range("a1:a" & Range("a" & Rows.Count).End(3).Row)
        ReDim k(1 To UBound(ka, 1) * Application.Max(ka), 1 To 1)
        
        For i = 1 To UBound(ka, 1)
            j = 1
            Do While j <= ka(i, 1)
                n = n + 1
                k(n, 1) = i
                j = j + 1
            Loop
        Next
        [f2].Resize(n) = k
        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Senior Member
    Join Date
    Oct 2011
    Posts
    135
    Rep Power
    13
    Hi,

    Thank you for answer.

    The code that is definitely improved, I was not continually called Redim Preserve ...

    I have to make a comparison using two different array before knowing the value *Application.Max(ka)
    I hope I do not see how to work find it difficult, now I try to insert and include this cycle.

Similar Threads

  1. Filter In Arrays
    By Transformer in forum Tips, Tricks & Downloads (No Questions)
    Replies: 0
    Last Post: 05-28-2013, 08:40 PM
  2. Replies: 14
    Last Post: 01-26-2013, 04:58 AM
  3. Replies: 5
    Last Post: 12-05-2012, 03:01 AM
  4. Transposing arrays to the worksheet
    By Rasm in forum Excel Help
    Replies: 1
    Last Post: 11-01-2011, 12:02 PM
  5. Making a Histogram Chart Using Arrays
    By Rasm in forum Download Center
    Replies: 2
    Last Post: 04-05-2011, 07:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •