【问题标题】:Get Column number of matching string in first row. Excel VBA获取第一行匹配字符串的列号。 Excel VBA
【发布时间】:2014-12-08 17:30:07
【问题描述】:

我正在尝试创建一个函数来根据插入的匹配字符串获取单元格的列号。如果在第一行找到两个匹配项,我想返回最后一个匹配项。例如,“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 对象找不到列号的下一个。

【问题讨论】:

    标签: vba excel numbers find


    【解决方案1】:

    试试这个,然后告诉我。

    私有函数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
    

    结束函数

    【讨论】:

    • 一般都可以,但是TotalSalary Feb 和TotalSalary March 这样的例子还是把TotalSalary Feb 拿出来了。我试试看我能做什么。 *编辑:现在它可以工作了。只需在最后三行将 intPrevious 更改为 intCurrent 即可。顺便说一句,好答案!
    【解决方案2】:

    我使用了另一种方法,通过改变搜索方向,您可以使用单个 find 方法找到最后一个实例。

    私有函数 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
    

    结束函数

    【讨论】:

      【解决方案3】:

      我已经创建了另一种方法来添加到这个函数中。顺便说一句,这是可行的...我正在使用 Variant Array 来获取列号。

      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/ 显然,查找和匹配的使用对您的计算机要求很高。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2012-05-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多