2017-08-29 46 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 
相關問題