+ Reply to Thread
Results 1 to 4 of 4

Iterate over two index variables to reorder an array

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Iterate over two index variables to reorder an array

    Hi,

    I have to reorder my array and paste it to a sheet. I am trying to do this with a for loop that iterates over two index variables.

    Sub example()
    
    Dim MyArray() As Variant
    Dim FirstElement As Range
    Dim i As Integer
    Dim j As Integer
    
    
    Set FirstElement = Range("J1")
    MyArray = Range(Cells(2, 2), Cells(17, 2)).Value
    
    For i = 1 To 16
        For j = 1 To 16 Step 2
            FirstElement.Offset(j, 0).Value = MyArray(i, 1)
        Next j
    Next i
    
    
    End Sub
    But I dont get it to work. I have attached example of my array. That better explains what I am trying to accomplish.
    Attached Files Attached Files

  2. #2
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Iterate over two index variables to reorder an array

    This works, but far from elegant and not very efficient.

    I would like to reorder arrays in VBA and than paste to sheet.

    k = 1
    l = 2
    
    For i = 1 To 8
        FirstElement.Offset(k, 0) = MyArray(i, 1)
        k = k + 2
    Next i
    
    For j = 9 To 16
        FirstElement.Offset(l, 0) = MyArray(j, 1)
        l = l + 2
    Next j

  3. #3
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Iterate over two index variables to reorder an array

    Hi,

    Here is one method
    Dim MyArray() As Variant, tempArray()
    Dim FirstElement As Range
    Dim i As Integer
    Dim j As Integer
    
    
    Set FirstElement = Range("J2")
    MyArray = Range(Cells(2, 2), Cells(17, 2)).Value
    ReDim tempArray(1 To UBound(MyArray), 1 To 1)
    
    For i = 1 To 8
        tempArray((i - 1) * 2 + 1, 1) = MyArray(i, 1)
        tempArray((i - 1) * 2 + 2, 1) = MyArray(i + 8, 1)
    Next i
    FirstElement.Resize(UBound(tempArray), 1).Value2 = tempArray
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  4. #4
    Forum Contributor
    Join Date
    01-28-2013
    Location
    sweden
    MS-Off Ver
    Excel 2010
    Posts
    108

    Re: Iterate over two index variables to reorder an array

    beutiful, more efficient. thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 0
    Last Post: 01-27-2016, 11:19 AM
  2. Replies: 5
    Last Post: 12-02-2015, 05:23 AM
  3. Iterate through Worksheets Building an Array
    By MaterialMaster in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-12-2014, 04:34 PM
  4. [SOLVED] Iterate Array Names with Integers
    By AlvaroSiza in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 07-06-2014, 10:32 AM
  5. [SOLVED] finding an output from 5 variables in an array using exterior input user variables
    By Allsort in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-09-2013, 11:16 AM
  6. Reorder - Array
    By -EE- in forum Excel General
    Replies: 1
    Last Post: 01-08-2012, 01:57 AM
  7. How can I randomly reorder my values in my array?
    By shapper in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-15-2008, 05:24 AM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1