在VBA中查找标题

时间:2015-03-04 15:18:21

标签: excel vba excel-vba header

我在VBA中有一个函数可以找到标题并选择标题下面的列。它工作得非常漂亮,除了我试图找到一个以CLIENT作​​为标题的列的一个实例,并且它继续选择“CLIENT ID作为标题(它就在它旁边)的列。我试图把东西放在处理这个的功能,但每次我尝试,它只是失败。任何帮助这个问题将不胜感激!

Function FindHeader(HEADER_NAME As String, sheetName As String) As Range

    Dim rngHeaders As Range
    Dim rngHdrFound As Range

    Const ROW_HEADERS As Integer = 1

    Set rngHeaders = Intersect(Worksheets(sheetName).UsedRange, Worksheets(sheetName).Rows(ROW_HEADERS))
    Set rngHdrFound = rngHeaders.Find(HEADER_NAME)

    If rngHdrFound Is Nothing Then
       MsgBox ("ERROR: Cannot find appropriate header.")
        Exit Function
    ElseIf rngHdrFound = "CLIENT" Then
     'Handle Client range here   
    End If

  Set FindHeader = Range(rngHdrFound.Offset(1), rngHdrFound.End(xlDown))

End Function

1 个答案:

答案 0 :(得分:2)

更改FIND方法以仅查找整个匹配而非部分匹配。只要确保这在其他地方没有负面影响。

Set rngHdrFound = rngHeaders.Find(HEADER_NAME,,,xlWhole)