简体   繁体   中英

VBA Excel count cells in a range with a specific value

I am looking for a way to count the number of cells in a range with a specific value, but will skip cells that are next to the previous cell if they have the same value. So that if I have a series like 1,2,3,3,4,3 the result for the number of 3's would be 2.

Use this:

=COUNTIF(A:A,3)-COUNTIFS(A1:A1040000,3,A2:A1040001,3)

在此处输入图片说明


Edit:

The following formula works just as well:

=COUNTIFS(A1:A1040000,3,A2:A1040001,"<>" & 3)

In vba:

Dim output as Long
With AcitveSheet
    output = Application.WorksheetFunction.Countifs(.Range("A1:A1040000"),3,.Range(A2:A1040001),"<>" & 3)
End With
Debug.Print output

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM