作业帮 > 综合 > 作业

”excel vba 有4个数2,2,4,5 用循环相互对比,得出这4个数中有3个互不相同的数的个数是3

来源:学生作业帮 编辑:拍题作业网作业帮 分类:综合作业 时间:2024/04/27 19:28:22
”excel vba 有4个数2,2,4,5 用循环相互对比,得出这4个数中有3个互不相同的数的个数是3
Sub createdictionary()
arr = Array(2,2,4,5)
Set dic = CreateObject("scripting.dictionary")
For Each a In arr
If Not dic.exists(a) Then dic.Add a,""
Next
MsgBox "这一组数据中不重复的数据个数为" & dic.Count & "个"
End Sub
再问: 如果4列单元格,很多行的话,而且有的单元格里面还没有数据,例如“ ” “ ” “ ” “ ”这种情况是0,(2,2,4,“ ”)这种就输出2,麻烦了
再答: Sub createdictionary() on error resume next arr = selection.value'请先选中需要统计的区域 Set dic = CreateObject("scripting.dictionary") For Each a In arr If a"" and Not dic.exists(a) Then dic.Add a, "" Next MsgBox "所选单元格区域中不重复的数据个数为" & dic.Count & "个" End Sub
再问: 谢谢你 高手, 我如果想要一行一输出个数呢, 例如第一行5 5 5 6 输出2 , 第二行5 8 9 7 输出4 , 第三行85空空输出2, 就在选中的区域的后一列中输出。“空出现的时候只会在数字后面,不会在数字中间的” 谢谢您了
再答: 给你一个自定义函数吧: Function DISTINCT(ByVal rng As Variant) Dim a Set dic = CreateObject("scripting.dictionary") For Each a In rng.Value If a "" And Not dic.exists(a) Then dic.Add a, "" Next DISTINCT = dic.Count End Function 如果是求A1:D1单元格中不重复的数据个数,公式就写成: =DISTINCT(A1:D1)