VBA Array - is there a quick way to export an entire row of a 2 dimensional array to a worksheet range?

Mike UK

New Member
Joined
Dec 14, 2011
Messages
41
Hi,
I have a two dimensional array. The code checks a row element (column 9).

If it contains "Include" then I want the entire array row on one worksheet.
If it doesn't contain include then the entire row is placed on another worksheet.
It then goes through each row in the array checking column 9 outputting as defined.

Is there a quick way to output the entire row rather than cell by cell? The two variants of code below don't work.

Sub ExportArray()

Dim OutputArray As Variant
Dim BDMRow As Integer
Dim NatRow As Integer
Dim i As Integer

RowCount = Sheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
OutputArray = Sheets("Combined").Range("A1:O" & RowCount)

BDMRow = 2
NatRow = 2

For i = 2 To UBound(OutputArray, 1)

If OutputArray(i, 9) = "Include" Then
Sheets("BDM").Range("A" & i & ":O" & i) = OutputArray.Range(Cells(i, 1), Cells(i, 15))
BDMRow = BDMRow + 1
Else
Sheets("Nationals").Range("A" & i & ":O" & i) = OutputArray.Cells(i, 1).Value
NatRow = NatRow + 1
End If

Next


End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Have you tried using Application.Index?
Code:
Sub ExportArray()
Dim OutputArray As Variant
Dim BDMRow As Long
Dim NatRow As Long
Dim i As Long

    RowCount = Sheets("Combined").Range("A" & Rows.Count).End(xlUp).Row
    OutputArray = Sheets("Combined").Range("A1:O" & RowCount)
    
    BDMRow = 2
    NatRow = 2
    
    For i = 2 To UBound(OutputArray, 1)
    
    If OutputArray(i, 9) = "Include" Then
        Sheets("BDM").Range("A" & BDMRow & ":O" & BDMRow) = Application.Index(OutputArray, i)
        BDMRow = BDMRow + 1
    Else
        Sheets("Nationals").Range("A" & NatRow & ":O" & NatRow) = Application.Index(OutputArray, i)
        NatRow = NatRow + 1
    End If
    
    Next i

End Sub
 
Upvote 0
Thanks Norie, I'm new to Arrays so stumbling my way around a bit. Your code worked perfectly. Thank you very much.
 
Upvote 0
Thanks Norie, I'm new to Arrays so stumbling my way around a bit. Your code worked perfectly. Thank you very much.

My Array has 18,000 rows..... would it be quicker to write to two separate arrays then output each array in total to the relevant worksheet or leave as is and write to each worksheet as it moves through the rows?
 
Upvote 0
Writing the output to separate arrays would be quicker than constantly writing to the sheet.
Another option would be to autofilter & copy.
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,397
Members
448,957
Latest member
Hat4Life

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