如何使用VB6返回Array中最后一次出现的值

时间:2017-03-31 19:08:18

标签: arrays vb6

您需要返回数组中最后一次出现值的索引。 我尝试了这个,但它没有工作。我做错了什么?

不是最后的索引..我正在寻找数组中包含的特定值的最后一个索引。在这种情况下,它应该返回3,它将是值的最后一个索引"这个"

在基于零的数组中,这应该返回3

  Dim ary() As Variant
  Dim  stg as string
  stg = "this,one,is,this,going,to,be,fun"
  ary = Split(stg, ",")

  MsgBox (ary.LastIndexOf(ary, "this"))

1 个答案:

答案 0 :(得分:2)

你可以试试这个。我从Variant数组更改为string,并创建了FindLast函数。

Sub test()

  Dim ary() As String
  Dim stg As String
  Dim i As Integer

  stg = "this,one,is,this,going,to,be,fun"
  ary = Split(stg, ",")

  MsgBox FindLast(ary, "this")

End Sub

Private Function FindLast(ary() As String, value As String) As Integer

  For i = UBound(ary) To 0 Step -1
    If ary(i) = value Then
        FindLast = i
        Exit For
    End If
  Next

End Function