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.
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.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
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
Bookmarks