Select part of input range

eviMogwai

New Member
Joined
Jul 4, 2011
Messages
26
Hey folks!

So I'm programming a function that takes a range as input but during the course of the function I need to use a subset of this range as comparison.

So if I get a range("A20:A46") as input I need get the range without a few of the cells at start and end, so something like range("A24:A40").

Is there an elegant way to do this?
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
What's the criteria for exclusion?

If you just wanted to chop off the first and last 4 you could use something like

Code:
set mySmallerRange = myBiggerRange.offset(4).resize(myBiggerRange.rows.count - 8)
for example.

Whether that counts as elegant or not is purely subjective!
 
Upvote 0
The problem should be described correctly in the first post, but if anything's unclear here's an analogy in pseudocode

Code:
'-- An arbitrary list as input
myList[] = [a,b,c,d,e,f,g,h,i,j,k]
 
'-- A useful list consisting of a middle part of the input
UsefulList[] = myList[index 2 to index 8]
 
'-- UsefulList is [b,c,d,e,f,g,h]
 
Upvote 0
Try..


Code:
Sub Foo()
Set OrigInput = Range("A20:A46")
MsgBox OrigInput.Address
Set SubInput = OrigInput.Offset(4).Resize(17, 1)
MsgBox SubInput.Address
End Sub
 
Upvote 0
Well, this part of the function accepts 3 lengths of input range and depending on this length I need to trim off a couple of cells from start and end respectively.

Your solutions are neat, but is there a way to do this without knowing the orientation of the range?


If I put my range into an array, would there be an easier way?
 
Upvote 0
additional view of possibilities...

Code:
Sub RunThisMacroFirst()
x = 4
y = 17
Call Foo(x, y)
End Sub


Sub Foo(x, y)
Set OrigInput = Range("A20:A46")
MsgBox OrigInput.Address
Set SubInput = OrigInput.Offset(x).Resize(y, 1)
MsgBox SubInput.Address
End Sub
 
Upvote 0
Thanks for the help.

Although this would mean i first had to check that the input is a column or a row and modify respectively, it's a solution, but kind of cumbersome in my program.
 
Upvote 0
Your OrigRange you say you get through "Input" - how do you communicate this Input range to Excel?

Next, to me is what? reveals to you or the system what subRange (cells) that you need/want to identify? I'm not clear on this! (so can't help more)..
 
Upvote 0
Next, to me is what? reveals to you or the system what subRange (cells) that you need/want to identify? I'm not clear on this! (so can't help more)..

Sorry, but I don't seem to understand this comment, I'm not a native english speaker, could you please expand?


But I have managed a workaround (and hit another snag), the idea is to put the range through a for loop that picks the interesting cells from the input and union them together into a new range.

Code:
function foo(rLevels as Range) as Variant
'-- rLevels is at least 16 cells
 
'-- code goes here
 
'-- init as Union doesn't handle empty ranges
Set rMyLevels = rLevels(4)
 
    For i = 1 To rLevels.Cells.Count
        If i > 4 And i < 20 Then
           Set rMyLevels = Application.WorksheetFunction.Union(rMyLevels, rLevels.Cells(i))
        End If
        Next

I have deliberately put in 2 different ways of referring to a cell,
rLevels(4) and rLevels.Cells(i), with hope that someone could tell me which one is right, both return the value, but what type is it?

When I run the code, i get error 438 " Object Doesn't Support This Property Or Method".
any ideas?
 
Upvote 0

Forum statistics

Threads
1,214,873
Messages
6,122,029
Members
449,061
Latest member
TheRealJoaquin

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