[算表] Excel VBA變數字元限制

作者: home0303 (Blue night)   2019-01-22 11:22:51
軟體:EXCEL
版本:2010
因有同事需要 我協助寫了一個自訂函數
目的是取得不重複項目個數
Function NoDuplicateCount(範圍) '統計不重複個數
字串 = "^"
For Each cell In 範圍
If InStr(字串, cell) = 0 Then 字串 = 字串 & "^" & cell.Value
Next
If 字串 = "^" Then NoDuplicateCount = 0: Exit Function
NoDuplicateCount = Len(字串) - Len(Application.Substitute(字串, "^", "")) - 1
End Function
一開始還可順利運作 但資料一多發現就錯了
我用msgbox抓了一下 發現是因為 字串 在某處就停止了
我猜應該是VBA對於變數的容量已經到了極限
那請問類似的目的有何其他的解法呢??
謝謝
作者: soyoso (我是耀宗)   2019-01-22 11:47:00
設個變數累加,以工作表函數countif計數,為1時累加1最後回傳該變數值或以要用dictionary方式,不存在exists時寫入任一數字,存在時改寫入個文字,最後工作表函數count該變數的項目集合
作者: home0303 (Blue night)   2019-01-22 12:51:00
謝謝 Countif的方式我可以理解 就用這個來解

Links booklink

Contact Us: admin [ a t ] ucptt.com