【问题标题】:Excel VBA: "unable to get the match property of the worksheetfunction class" Error when match should existExcel VBA:“无法获取工作表函数类的匹配属性”当匹配应该存在时出错
【发布时间】:2014-10-09 19:43:57
【问题描述】:

因此,如果假设 Match 函数在我的情况下正常工作,则该函数正在搜索的索引存在并且应该显示出来。问题是它没有。查看其他一些遇到Match 问题的人的帖子,似乎它有一些限制。

我本质上是使用 Match 函数来确定新排序列表的顺序,例如numsArray 提供了 4 个列表。我需要两个字符串:第一个包含两个最小的 val,第二个包含两个最大的。但是,我需要这两个字符串按照 numsArray 的最低到最高索引值对它们的值进行排序。 Array_Values 包含已排序的数组,但 numsArray 包含要排序的值的索引。

不管怎样,

提供此代码:

Dim numsArray() As Double 
Dim Array_Values() As Double
Dim high() As Double
Dim low() As Double 
ReDim high(1) 
ReDim low(1)
ReDim numsArray(3)
ReDim Array_Values(3)

numsArray = (37669.1, 37343.6, 24, 16)
Array_Values = ( 16, 24, 37669.1, 37343.6) ' in reality Array_Values = SortFunction(numsArray)

'collect low()
    Dim tempI1
    Dim tempI2
    tempI1 = WorksheetFunction.Match(Array_Values(0), numsArray) '***Error Occurs here***
    tempI2 = WorksheetFunction.Match(Array_Values(1), numsArray)
    low(0) = numsArray(WorksheetFunction.Max(tempI1, tempI2))
    low(1) = numsArray(WorksheetFunction.Min(tempI1, tempI2))

'collect high()
    tempI1 = WorksheetFunction.Match(Array_Values(2), numsArray)
    tempI2 = WorksheetFunction.Match(Array_Values(3), numsArray)
    high(0) = numsArray(WorksheetFunction.Max(tempI1, tempI2))
    high(1) = numsArray(WorksheetFunction.Min(tempI1, tempI2))

如何让它工作? 如果我仍然可以使用Match 函数,我会更喜欢

【问题讨论】:

  • 我不确定 match 函数是否适用于数组。我认为它适用于 Ranges。

标签: excel vba sorting match worksheet-function


【解决方案1】:

Application.Match 应该可以代替WorksheetFunction 工作。

Sub d()
numsArray = Split("37669.1,37343.6,24,16", ",")
Array_Values = Split("16,24,37669.1,37343.6", ",") ' in reality Array_Values = SortFunction(numsArray)

'collect low()
    Dim tempI1
    Dim tempI2
    tempI1 = Application.Match(Array_Values(0), numsArray)
End Sub

但是,请注意,对数组的迭代速度最多快 10 倍:

Sub d()
numsArray = Split("37669.1,37343.6,24,16", ",")
Array_Values = Split("16,24,37669.1,37343.6", ",") ' in reality Array_Values = SortFunction(numsArray)

'collect low()
    Dim tempI1
    Dim tempI2
    tempI1 = ArrayIndex(numsArray, Array_Values(0))
End Sub
Function ArrayIndex(arr, val) as Variant
Dim i as Long
For i = lBound(arr) to uBound(arr)
    If array(i) = val then
        Arrayindex = i
        GoTO EarlyExit
    End If
Next
ArrayIndex = CVerr(2042) 'value NOT found
EarlyExit:
End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多