根据表头和行名获取一定的值

可以说我的Excel工作表看起来像这样:

Column A | Column B ------ | ------ Table header1 | Some row name | 1000 Some row name | 2000 Total | 3000 | Table header2 | Some row name | 2000 Some row name | 2000 Total | 4000 

此工作表来自外部来源,行的位置并不总是完全相同。 我想要列B的值,其中列A是Total ,表头是Table header2

通常我会写一些静态代码,如: 查找Table header2并求和x行,直到达到所需的值。 在这种情况下,表头和Total行之间的行数是dynamic的,所以不能帮助我。

你们有什么想法来解决这个问题吗?

一个简单的子喜欢

 Sub GetTotalValue2() Dim RefRow As Integer Dim CellContent As String Dim Total As Long RefRow = 0 'Find header row: Do RefRow = RefRow + 1 CellContent = ThisWorkbook.Sheets("Sheet1").Cells(RefRow, 1).text Loop Until CellContent = "Table header2" 'Find total row: Do RefRow = RefRow + 1 CellContent = ThisWorkbook.Sheets("Sheet1").Cells(RefRow, 1).text Loop Until CellContent = "Total" 'Return value to Total variable: Total = ThisWorkbook.Sheets("Sheet1").Cells(RefRow, 2).Value End Sub 

将会解决你的问题,我想。

请注意:a)您必须通过您使用的whorksheet名称更改“sheet1”; b)如果你的表没有在表头中有一个“Table header2”文本内容或者“Total”文本内容的单元格,这段代码将会产生一个错误。 如果是这种情况,我们可以做一个error handling程序来解决这个问题。

UPDATE

单一的公式来返回值

 =INDEX(A1:B1000,MATCH("Total",OFFSET(A1:A1000,MATCH("Table Header 2",A1:A1000,0),0),0)+MATCH("Table Header 2",A1:A1000,0),2) 

该公式通过查找“Table Header 2”行然后使用偏移量来推动范围来查找表格标题下面的Total,从而find正确的Total行。

先前的答案要求将公式自动填入列C中

如果您的列从第2行开始,那么您可以使用此公式并自动填充,以在表头2下面的Total旁边返回4000。

 =IF(A2="Table Header 2",TRUE,IF(C1=TRUE,IF(A2="Total",B2,TRUE))) 

你可以用一个参考文本replace文本,例如$F$2

 =IF(A2=$F$2,TRUE,IF(C1=TRUE,IF(A2="Total",B2,TRUE))) 

然后在G2单元格中添加一个和,返回F2指定的标题的值

 =sum(B2:B1000) 

就像一个迟到的答案,下面的工作对我很好。 它将使您能够根据标题的第一次出现量身定制您的search,然后search您正在查找的小计值的名称。

 'An arbitrary number to limit the number of times we iterate through the 'loop checking cells - we don't want to go too far... Const gapSize As Integer = 10 'GetSectionSubtotal ' Gets a value, as an implied subtotal, from a list of values after a ' particular title occurs in the list. 'Usage (direct function entry into a cell): ' =GetSectionSubtotal(A1:B30, "Title 2", "Total") 'Parameters: ' rng (Range) - A range definition containing the data you wish to test. ' secTitle (String) - A string containing the title (or first occurrence) ' of the section you wish to test for. ' totTitle (String) - A string containing the name of the cell you wish to ' return a value for after the first occurrence of secTitle. 'Notes: ' This function returns the *first* value associated to totTitle after the ' first occurrence of secTitle. If you have mulitple occurrences of the ' section title then this code will need to be revised. ' The gapSize allows the function to quit gracefully if the process ' experiences a series of empty cells. Public Function GetSectionSubtotal( _ rng As Range, _ secTitle As String, _ totTitle As String) As Double Dim r As Integer Dim rv As Double Dim go As Boolean Dim fail As Integer Dim found As Boolean r = 1 go = True fail = 0 Do While go 'Determine if we've found our title section... found = found Or rng.Cells(r, 1) = secTitle 'We only want to continue when the title is either found or we've 'passed the end of the list... go = (((found And Not rng.Cells(r, 1) = totTitle) _ Or (Not found)) And fail < gapSize) 'If we're not going anymore then the answer has been found or we've 'exceeded the end of the block within the range... If found And Not go Then rv = rng.Cells(r, 2) 'Increase fail based on the value of the first column. This 'assignment will zero fail again if a value is found... fail = ((fail + 1) And rng.Cells(r, 1) = "") 'Increment our row counter... r = r + 1 Loop GetSectionSubtotal = rv End Function