2017-04-05 50 views
0

我有這個Excel公式轉換Excel公式VBA代碼(重複單元的返回地址)

=IF(COLUMNS($D2:D2)>$C2,"",SMALL(IF($B$2:$B$20=$B2,ROW(B$2:B$20)),COLUMNS($D2:D2))) 

基本上,這個公式確實是期待通過姓名(重複)的列表,然後返回的位置我正在尋找的名字。

例如,如果我的數據看起來是這樣的:

A 
B 
D 
A 

,如果我找一個,然後我的公式將返回1和4(在兩個不同的細胞)

我的問題是如何將這個公式轉換成VBA代碼?

回答

0

假設你要放置在E2這個數組公式,然後將其拖動到N2,試試這個...

Range("E2").FormulaArray = "=IF(COLUMNS($D2:D2)>$C2,"""",SMALL(IF($B$2:$B$20=$B2,ROW(B$2:B$20)),COLUMNS($D2:D2)))" 
Range("E2").AutoFill Destination:=Range("E2:N2"), Type:=xlFillDefault 
0
Sub checkDuplicate() 
Dim sduplicate As String 
Dim lastrow As Long 
Dim iCount As Long 
Dim j As Long 
    'To Find the Last Row of the Sheet 
lastrow = Sheets("sheetname").Cells(Rows.Count, "A").End(xlUp).Row 
    'To get the input from the user 
sduplicate = InputBox("Please enter the Name:") 
    'Looping 
j = 1 
    For iCount = 1 To lastrow 
     'Assuming the "name" is in column 1 so checking in column 1 
     If Sheets("sheetname").Cells(iCount, 1).Value = sduplicate Then 
      'if the duplicate is found then put the value in column 5 
      Sheets("sheetname").Cells(j, 5).Value = iCount 
      j = j + 1 
     End If 

    Next iCount 

End Sub