Solved

Building array based on cell value

Posted on 2015-02-07
3
139 Views
Last Modified: 2016-02-10
Hi,
I'm new to arrays, and slowly getting better with vba.
I know how to use 'Do' or 'For Next' loops, but I have a sheet with about 15,000 rows of data and need to populate another sheet with data from the large range. So for example, gather all the rows relating to a certain month number. I have been reading that building a temporary array and passing the result to the sheet would be much quicker than using a loop. I can find examples of getting arrays from contiguous data, but I only want to add rows from the data where, for example, the value of column 5 is greater than 0. If anyone can point me in the right direction it would be much appreciated.
0
Comment
Question by:Stephen Byrom
3 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 40596273
You could put all the data in an array, then create another array from that array using your criteria and then transfer the new array to the other worksheet.
Dim arrData As Variant
Dim arrFiltered() As Variant
Dim I As Long
Dim J As Long
Dim Cnt As Long

    arrData = Sheets("Sheet1").UsedRange.Value

    For I = LBound(arrData) To UBound(arrData)
        If arrData(I, 5) > 0 Then
            Cnt = Cnt + 1
            ReDim Preserve arrFiltered(1 To UBound(arrData, 2), 1 To Cnt)
            For J = LBound(arrData, 2) To UBound(arrData, 2)
                arrFiltered(J, Cnt) = arrData(I, J)
            Next J
        End If
    Next I
    
    If Cnt > 0 Then
        Sheets("Sheet2").Range("A1").Resize(UBound(arrFiltered, 2), UBound(arrFiltered, 1)).Value = Application.Transpose(arrFiltered)
    End If

Open in new window

0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40596429
Can you post a sample workbook?
0
 
LVL 1

Author Closing Comment

by:Stephen Byrom
ID: 40596565
OMG !!
So much quicker than looping through the data.
I will study your code more deeply later and try to build other arrays myself, but for now it works perfectly on the data I have been given.
Thank you so much for your time.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
: Microsoft Office Collaborate for free and online versions of Microsoft  Word, Excel, Powerpoint, OneNote, Onedrive , Email, Calendar etc. In short we can say that Microsoft office is a suite of servers, applications and services developed by  Micr…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question