如何通过索引位置返回数组中的元素值?

时间:2019-05-02 02:27:30

标签: excel vba

我无法打印错误消息。 问题-需要StrArray和ConArray元素的位置值。

Dim RMSIND As Integer, STRIND As Integer
Dim lRow As Long
Dim arra As Variant
Dim StrArray As Variant
Dim ConArray As Variant
Dim i As Long
Dim lColumn As Long
Dim ColAdd1 As Long
Dim ColAdd2 As Long
Dim Col_1 As Range
Dim Col_2 As Range
Dim rng1 As Range
Dim rng2 As Range
Dim pos

 Set Col_1 = ActiveSheet.Rows(1).Find("Scheme")
 ColAdd1 = Col_1.Column

Set rng1 = ActiveSheet.Range(Cells(2, ColAdd1), Cells(lRow, ColAdd1))
For Each Cell In rng1
  If Cell.Value = "RMS IND" Then
          arra = VBA.Array(1001, 1011, 1021, 1031, 1041, 1051, 2001, 2011, 3001, 3011, 3021, 3031, 3041, 3051, 3061, 4001, 4011, 4021, 4031, 5001, 5002, 5003, 5011, 5021, 5022, 5023, 5031, 5032, 5033, 6001, 6011, 6012, 6013, 6021, 6031, 6032, 6033, 6041, 6042, 6043, 7001, 7011, 7021, 7031, 7041, 7051, 7061, 7071, 7081, 7082, 7083)
          RMSIND = Cell.Offset(0, 1).Value
            pos = Application.Match(RMSIND, arra, False)
            If Not IsError(pos) Then
                StrArray = VBA.Array(16, 20, 10, 18, 14, 14, 15, 24, 33, 23, 42, 24, 34, 30, 25, 15, 8, 8, 15, 15, 15, 15, 44, 15, 15, 15, 15, 15, 15, 100, 10, 10, 10, 40, 100, 100, 100, 100, 100, 100, 11, 40, 25, 62, 61, 100, 85, 0, 75, 75, 75)
                ConArray = VBA.Array(84, 80, 90, 82, 86, 86, 85, 76, 67, 77, 58, 76, 66, 70, 75, 85, 92, 92, 85, 85, 85, 85, 56, 85, 85, 85, 85, 85, 85, 0, 90, 90, 90, 60, 0, 0, 0, 0, 0, 0, 89, 60, 75, 38, 39, 0, 15, 100, 25, 25, 25)
                 i = StrArray(pos)
                 MsgBox (i)
'Unable to print i getting error msg.
'Q- Need StrArray and ConArray elements value at position of pos.

'Unable to print i getting error msg.
'Q- Need StrArray and ConArray elements value in an array by position of pos.

1 个答案:

答案 0 :(得分:0)

Match()返回基于 1的索引,但是您的数组基于基于零的,因此,如果匹配发生在{{1 }},然后arra将超出pos的范围(即,将是+1的ubound)

...即使您没有收到错误,从StrArray返回的值也将是错误的

一个较小的版本来说明:

StrArray
相关问题