在工作表vba中查找第一,第二,第三和第四个特定列标题

我试图在工作表中find特定的列标题。 例如标题名称payout date ,需要在工作表中findpayout date的发生,如第一出现单元地址,第二出现单元地址,第三出现单元地址和第四出现单元地址。 任何build议,将不胜感激。 下面的代码无法正常工作

 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 

以下可能会有所帮助。

 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 

请参阅图片以供参考。

在这里输入图像描述

FindNext方法具有Rangetypes的可选参数,省略或使其成为:

 Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(r) 

这可能是我每天做几次的事情。 因此,我已经build立了我的自定义function,我可以轻松分享。 如果你有改进的想法 – 我愿意听取他们的意见。

这是function:

 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 

因此,如果你想获得活动工作表中第一行的第一个值,你可以这样调用:

 fnLngLocateValueCol("valueToSearchFor",ActiveSheet) 

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

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

对于你所调用的最后一个值,像这样:

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

如果你在列中有ValueToSearchFor ,你可以通过查找以Value开头的任何东西来find它。 喜欢这个:

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

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

对于lngRow (当它不是顶行)或blnLookForPart (当您正在查找零件时)也有可选参数。 -999是未find的值的代码。

到目前为止,它在一些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