简体   繁体   English

Excel for Mac 2011:UBound() 不工作

[英]Excel for Mac 2011: UBound() Not Working

I'm working on making an existing macro-enabled spreadsheet functional on Excel for Mac 2011.我正在努力使现有的启用宏的电子表格在 Excel for Mac 2011 上运行。

I have a function ( Source ) that searches arrays for a specified value:我有一个函数 ( Source ) 可以在数组中搜索指定值:

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

It works perfectly in Excel 2013, but on Excel for Mac 2011, I receive the error:它在 Excel 2013 中完美运行,但在 Excel for Mac 2011 上,我收到错误消息:

Runtime error '9': Subscript out of range

I broke it apart and found that the UBound call is what's causing the error.我把它拆开,发现 UBound 调用是导致错误的原因。

I'd like to change as little as possible for maintainability.为了可维护性,我想尽可能少地改变。 How can I fix this error for the Mac version?如何修复 Mac 版本的此错误?

Thanks in advance for any replies!提前感谢您的任何答复!

Edit: @Siddharth Rout's solution is spot on, but since I was searching arrays within a loop, I had to modify the loop to reset the array between each iteration as follows (in case anyone else runs into the same issue!):编辑: @Siddharth Rout 的解决方案是正确的,但是由于我在循环中搜索数组,因此我必须修改循环以按如下方式在每次迭代之间重置数组(以防其他人遇到相同的问题!):

' --- START Reset Array for OS X ---
Dim OS_X_Hack(99) As String

For intIndex = 0 To 99
    OS_X_Hack(intIndex) = Original(intIndex)
Next

Erase Original()
ReDim Original(0 To 99) As String

For intIndex = 0 To 99
    Original(intIndex) = OS_X_Hack(intIndex)
Next

Erase OS_X_Hack()
' --- END Reset Array for OS X ---

Ok This is my observation.好的,这是我的观察。 If you call the function once in a procedure then it will work fine.如果您在一个过程中调用一次该函数,那么它将正常工作。 For Example例如

Sub Sample()
    Dim a As Variant
    Dim s As String
    Dim strTemp As String

    s = "CC"
    strTemp = "A,B,C,D"

    a = Split(strTemp, ",")

    Debug.Print IsInArray(s, a)
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

在此处输入图片说明

However if you call it twice in the procedure then you will get an error Runtime error '9': Subscript out of range .但是,如果您在该过程中调用它两次,则会收到错误Runtime error '9': Subscript out of range Maybe it is an Excel 2011 Bug?也许这是一个 Excel 2011 错误?

Sub Sample()
    Dim a As Variant
    Dim s As String
    Dim strTemp As String

    s = "CC"
    strTemp = "A,B,C,D"

    a = Split(strTemp, ",")

    Debug.Print IsInArray(s, a)

    s = "A"
    Debug.Print IsInArray(s, a)
End Sub

在此处输入图片说明

Solution解决方案

Recreate the array.重新创建数组。 See this example.请参阅此示例。

Sub Sample()
    Dim a As Variant
    Dim s As String
    Dim strTemp As String

    s = "CC"
    strTemp = "A,B,C,D"

    a = Split(strTemp, ",")
    Debug.Print IsInArray(s, a)

    s = "A"
    a = Split(strTemp, ",")
    Debug.Print IsInArray(s, a)
End Sub

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
    IsInArray = (UBound(Filter(arr, stringToBeFound)) > -1)
End Function

在此处输入图片说明

Credit for this solution goes to this answer by Brian Hinchey .这个解决方案的功劳归功于 Brian Hinchey 的这个答案 Using the code below, I'm able to call IsInArray within a loop in Excel for Mac 2011.使用下面的代码,我可以在 Excel for Mac 2011 的循环中调用 IsInArray。

Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
  IsInArray = Not IsError(Application.Match(stringToBeFound, arr, 0))
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM