VBA:操纵2维数组

时间:2015-07-10 05:46:49

标签: arrays excel vba excel-vba

我是VBA的新手,我有一个关于二维数组的问题。我有一个二维数组,其数据如下:

Dim MyArray(10, 10) As String ' array definition should be 10, 10 as you 10 by 10 elements

MyArray(1, 0) = A
MyArray(2, 0) = A
MyArray(3, 0) = A
MyArray(4, 0) = J
MyArray(5, 0) = J
MyArray(6, 0) = J
MyArray(7, 0) = A
MyArray(8, 0) = T
MyArray(9, 0) = T
MyArray(10, 0) = J

MyArray(0, 1) = b
MyArray(0, 2) = b
MyArray(0, 3) = g
MyArray(0, 4) = k
MyArray(0, 5) = k
MyArray(0, 6) = b
MyArray(0, 7) = g
MyArray(0, 8) = u
MyArray(0, 9) = u
MyArray(0, 10) = u

所以数据看起来像这样:

[A, b]
[A, b]
[A, g]
[J, k]
[J, k]
[J, b]
[A, g]
[T, u]
[T, u]
[J, u]

使用这些数据,我想遍历第二列,如果前面的字符和当前字符相同,则删除第一列中的相应字符。 这就是我希望数组看起来像:

[A, b]
["", b]
[A, g]
[J, k]
["", k]
[J, b]
[A, g]
[T, u]
["", u]
["", u]

任何人都可以给我一个指向正确方向的指针,用于实现此目的的循环算法吗?

3 个答案:

答案 0 :(得分:2)

请参阅下面的代码以显示您的10 x 2数据,一旦您了解了循环,您就可以进行检查,玩得开心!

Option Explicit

Sub ArraysExample()
    'Dim MyArray(10, 2) As String ' This is actually 11 rows by 3 coulms
    Dim MyArray(9, 1) As String ' This is what you should have for 10 by 2
    Dim rMax As Long, cMax As Long ' Max of Rows and Columns
    Dim r As Long, c As Long ' Rows and Columns
    Dim sTmp As String ' Temporary text

    MyArray(0, 0) = "A"
    MyArray(1, 0) = "A"
    MyArray(2, 0) = "A"
    MyArray(3, 0) = "J"
    MyArray(4, 0) = "J"
    MyArray(5, 0) = "J"
    MyArray(6, 0) = "A"
    MyArray(7, 0) = "T"
    MyArray(8, 0) = "T"
    MyArray(9, 0) = "J"

    MyArray(0, 1) = "b"
    MyArray(1, 1) = "b"
    MyArray(2, 1) = "g"
    MyArray(3, 1) = "k"
    MyArray(4, 1) = "k"
    MyArray(5, 1) = "b"
    MyArray(6, 1) = "g"
    MyArray(7, 1) = "u"
    MyArray(8, 1) = "u"
    MyArray(9, 1) = "u"

    rMax = UBound(MyArray)
    cMax = UBound(Application.Transpose(MyArray)) - 1 ' Transposed are 1 based, so you need to subtract 1

    ' To Display data visually
    For r = 0 To rMax
        sTmp = "["
        For c = 0 To cMax
            sTmp = sTmp & MyArray(r, c)
            If c = cMax Then
                sTmp = sTmp & "]"
            Else
                sTmp = sTmp & ", "
            End If
        Next
        Debug.Print sTmp
    Next
End Sub

答案 1 :(得分:1)

试试这个

For i =1 to 9 'exclude first element
    if MyArray(0, i) = MyArray(0, i-1) then
        MyArray(i, 0) = ""
    end if
debug.print i & ', 0: is ' & MyArray(i, 0)
next

答案 2 :(得分:1)

查看通过Debug.Print语句发送到VBE的立即窗口(Ctrl + G)的结果是否有助于您理解这一切。

Sub arrTest()
    Dim vARRs As Variant, v As Long, w As Long

    ReDim vARRs(0 To 9, 0 To 1)
    Debug.Print LBound(vARRs, 1) & ":" & UBound(vARRs, 1)
    Debug.Print LBound(vARRs, 2) & ":" & UBound(vARRs, 2)
    Debug.Print "---"

    vARRs(0, 0) = Chr(65)
    vARRs(1, 0) = Chr(65)
    vARRs(2, 0) = Chr(65)
    vARRs(3, 0) = Chr(74)
    vARRs(4, 0) = Chr(74)
    vARRs(5, 0) = Chr(74)
    vARRs(6, 0) = Chr(65)
    vARRs(7, 0) = Chr(84)
    vARRs(8, 0) = Chr(84)
    vARRs(9, 0) = Chr(74)

    vARRs(0, 1) = Chr(98)
    vARRs(1, 1) = Chr(98)
    vARRs(2, 1) = Chr(103)
    vARRs(3, 1) = Chr(107)
    vARRs(4, 1) = Chr(107)
    vARRs(5, 1) = Chr(98)
    vARRs(6, 1) = Chr(103)
    vARRs(7, 1) = Chr(117)
    vARRs(8, 1) = Chr(117)
    vARRs(9, 1) = Chr(117)

    'show data before culling
    For v = LBound(vARRs, 1) To UBound(vARRs, 1)
        Debug.Print vARRs(v, 0) & " - " & vARRs(v, 1)
    Next v
    Debug.Print "---"

    For v = LBound(vARRs, 1) + 1 To UBound(vARRs, 1)
        If vARRs(v, 1) = vARRs(v - 1, 1) Then _
            vARRs(v, 0) = vbNullString
    Next v

    'show data after culling
    For v = LBound(vARRs, 1) To UBound(vARRs, 1)
        Debug.Print vARRs(v, 0) & " - " & vARRs(v, 1)
    Next v

End Sub

结果:

arrTest
0:9
0:1
---
A - b
A - b
A - g
J - k
J - k
J - b
A - g
T - u
T - u
J - u
---
A - b
 - b
A - g
J - k
 - k
J - b
A - g
T - u
 - u
 - u

正如您所看到的,我更喜欢变体数组,但方法几乎相同。使用LBound functionUBound function有助于跟踪两个排名中数组的范围,无论您使用的是基于维度还是基于零的维度。