Public Sub distinctCount()
'求Col列中StartRow到EndRow范围中不重复的个数
'本例是计算标题为sheet1的Excel表中的A1:A240的不重复值的个数
'修改下面四行的结尾值
Dim sheetsCaption As String: sheetsCaption = "Sheet1"
Dim Col As String: Col = "A"
Dim StartRow As Integer: StartRow = 1
Dim EndRow As Integer: EndRow = 240
'以下固定
Dim Count As Integer: Count = 0
With Sheets(sheetsCaption)
For i = StartRow To EndRow
Count = Count + 1
For j = StartRow To i - 1
If .Range(Col & i) = .Range(Col & j) Then
Count = Count - 1
Exit For
End If
Next
Next
End With
MsgBox Count
End Sub
'求Col列中StartRow到EndRow范围中不重复的个数
'本例是计算标题为sheet1的Excel表中的A1:A240的不重复值的个数
'修改下面四行的结尾值
Dim sheetsCaption As String: sheetsCaption = "Sheet1"
Dim Col As String: Col = "A"
Dim StartRow As Integer: StartRow = 1
Dim EndRow As Integer: EndRow = 240
'以下固定
Dim Count As Integer: Count = 0
With Sheets(sheetsCaption)
For i = StartRow To EndRow
Count = Count + 1
For j = StartRow To i - 1
If .Range(Col & i) = .Range(Col & j) Then
Count = Count - 1
Exit For
End If
Next
Next
End With
MsgBox Count
End Sub
博客给出一段VBA代码,用于计算Excel表中指定列和行范围的不重复值个数。代码中可修改工作表名称、列名、起始行和结束行,通过嵌套循环遍历范围,若有重复则减少计数,最后用消息框显示结果。
8190

被折叠的 条评论
为什么被折叠?



