简体   繁体   English

如何根据一个条件查找范围,然后根据另一个条件查找该范围内的单元格

[英]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.因此,在 cell("G2") 中,您将是第一个属性名称,然后在 range("G3:I3") 中将是每个报表点差的报表日期,而 range("G38:I38") 包含我最终需要的数据提取。 Any additional property would have the same row numbers but everything would be 6 columns to the right.任何其他属性都将具有相同的行号,但所有内容都将在右侧 6 列。

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.所以基本上我需要找到属性名称,然后在该范围内(比如(“G2:I60”))我需要根据所需的声明日期在 G38、H38 或 I38 中找到那些数据。

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. 1 显示所有属性名称,在您选择后,它会显示显示语句日期的第二个使用表单。

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.在此示例中,当从列表框中选择时,我需要第一个用户表单来查找属性 DEF St,然后查找从下一个用户表单的列表框中选择的任何报表数据的净收入。

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.样本数据在第 2 至 6 行,公式在 G10:G14 的样本数据下方。 It does not matter if columns J, K or L exist or not. J、K 或 L 列是否存在并不重要。

INDEX function is being used a lot in this example to determine your pick points or your ranges. INDEX 函数在这个例子中被大量使用来确定你的拾取点或你的范围。 There is a strong possibility that it can be simplified.有很大的可能性可以简化它。

G10 is the property that is selected G10 是选择的属性
G11 is the date that is selected G11 是选择的日期

G12 to G14 is a formula that pulls the appropriate value from the corresponding property and date using the following formula: G12 到 G14 是一个公式,它使用以下公式从相应的属性和日期中提取适当的值:

=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这被放置在Cell G4中并复制下来

HOW IT WORKS:它是如何工作的:

INDEX returns an address/range that matches the information that is fed to it. INDEX 返回与提供给它的信息相匹配的地址/范围。 I defined the appropriety rows either through hard code a numeric value or using a MATCH function to determine positions.我通过硬编码数值或使用 MATCH 函数来确定位置来定义适当的行。

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) .式的这个部分告知Excel通过范围外表G2:O6 ,限制了其行与到行1 1 ,然后确定在与列外表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.为了分解匹配正在做什么,它试图在 G2:O6 的第一行范围内的单元格 G10(属性)中找到值。 The first 0 is there for two reasons.有第一个 0 有两个原因。 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.由于范围是 2 维,因此您似乎需要在此处指定一些值,否则尽管将其标记为可选,您仍会收到错误消息。 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.第二个原因是 0 告诉索引使用所有列或行,具体取决于它是在 INDEX 公式的列部分还是行部分中使用。 The second 0 tells index to look for an exact match.第二个 0 告诉索引查找完全匹配。

As a result, when this portion is successfully evaluated, it returns the address of M2.因此,当成功评估此部分时,它将返回 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.然后重复此过程,但将匹配结果添加 2 以将我们放入 O 列并硬编码 5 以获取 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.然后通过在我们刚刚确定的这个范围内使用类似的 MATCH 和 INDEX 组合,我们计算出该范围中的 3 列中哪些与日期匹配,以及 5 行中的哪些与 F12 到 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.您还可以使用“”或“未找到”将整个公式包含在 IFERROR() 函数中以获取错误结果。 This would prevent excel error messages from being displayed in the cells when not all the information has been entered.这将防止在未输入所有信息时在单元格中显示 excel 错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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