简体   繁体   中英

How find a Range based on one criteria then a Cell within that Range based on another criteria

I need a way to find a Range based on one criteria then a Cell within that Range based on another criteria.

In this case I have a worksheet that you can add as many commercial real estate properties as you want and then spread there financials. You can have up to three years of financials for each property. So in cell("G2") you would be the first properties name and then in range("G3:I3") would be statement date for each statement spread and the range("G38:I38") contains the data I ultimately need to extract. Any additional property would have the same row numbers but everything would be 6 columns to the right.

So essentially I need to find the property name and then within that range (lets say ("G2:I60")) I need to find those the data in either G38, H38, or I38 based on the statement date needed.

Right now I have two userforms; 1 that displays all the property names and after you select that it shows the 2nd useform which displays the statement dates.

Can anyone help with this. Let me know if there are any unknowns I need to explain as this was difficult for me to put into words.

Example:

Property Name:  DEF St                                      
                12/31/2013  12/31/2014  12/31/2015
Rental Income       150         240         485
Expenses            100         200         300
Net Income          50          40          185

Property Name:  ACME St 
                12/31/2013  12/31/2014  12/31/2015
Rental Income       100         120         350         
Expenses            75          125         180         
Net Income          66          76          266         

In this example, I would need the 1st user form to find the property DEF St when selected from the list box then find the net income for whichever statement data was selected from the list box in the next user form.

Proof of concept

POC

The layout out of the data is SIMILAR to what is illustrated in the picture above. The sample data is in rows 2 to 6 with the formulas being below the sample data in G10:G14. It does not matter if columns J, K or L exist or not.

INDEX function is being used a lot in this example to determine your pick points or your ranges. There is a strong possibility that it can be simplified.

G10 is the property that is selected
G11 is the date that is selected

G12 to G14 is a formula that pulls the appropriate value from the corresponding property and date using the following formula:

=INDEX(INDEX($G$2:$O$6,1,MATCH($G$10,INDEX($G$2:$O$6,1,0),0)):INDEX($G$2:$O$6,5,MATCH($G$10,INDEX($G$2:$O$6,1,0),0)+2),MATCH($F12,$F$2:$F$6,0),MATCH($G$11,INDEX($G$2:$O$6,2,MATCH($G$10,INDEX($G$2:$O$6,1,0),0)):INDEX($G$2:$O$6,2,MATCH($G$10,INDEX($G$2:$O$6,1,0),0)+2),0))

This was placed in Cell G4 and copied down

HOW IT WORKS:

INDEX returns an address/range that matches the information that is fed to it. I defined the appropriety rows either through hard code a numeric value or using a MATCH function to determine positions.

INDEX($G$2:$O$6,1,MATCH($G$10,INDEX($G$2:$O$6,1,0),0))

This portion of the formula tells excel to look through the range G2:O6 , restricts its row to row 1 with the 1 , and then determines which column to look in with MATCH($G$10,INDEX($G$2:$O$6,1,0),0) .

For a break down of what match is doing, it is trying to find the value in cell G10 (The property) within the range G2:O6's first row. The first 0 is there for two reasons. Since the range is 2 dimension it appears you need to specify some value here or you will get an error despite it being labelled as optional. The second reason is the 0 tells index to use all column or rows depending on whether its used in the column or row portion of the INDEX formula. The second 0 tells index to look for an exact match.

As a result, when this portion is successfully evaluated, it returns the address of M2. This process is then repeated but adding 2 to the match results to put us in the O column and hard coding 5 to get to the address of O6. So using a little abbreviating here as an example, the above formulas of:

INDEX():INDEX()

with the example data winds up being evaluated to:

M2:O2

Then by using similar MATCH and INDEX combination within this range we just determine, we figure out which of the 3 columns in the range matches the date, and which of the 5 rows matches the word to the left in F12 to F14. This could have been hard coded but I opted to leave it as variable in case the data order was not the same as display order.

It is important that the spelling and dates all match exactly. Trailing spaces will throws things for a loop and may get some unexpected results. You can also wrap the whole formula in and IFERROR() function using "" or "Not Found" for the error results. This would prevent excel error messages from being displayed in the cells when not all the information has been entered.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM