+ Reply to Thread
Results 1 to 4 of 4

Adding non contiguous rows of data to an array by means of areas

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-28-2013
    Location
    Here
    MS-Off Ver
    Excel 2010
    Posts
    119

    Adding non contiguous rows of data to an array by means of areas

    Hello,

    I have a data set where users can select entire non contiguous in order to print the selected data on a special form, so I am trying to use 'areas' to join the non contiguous row selections into an array.

    I have the following code that works but I'm getting the feeling there must be a better way.
    Sub test()
    
    Dim myRange As Range
    Dim myAreasCount As Long
    Dim myArray() As Variant
    Dim i As Long
    Dim j As Long
    Dim k As Long
    Dim iCount1 As Long
    Dim iCount2 As Long
    Dim myRow As Long
    Dim myAreaRowsCount As Long
    
    Selection.EntireRow.Select
    
    myAreasCount = Selection.Areas.Count
    
    iCount1 = 1
    
    For i = 1 To myAreasCount
        myAreaRowsCount = Selection.Areas(i).Rows.Count
        For j = 1 To myAreaRowsCount
            iCount1 = iCount1 + 1
        Next
    Next
    
    ReDim myArray(iCount1 - 1, 7)
    
    iCount2 = 1
    For i = 1 To myAreasCount
        myAreaRowsCount = Selection.Areas(i).Rows.Count
        For j = 1 To myAreaRowsCount
            For k = 1 To 7
                myArray(iCount2, k) = Selection.Areas(i).Rows(1).Cells(j, k)
                Debug.Print myArray(iCount2, k)
                Debug.Print myArray(1, 1)
            Next
            iCount2 = iCount2 + 1
        Next
    Next
    End Sub
    I'm using the first For loop to determine the total number of rows selected and thus the necessary size of the array, keeping in mind that each area may have contiguous rows within it.

    The second For loop is pretty much a repeat of the first For loop and adds the data of the selected rows to the array one cell at a time.

    There's got to be a better way!


    TV

  2. #2
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Adding non contiguous rows of data to an array by means of areas

    Hi TV,
    try this
    Sub test2()
    Dim myArray
    With Sheets("Sheet2")
        .UsedRange.Clear
        Intersect(Selection.EntireRow, Range("A:G")).Copy .Range("A1")
        myArray = .Range("A1").CurrentRegion.Value
    End With
    MsgBox UBound(myArray) & "  " & UBound(myArray, 2)
    'Range("J1").Resize(UBound(myArray), UBound(myArray, 2)).Value = myArray
    End Sub

  3. #3
    Forum Contributor
    Join Date
    11-28-2013
    Location
    Here
    MS-Off Ver
    Excel 2010
    Posts
    119

    Re: Adding non contiguous rows of data to an array by means of areas

    Nilem, thanks for you response. I also considered temporarily moving the data to another sheet, but I was looking for a solution that did not rely on that.

    TV

  4. #4
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Adding non contiguous rows of data to an array by means of areas

    then maybe so
    Sub test3()
    Dim r As Range, x, y(), i&, j&, k&
    With Intersect(Selection.EntireRow, Range("A:G"))
        ReDim y(1 To ActiveSheet.UsedRange.Rows.Count, 1 To .Columns.Count)
        For Each r In .Areas
            x = r.CurrentRegion.Value
            For i = 1 To UBound(x)
                k = k + 1
                For j = 1 To UBound(x, 2)
                    y(k, j) = x(i, j)
                Next j
            Next i
        Next r
    End With
    Range("J1").Resize(k, UBound(y, 2)).Value = y()
    End Sub

+ 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: 2
    Last Post: 11-04-2012, 12:18 AM
  2. [SOLVED] Count rows between two areas in a collection/selection of multiple areas
    By JTwrk in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-02-2012, 10:38 AM
  3. Copy and Paste an array (contiguous & non contiguous ranges)
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-09-2010, 09:17 AM
  4. Can i print non-contiguous areas on one page??
    By yiota in forum Excel General
    Replies: 2
    Last Post: 03-27-2006, 09:55 AM
  5. Can i print non-contiguous areas on one page
    By yiota in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 03-27-2006, 07:30 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