获取第一行中匹配字符串的列号。 Excel VBA

时间:2014-10-14 03:12:56

标签: excel vba excel-vba numbers find

我正在尝试创建一个函数来根据插入的匹配字符串获取单元格的列号。如果在第一行中找到两个匹配,我想返回最后一个匹配。例如,“TotalSalary Jan”和“TotalSalary Feb”。将“TotalSalary”作为参数插入,我将获得“TotalSalary Feb”的列号。我的代码:

Private Function GetColumnNumber(name As String) As Integer

Dim res As Object, ret As Integer

Set res = Sheets("Unified").Cells(1, 1).EntireRow.Find(What:=name, LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)

If Not res Is Nothing Then
    ret = res.Column
    Do
        Set res = .FindNext(res)
        ret = res.Column
    Loop While Not res Is Nothing And res.Column <> ret
    GetColumnNumber = ret
End If

End Function

顺便说一句,代码无法正常工作。 res对象无法找到列号的下一个。

3 个答案:

答案 0 :(得分:2)

试试这个并告诉我。

私有函数GetColumnNumber(strKeyword As String)As Integer

    Dim rngColLoop      As Range
    Dim intCounter      As Integer
    Dim intColNum       As Integer
    Dim intPrevious     As Integer
    Dim intCurrent      As Integer

    lngCounter = 0
    With Sheets("Unified").Cells(1, 1).EntireRow
        For Each rngColLoop In .Columns
            If Trim(rngColLoop) <> "" Then
                If InStr(1, UCase(Trim(rngColLoop)), UCase(Trim(strKeyword))) > 0 Then
                    intCounter = intCounter + 1
                    If intCounter = 1 Then
                        intPrevious = rngColLoop.Column
                        intCurrent = rngColLoop.Column
                    Else
                        intPrevious = intCurrent
                        intCurrent = rngColLoop.Column
                    End If

                End If
            End If
        Next rngColLoop
    End With
    If intCounter = 0 Then
        GetColumnNumber = 0
    Else
        GetColumnNumber = intCurrent
    End If

    Set rngColLoop = Nothing

结束功能

答案 1 :(得分:1)

我使用了不同的方法,通过更改搜索方向,您可以使用单个查找方法找到最后一个实例。

私有函数GetColumnNumber(name as String)As Integer

Dim res As Object

Set res = Sheets("Unified").Cells(1, 1).EntireRow.Find(What:=name _
                                                       , LookIn:=xlValues _
                                                       , LookAt:=xlPart _
                                                       , SearchOrder:=xlByColumns _
                                                       , SearchDirection:=xlPrevious _
                                                       , MatchCase:=False)

If res Is Nothing Then
    GetColumnNumber = 0
Else
    GetColumnNumber = res.Column
End If

结束功能

答案 2 :(得分:1)

我已经创建了另一个在此函数中添加的方法。这工作顺便......我正在使用变量数组来获取列号。

Private Function GetColumnNumber(name As String) As Integer
    Dim play As Variant, j As Long, Current As Integer
    Set play = Sheets("Unified").Range("1:1")
    For i = 1 To play.Columns.Count
        If InStr(play(1, i), name) > 0 Then
            Current = i
        End If
    Next i
    GetColumnNumberArray = Current
End Function

我看一下这篇文章,它对优化代码非常有帮助。 http://fastexcel.wordpress.com/2011/10/26/match-vs-find-vs-variant-array-vba-performance-shootout/ 显然,查找和匹配的使用对于您的计算机来说是非常苛刻的命令。