簡體   English   中英

Excel VBA:“無法獲取工作表函數類的match屬性”,應存在匹配項時出錯

[英]Excel VBA: “unable to get the match property of the worksheetfunction class” Error when match should exist

因此,假設在我的情況下Match函數運行正常,則該函數正在搜索的索引存在並且應該顯示。 問題是事實並非如此。 看看其他遇到Match問題的人的帖子,似乎有一些限制。

我本質上是使用Match函數來確定新排序列表的順序,例如numsArray提供了一個列表4。我需要兩個字符串:第一個包含兩個最小的val,第二個包含兩個最大的numsArray 但是,我需要兩個字符串按照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函數,我會更喜歡

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM