在合并单元格中查找值

时间:2015-07-06 08:53:45

标签: vba excel-vba excel

我在合并单元格上搜索值时遇到问题。第9行和第10行合并标记为灰色。现在,我想搜索一个文本" TOTAL"并转到它的lastrow值,即#34; 2812"。如果不拆开细胞,有没有办法做到这一点?另外,单词" TOTAL"不是唯一的。还有" TOTAL"在另一个第20列,但我想选择第一列有这个词。

The grey filled cells are merged(row 9 and row 10)

1 个答案:

答案 0 :(得分:0)

以下假设标题位于活动表的第1行

Sub t()
Dim rng As Range
With ActiveSheet
    Set rng = .Range("A9:" & .Range("ZZ9").End(xlToRight).Address)
    col = Application.WorksheetFunction.Match("TOTAL", rng, 0)
    lastrow = .Cells(1000000, col).End(xlUp).Row
    Debug.Print Cells(lastrow, col).Value

End With
End Sub

要查找第二次出现,请复制代码并将起始范围从“A9”更改为.Cells(9,col + 1)。地址这将在下一列开始新范围

Sub t()
Dim rng As Range
With ActiveSheet
    Set rng = .Range("A9:" & .Range("ZZ9").End(xlToRight).Address)
    col = Application.WorksheetFunction.Match("TOTAL", rng, 0)
    lastrow = .Cells(1000000, col).End(xlUp).Row
    Debug.Print Cells(lastrow, col).Value

    Set rng = .Range(.Cells(9, col + 1).Address & ":" & .Range("ZZ9").End(xlToRight).Address)
    col2 = Application.WorksheetFunction.Match("TOTAL", rng, 0)
    Debug.Print Cells(lastrow, col + col2).Value

End With
End Sub