那個 VBA 迪西這兩天找了一個範例,僅只能使用一組 連續參照的範例,
被迪西意外想到以某種方式實現,在 (值) 的輸入上可以像 SUM () 一樣
有彈性的使用了。
初步已在 Excel 2003/2010 上面跑過 ... 效果對不對就 ... 再討論
COUNT_NRN 以 (數值) 為條件的統計計算方式,功能、使用方式跟 COUNT 差不多
語法:
Function COUNT_NRN(ParamArray Value() As Variant)
'═════════════════════════
' COUNT_NRN ver. 1a
' 動作1:資料選擇範圍
'
' 以 (數值) 為條件的計算資料出現的次數,
' 當有重複的(值)時後,該值只計算一次。
' 使用方式、功能類似於 COUNT 內建函數。
'
' Value:可以是 (數值) 、單一參照值、連續參照值,
' 數量尚最少 1 個,多個資料時,以 "," 區分。
'═════════════════════════
Dim i, j, Count, arg, Value_new() As Variant
On Error Resume Next
Count = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Count = Count + 1
Next arg
Next i
Err.Clear
ReDim Value_new(Count - 1)
j = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Value_new(j) = arg
j = j + 1
Next arg
If Err.Number = 92 Then Value_new(j - 1) = Value(i)
Next i
Err.Clear
For i = LBound(Value_new) To UBound(Value_new)
If WorksheetFunction.IsNumber(Value_new(i)) = False Then Value_new(i) = ""
If i = UBound(Value_new) Then Exit For
For j = i + 1 To UBound(Value_new)
If Value_new(i) = "" Then Exit For
If Value_new(i) = Value_new(j) Then Value_new(j) = ""
Next j
Next i
Count = 0
For Each arg In Value_new
If arg <> "" Then Count = Count + 1
Next arg
COUNT_NRN = Count
End Function
COUNT_NRA 以 (字串) 為條件的統計計算方式,功能、使用方式跟 COUNTA 差不多
語法:
Function COUNT_NRA(ParamArray Value() As Variant)
'═════════════════════════
' COUNT_NRA ver. 1a
' 動作1:資料選擇範圍
'
' 以 (字串) 為條件的計算資料出現的次數,
' 當有重複的(值)時後,該值只計算一次。
' 使用方式、功能類似於 COUNTA 內建函數。
'
' Value:可以是 (字串) 、單一參照值、連續參照值,
' 數量尚最少 1 個,多個資料時,以 "," 區分。
'═════════════════════════
Dim i, j, Count, arg, Value_new() As Variant
On Error Resume Next
Count = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Count = Count + 1
Next arg
Next i
Err.Clear
ReDim Value_new(Count - 1)
j = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Value_new(j) = arg
j = j + 1
Next arg
If Err.Number = 92 Then Value_new(j - 1) = Value(i)
Next i
Err.Clear
For i = LBound(Value_new) To UBound(Value_new)
If WorksheetFunction.IsNonText(Value_new(i)) Then Value_new(i) = ""
If i = UBound(Value_new) Then Exit For
For j = i + 1 To UBound(Value_new)
If Value_new(i) = "" Then Exit For
If Value_new(i) = Value_new(j) Then Value_new(j) = ""
Next j
Next i
Count = 0
For Each arg In Value_new
If arg <> "" Then Count = Count + 1
Next arg
COUNT_NRA = Count
End Function
COUNT_NR2 以 (數值 + 字串) 為條件的統計計算方式,功能、使用方式跟 COUNT、或 COUNTA 差不多
語法:
Function COUNT_NR2(ParamArray Value() As Variant)
'═════════════════════════
' COUNT_NR2 ver. 1a
' 動作1:資料選擇範圍
'
' 以 (數值 + 字串) 為條件的計算資料出現的次數,
' 當有重複的(值)時後,該值只計算一次。
' 使用方式、功能類似於 COUNT、或 COUNTA 內建函數。
'
' Value:可以是 (數值) 或 (字串)、單一參照值、連續參照值,
' 數量尚最少 1 個,多個資料時,以 "," 區分。
'═════════════════════════
Dim i, j, Count, arg, Value_new() As Variant
On Error Resume Next
Count = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Count = Count + 1
Next arg
Next i
Err.Clear
ReDim Value_new(Count - 1)
j = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Value_new(j) = arg
j = j + 1
Next arg
If Err.Number = 92 Then Value_new(j - 1) = Value(i)
Next i
Err.Clear
For i = LBound(Value_new) To UBound(Value_new)
If WorksheetFunction.IsLogical(Value_new(i)) Then Value_new(i) = ""
If i = UBound(Value_new) Then Exit For
For j = i + 1 To UBound(Value_new)
If Value_new(i) = "" Then Exit For
If Value_new(i) = Value_new(j) Then Value_new(j) = ""
Next j
Next i
Count = 0
For Each arg In Value_new
If arg <> "" Then Count = Count + 1
Next arg
COUNT_NR2 = Count
End Function
COUNT_NRS 以 (數值 + 字串) 為條件的統計計算方式,以 (模式) 選擇條件的統計計
算方式,基本用法 OUNT_NRS(模式,數值,數值,…)
模式一定要有,不可以略過 0~2
0 表示以(數值)計算。
1 表示以(字串)計算。
2 表試以(數值 + 字串)計算
後面的 數值 部份則可以如 SUM 一樣的選取。
語法:
Function COUNT_NRS(ByVal mode As Byte, ParamArray Value() As Variant)
'═════════════════════════
' COUNT_NRS ver. 1b
' 動作1:資料選擇範圍
'
' 以 (模式) 選擇條件的計算資料出現的次數,
' 當有重複的(值)時後,該值只計算一次。
' 使用方式、功能類似於 COUNTIF 內建函數。
' 引數的使用方式 類似於 函數(模式,值,值,…)
' Mode:
' 0 表示以(數值)計算。COUNT_NRS(0,A1:B8,111,566)
' 1 表示以(字串)計算。COUNT_NRS(1,A1:B8,C1,D5,"ABC","AAV")
' 2 表試以(數值 + 字串)計算。COUNT_NRS(2,A1:B8,C1,D5,111,"AAV")
' Value:可以是 (數值) 或 (字串)、單一參照值、連續參照值,
' 數量尚最少 1 個,多個資料時,以 "," 區分。
'═════════════════════════
Dim i, j, Count, arg, Value_new() As Variant
On Error Resume Next
Count = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Count = Count + 1
Next arg
Next i
Err.Clear
ReDim Value_new(Count - 1)
j = 0
For i = LBound(Value) To UBound(Value)
Err.Clear
For Each arg In Value(i)
Value_new(j) = arg
j = j + 1
Next arg
If Err.Number = 92 Then Value_new(j - 1) = Value(i)
Next i
Err.Clear
For i = LBound(Value_new) To UBound(Value_new)
Select Case mode
Case 0
GoTo term_1
Case 1
GoTo term_2
Case 2
GoTo term_3
Case Else
GoTo term_3
End Select
term_1:
If WorksheetFunction.IsNumber(Value_new(i)) = False Then Value_new(i) = ""
GoTo term_Next
term_2:
If WorksheetFunction.IsNonText(Value_new(i)) Then Value_new(i) = ""
GoTo term_Next
term_3:
If WorksheetFunction.IsLogical(Value_new(i)) Then Value_new(i) = ""
GoTo term_Next
term_Next:
If i = UBound(Value_new) Then Exit For
For j = i + 1 To UBound(Value_new)
If Value_new(i) = "" Then Exit For
If Value_new(i) = Value_new(j) Then Value_new(j) = ""
Next j
Next i
Count = 0
For Each arg In Value_new
If arg <> "" Then Count = Count + 1
Next arg
COUNT_NRS = Count
End Function