How to query a range

oaishm

Board Regular
Joined
Jan 30, 2009
Messages
97
I have a range called movie with field names in the first row. It looks like this:

movienum title rating
1 batman R
2 superman R
3 wonderwoman PG

How can I return an array of only the records that will fulfill some criteria. If this were sql it would look like

select * from movie where rating = "PG"
 
You are trying to resizing the 1st dimention, but you can only resize the last dimention of the array.
try
Code:
Function filterarray(frange As Range, filtercolumn, criteria As String)
Dim a, i As Long, ii As Long, n As Long, result() As Variant
a = frange.Value
For i = 1 To UBound(a, 1)
    If a(i, filtercolumn) = criteria Then
        n = n + 1
        ReDim Preserve result(1 To UBound(a, 2), 1 To n)
        For ii = 1 To UBound(a, 2)
            result(ii, n) = a(i, ii)
        Next
    End If
Next
filterarray = Application.Transpose(result)
End Function
 
Upvote 0

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
You can only resize the last dimension of an array? Why is that. See, this is stuff that isn't documented anywhere. Then you have to transpose in order to filter. That's crazy. Alas. Thanks for the help. This has been killing me
 
Upvote 0
If you need to deal with the array, you should understand the basic nature of the array, otherwise it will always put you in a trouble.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

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