过滤重复值并使用Excel VBA计算出现次数

时间:2015-03-05 17:56:52

标签: excel-vba vba excel

如何在VBA中执行此任务?

示例,以下信息包含在Excel中名为fraca1的电子表格中,请注意字母A和F是Excel中的列名,而不是数据集的一部分。

A                   F
FRACA 012313        Correction 1
FRACA 012313        Correction 2
FRACA 012313        Investigation 1
FRACA 012313        Investigation 2
FRACA 012317        Investigation 1
FRACA 012317        Investigation 2
FRACA 012317        Investigation 3
FRACA 018593        Correction 1
FRACA 035586        Correction 1

我的数据包含在几个列中。我需要找到3件事。

  1. A栏中有哪些独特的FRACA编号。
  2. F列中有多少个单元格包含单词"更正"对应于每个唯一的FRACA编号。
  3. F列中有多少单元格包含"调查"对应于每个唯一的FRACA编号。
  4. 然后,必须将此信息放入同一工作簿中的新电子表格中。生成的电子表格如下所示:

    A               B                 C
    FRACA No.       Correction No.    Investigation No.
    FRACA 012313    2                 2
    FRACA 012317    0                 3
    FRACA 018593    1                 0
    FRACA 035586    1                 0
    

1 个答案:

答案 0 :(得分:2)

我希望这会有所帮助,这是你问题的第一部分。您必须在VBA上激活Microsoft Scripting Runtime引用。

''''''''''''''''''
'1-What are the unique FRACA numbers in column A.
''''''''''''''''''
Application.Calculation = xlCalculationManual 'For faster running time
'Create the Scripting Dictionary
Dim d As Object
Set d = CreateObject("Scripting.Dictionary")
Dim myFirstCol() As Variant
Dim i As Long
'Code Assumes data is in Sheet1 and needs to go to Sheet2 (code names)
arrLength = WorksheetFunction.CountA(Sheet1.Columns("A:A"))
ReDim myFirstCol(1 To arrLength)
'First read your input column
For i = 1 To arrLength
    myFirstCol(i) = Sheet1.Cells(i, 1)
Next i
'create the unique keys
For i = LBound(myFirstCol) To UBound(myFirstCol)
    d(myFirstCol(i)) = mySecondCol(i)
Next i
'Now write your keys to Sheet2
Dim v As Variant
i = 2
For Each v In d.Keys()
    Sheet2.Cells(i, 1) = v
    i = i + 1
Next v
Application.Calculation = xlCalculationAutomatic

对于您的其余问题,现在应该更容易解决FRACA的唯一值。如果我有一些时间,我将用其他两个部分完成答案。