2017-08-29 101 views
2

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

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 
+0

是头部都在一个特定的列或行? –

+0

那么你的实际产出是多少?你期望什么?你有什么问题,你没有问过吗?你有没有看过文档的例子[Ra​​nge.FindNext](https://msdn.microsoft。com/en-us/vba/excel-vba/articles/range-findnext-method-excel)能正常工作吗?有一个完整的例子来说明如何实现它。 –

+0

@ L.Dutch它的一个特定列 – Ashok

回答

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

+0

我试图得到类似的东西,从行A1到最后一个单元格。但是,我第二次发现到第五。而不是第一个到第四个。与你的类似,只是改变这部分'Loop While Not cF is Nothing and cF.Address <> FirstAddress而我<= cnt'和'cnt = 4'。 – danieltakeshi

+0

使用你的公式我还是有'发生1:$ A $ 2 发生2:$ A $ 3 发生3:$ A $ 4 发生4:$ A $ 5''Set rng = ws.Columns(1)'只是如果第一次出现是范围的第一个单元格,则不起作用 – danieltakeshi

+0

@danieltakeshi - 让我检查一下。 – Mrig

1

FindNext方法有Range类型的可选参数,它ommit或使其:

Set r = ThisWorkbook.ActiveSheet.UsedRange.FindNext(r) 
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 
相关问题