在数组中输入值,然后计算其中唯一值的数量

时间:2015-07-07 16:31:43

标签: excel vba excel-vba

我正在编写一个宏来计算工作表列中的唯一值数。还有一些其他参数,我已经介绍过了。基本上我想要找出的是如何将值输入到数组(unique()),然后如何计算该数组中唯一值的数量。这是我到目前为止所做的。

Private Sub CommandButton1_Click()

Dim dateCheck As String
Dim lastRow As Long
Dim L As Integer
Dim I As Long
Dim shipDay As Date
Dim search As String
Dim unique() As Variant
Dim number As String

For L = 0 To 21  ' Execute code for whole month

    shipDay = Worksheets("June Canada").Cells(L + 10, 10).Text   'Sets to correct date

    For I = 1 To Worksheets("Sheet1").UsedRange.Rows.Count  ' Searches entire worksheet

        search = Worksheets("Sheet1").Cells(I, 12).Value ' Checks for string within a row

        If ((InStr(1, search, "CAN", vbBinaryCompare) = 1) _
            And (Worksheets("Sheet1").Cells(I, 8) = shipDay) _
            And (Worksheets("Sheet1").Cells(I, 6).Text = "Invoice")) Then

            number = Worksheets("Sheet1").Cells(I, 10).Value ' Set order number to variable

            ' Add the variable values to the dynamic array

        End If

    Next I

    Worksheets("JUNE canada").Cells(L + 10, 8).Value = ???  'Enter # of unique values into sheet

    shipDay = shipDay - L

Next L

End Sub

1 个答案:

答案 0 :(得分:0)

这样的东西,但可能需要一些调整:

' Add the variable values to the dynamic array
Dim foundIt As Boolean = False
For x As Integer = 0 To unique.Length
    If unique(x) = number Then
        foundIt = True
        Exit For
    End If
Next
If foundIt = False Then
    unique(unique.Length + 1) = number
End If
相关问题