在工作表vba中查找特定列标题的第1,第2,第3和第4次出现

时间:2017-08-29 12:42:59

标签: excel vba excel-vba

我正在尝试在工作表中找到特定的列标题。例如 标题名payout date,需要在工作表中找到payout date的出现次数,如第一次出现单元格地址,第二次出现单元格地址,第三次出现单元格地址和第四次出现单元格地址。任何建议,将不胜感激。以下代码无法正常工作

Sub find()
d = "Payout Date"
Set r = ThisWorkbook.ActiveSheet.UsedRange.find(d)
Debug.Print r.Address
Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(d)
Debug.Print r.Address
Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(d)
Debug.Print r.Address
Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(d)
Debug.Print r.Address
End Sub

3 个答案:

答案 0 :(得分:2)

以下可能会有帮助。

Sub Demo()
    Dim ws As Worksheet
    Dim i As Long, cnt As Long
    Dim rng As Range, rng2 As Range
    Dim cellFound As Range

    Set ws = ThisWorkbook.Sheets("Sheet1")  'change Sheet1 to your data sheet
    Set rng = ws.Range("1:1") 'assuming headers are in row 1 else change 1 to row number with headers
    Set rng2 = rng(1, Columns.Count)
    cnt = 3              'number of occurrences to find
    i = 1
    With rng
    Set cellFound = .find(what:="ID", After:=rng2, LookIn:=xlValues)
        If Not cellFound Is Nothing Then
           firstAddress = cellFound.Address
           Do
               Debug.Print "Occurrence " & i & " : " & cellFound.Address
               i = i + 1
               If i > cnt Then: Exit Do
               Set cellFound = .FindNext(cellFound)
           Loop While cellFound.Address <> firstAddress
         End If
      End With
End Sub

参见图片以供参考。

enter image description here

答案 1 :(得分:1)

Range方法具有Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(r) 类型的可选参数,请将其省略或制作:

<Connector port="8543" maxHttpHeaderSize="8192" address="192.168.1.1"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" scheme="https" secure="true" clientAuth="false" 
keystoreFile="SomeDir/SomeFile.key" keystorePass="extensa"
truststoreFile="SomeDir/SomeFile.truststore" truststorePass="extensa" 
sslProtocol="TLSv1, TLSv1.1, TLSv1.2" 
ciphers="TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, 
TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, 
TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, ...
"/> 

答案 2 :(得分:1)

这可能是我每天要做几次的事情。因此,我为它构建了自定义函数,我可以轻松地分享它。如果你有改进的想法 - 我愿意听到它们。

这是功能:

Public Function fnLngLocateValueCol(ByVal strTarget As String,               
                   ByRef wksTarget As Worksheet, _
                   Optional lngRow As Long = 1, _
                   Optional lngMoreValuesFound As Long = 1, _
                   Optional blnLookForPart = False, _
                   Optional blnLookUpToBottom = True) As Long  

    Dim lngValuesFound          As Long
    Dim rngLocal                As Range
    Dim rngMyCell               As Range

    fnLngLocateValueCol = -999
    lngValuesFound = lngMoreValuesFound

    With wksTarget
        Set rngLocal = .Range(.Cells(lngRow, 1), .Cells(lngRow, Columns.Count))
    End With        

    For Each rngMyCell In rngLocal
        If blnLookForPart Then
            If strTarget = Left(rngMyCell, Len(strTarget)) Then
                If lngValuesFound = 1 Then
                    fnLngLocateValueCol = rngMyCell.Column
                    If blnLookUpToBottom Then Exit Function
                Else
                    Call Decrement(lngValuesFound)
                End If
            End If
        Else
            If strTarget = Trim(rngMyCell) Then
                If lngValuesFound = 1 Then
                    fnLngLocateValueCol = rngMyCell.Column
                    If blnLookUpToBottom Then Exit Function
                Else
                    Call Decrement(lngValuesFound)
                End If
            End If
        End If
    Next rngMyCell    

End Function

因此,如果您想获取活动工作表中第1行的第一个值,请按以下方式调用:

fnLngLocateValueCol("valueToSearchFor",ActiveSheet)

对于第二个值,您可以这样调用:

?fnLngLocateValueCol("valueToSearchFor",ActiveSheet,lngMoreValuesFound:=2)

对于您调用的最后一个值:

?fnLngLocateValueCol("valueToSearchFor",ActiveSheet,blnLookUpToBottom:=false)

如果列中包含 ValueToSearchFor ,您也可以通过查找以开头的任何内容来查找它。像这样:

?fnLngLocateValueCol("Value",ActiveSheet,blnLookForPart:=True)

您要查找的行也是可选参数(lngRow),值为1。

当您正在寻找零件时,lngRow(当它不是顶行)或blnLookForPart时,还有可选参数。 -999 是未找到值的代码。

到目前为止,它在一些VBA应用程序中的工作时间超过6个月。

代码中引用的例程Decrement如下:

Public Sub Decrement(ByRef value_to_decrement As Variant, Optional l_minus As Double = 1)
    value_to_decrement = value_to_decrement - l_minus
End Sub