奇怪的#VALUE!错误
Strange #VALUE! error
请看一下这个错误:
这里有两个函数:
Public Function CompareExtra(r1 As Range, r2 As Range) As Long
Dim r As Integer, v As Variant, v2 As Variant
Dim rr1() As String
Dim rr As Range
rr1 = Split(r1, ",")
For r = LBound(rr1) To UBound(rr1)
v = --Trim(rr1(r))
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then CompareExtra = CompareExtra + 1
Next rr
End If
Next r
End Function
Function getLeast(rng As Range, leastXValues As Long, maxValue As Long, displacement As Long, Optional Sorted As Boolean = True) As String
Dim d As Object, AL As Object
Dim i As Long, j As Long
Dim a As Variant
Set d = CreateObject("Scripting.Dictionary")
Set AL = CreateObject("System.Collections.ArrayList")
a = rng.Value
i = UBound(a, 1)
j = UBound(a, 2)
Do
If a(i, j) <> 0 Then d(a(i, j)) = a(i, j)
j = j - 1
If j = 0 Then
j = UBound(a, 2)
i = i - 1
End If
Loop Until i = 0
For i = 1 To maxValue
d(i) = i
Next i
a = d.Items()
For i = UBound(a) - displacement To UBound(a) - displacement - leastXValues + 1 Step -1
AL.Add a(i)
Next i
If Sorted Then AL.Sort
getLeast = Join(AL.ToArray, ", ")
End Function
我不明白为什么如果我从一个单独的单元格中获取 getLeast() 的结果 - 一切正常,但如果我将它直接添加到 CompareExtra(),它会抛出一个错误..
简单的解决方案是使用变体:
Public Function CompareExtra(r1 As Variant, r2 As Variant) As Long
Function getLeast(rng As Range, leastXValues As Long, maxValue As Long, displacement As Long, Optional Sorted As Boolean = True) As Variant
请看一下这个错误:
这里有两个函数:
Public Function CompareExtra(r1 As Range, r2 As Range) As Long
Dim r As Integer, v As Variant, v2 As Variant
Dim rr1() As String
Dim rr As Range
rr1 = Split(r1, ",")
For r = LBound(rr1) To UBound(rr1)
v = --Trim(rr1(r))
If v <> 0 And v <> "" Then
For Each rr In r2
v2 = rr.Value
If v = v2 Then CompareExtra = CompareExtra + 1
Next rr
End If
Next r
End Function
Function getLeast(rng As Range, leastXValues As Long, maxValue As Long, displacement As Long, Optional Sorted As Boolean = True) As String
Dim d As Object, AL As Object
Dim i As Long, j As Long
Dim a As Variant
Set d = CreateObject("Scripting.Dictionary")
Set AL = CreateObject("System.Collections.ArrayList")
a = rng.Value
i = UBound(a, 1)
j = UBound(a, 2)
Do
If a(i, j) <> 0 Then d(a(i, j)) = a(i, j)
j = j - 1
If j = 0 Then
j = UBound(a, 2)
i = i - 1
End If
Loop Until i = 0
For i = 1 To maxValue
d(i) = i
Next i
a = d.Items()
For i = UBound(a) - displacement To UBound(a) - displacement - leastXValues + 1 Step -1
AL.Add a(i)
Next i
If Sorted Then AL.Sort
getLeast = Join(AL.ToArray, ", ")
End Function
我不明白为什么如果我从一个单独的单元格中获取 getLeast() 的结果 - 一切正常,但如果我将它直接添加到 CompareExtra(),它会抛出一个错误..
简单的解决方案是使用变体:
Public Function CompareExtra(r1 As Variant, r2 As Variant) As Long
Function getLeast(rng As Range, leastXValues As Long, maxValue As Long, displacement As Long, Optional Sorted As Boolean = True) As Variant