You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Excel内置函数统计多列数组中的不同数字数量?

统计Excel中一行多列数组的不同数字数量

我有一个包含多列的数组,其中的整数范围为1到99,希望统计该数组中不同数字的数量。这看似简单,但我没找到能用的Excel内置函数,于是自己编写了一个用户自定义函数来实现这个功能。

示例

  • 3 3 1 => 2个不同数字(1、3)
  • 2 2 2 => 1个不同数字
  • 5 4 3 => 3个不同数字

以下是我编写的用户自定义函数,虽然用了3个循环略显复杂,但可以正常运行:

' 选择一行多列的单元格区域。
Function disnr(sel As Range) As Byte
Dim i     As Byte
Dim col   As Byte
Dim max   As Byte
Dim val   As Byte
Dim arr() As Variant

col = sel.Columns.Count
For i = 1 To col                             ' Loop1: 获取最大值
   If sel(1, i) > max Then max = sel(1, i)
Next i
ReDim arr(1 To max)

For i = 1 To col                             ' Loop2: 统计出现次数
   val = sel(1, i)
   arr(val) = arr(val) + 1
Next i

For i = 1 To max                             ' Loop3: 统计不同数字数量
   If arr(i) > 0 Then disnr = disnr + 1
Next i
End Function

内容的提问来源于stack exchange,提问作者Gecko

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.03 18:44:55