2

これは非常に基本的な質問です。私は vba の専門家ではないので、怒鳴らないでください。

それでは、以下のvba関数を作成しました

Public Function GetDuplicateCount(value As String) As Integer

    Dim counter As Integer

    counter = 0

    With Worksheets(1).Range("A:A")
        Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    MatchCase:=False)

        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                counter = counter + 1
                Set c = .FindNext(c)
            Loop While Not c Is Nothing

        End If
    End With

    GetDuplicateCount = counter
End Function

以下は私のExcelの値です

1 IND

2 アメリカ

3缶

4 IND

5缶

6 アメリカ

任意の値で検索するたびに、不明な値が返されます。機能に問題はありますか?

例: GetDuplicateCount("IND")

4

3 に答える 3

3

わかった…やっと

FindNext が機能しない 2 つの点があるため、@kazjaw が示唆するように、.find を試しました。これが機能するコードです。追加の条件を指定することを忘れないでください。それは「firstAddress <> c.Address」です。

Public Function GetDuplicateCount(value As String) As Integer

    Dim counter As Integer
    counter = 0

    With Worksheets(1).Range("A:A")
        Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    after:=Cells(1, 1), _
                    MatchCase:=False)

        If Not c Is Nothing Then
            firstAddress = c.Address
            Do
                counter = counter + 1
                Set c = .Find(value, _
                    LookIn:=xlValues, _
                    LookAt:=xlWhole, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlNext, _
                    after:=c, _
                    MatchCase:=False)
            Loop While Not c Is Nothing And firstAddress <> c.Address

        End If
    End With

    GetDuplicateCount = counter
End Function
于 2013-08-09T14:24:57.367 に答える
1

ネイティブの Countif 関数を使用しないのはなぜですか?

=COUNTIF(A:A,"IND")
于 2013-08-08T21:02:32.827 に答える