让我们说我的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
行之间的行数是动态的,因此无法帮助我。
你们对如何解决这个问题有什么想法吗?
答案 0 :(得分:1)
<强>更新强>
单个公式返回值
=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)
该公式的工作原理是找到“表头2”行,然后使用偏移推送范围以在该表头下方找到总计,从而找到正确的总行。
以前的答案需要在C列下自动填充公式
如果您的列从第2行开始,那么您可以使用此公式并自动填充以在表头2下的总计旁边返回4000.
=IF(A2="Table Header 2",TRUE,IF(C1=TRUE,IF(A2="Total",B2,TRUE)))
您可以使用参考资料替换文字,例如$F$2
=IF(A2=$F$2,TRUE,IF(C1=TRUE,IF(A2="Total",B2,TRUE)))
然后在单元格G2
中添加一个总和以返回F2
=sum(B2:B1000)
答案 1 :(得分:0)
像
这样的简单子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)您必须使用您正在使用的表格名称更改“sheet1”; b)如果您的表没有带有“表头2”文本内容的单元格或标题旁边的“总计”文本内容,则此代码将产生错误。如果是这种情况,我们可以使用错误处理程序来解决问题。
答案 2 :(得分:0)
就像一个迟到的答案,以下对我很有帮助。它将使您能够根据首次出现的标题定制搜索,然后是您要查找的小计值的名称...
'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