【问题标题】:Convert Excel formula to VBA code (return location of duplicate cell)将 Excel 公式转换为 VBA 代码(返回重复单元格的位置)
【发布时间】:2017-04-05 19:24:34
【问题描述】:

我有这个 Excel 公式

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

基本上,这个公式的作用是查看名称列表(有重复项),然后返回我要查找的名称的位置。

例如,如果我的数据看起来像这样:

A
B
D
A

如果我正在寻找 A,那么我的公式将返回 1 和 4(在两个不同的单元格中)

我的问题是如何将此公式转换为 VBA 代码?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    假设你想把这个 Array 公式放在 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
    

    【讨论】:

      【解决方案2】:
       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
      

      【讨论】:

        猜你喜欢
        • 2013-05-27
        • 2014-05-25
        • 2021-07-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多