Extract subrange from a range

azer

New Member
Joined
Jan 31, 2013
Messages
2
Hello and thank you for your time and help,

I am trying to create a function which would extract a subset from a given range depending on the values contained in another range. The final aim is to be able to use the Percentrank function on dynamically changeable groups

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Country[/TD]
[TD]GPB[/TD]
[TD]Group[/TD]
[TD]Percentrank[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]Albania[/TD]
[TD]5000[/TD]
[TD]1[/TD]
[TD]?[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]France[/TD]
[TD]28000[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Romania[/TD]
[TD]6000[/TD]
[TD]1[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]US[/TD]
[TD]34000[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

The idea would to have something like Percentrank(subset(B2:B4, C2:C4, 1), B2) with the subset function returning the subset as a range.

I have started with something like this but am stuck on a few issues:

Function Subset(x As Range, y As Range, V As String) As Range


Dim z As Range
Dim c As Range


For Each c In x

' I don't see how to refer to the equivalent cell in y:
If ??.Value = Valeur Then


If Not z Is Nothing Then
Set z = Union(z, c)
Else
Set z = c
End If
End If
Next

'Even with a simple example I am unable to return a range:

Subset = z


End Function


Thank you in advance for your help
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Here's my latest try which doesn't work:

Code:
Function Subs(x As Range, y As Range, V As Integer) As Range

Dim z As Range
Dim c As Cell
Dim n As Integer

    For Each c In x.Cells[INDENT]Set n = c.Row[/INDENT]
[INDENT]If y(1, n).Value = Valeur Then[/INDENT]
[INDENT=2]If Not z Is Nothing Then[/INDENT]
[INDENT=3]Set z = Union(z, c)[/INDENT]
[INDENT=2]Else[/INDENT]
[INDENT=3]Set z = c[/INDENT]
[INDENT=2]End If[/INDENT]
[INDENT]End If[/INDENT]
    Next

Subs = z

End Function

Any thoughts?
 
Upvote 0

Forum statistics

Threads
1,223,162
Messages
6,170,432
Members
452,326
Latest member
johnshaji

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