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