插入多维变量数组 VBA MS Access

Inserting in multidimensional variant array VBA MS Access

我正在尝试根据我在 google 上找到的教程将数据插入 VBA 中的 二维变体数组

这是我的代码:

    Dim sampleVariant As Variant
    ReDim sampleVariant(3, 3)

    For sample = 0 To UBound(sampleVariant, 1)
        For information_e = 0 To UBound(sampleVariant, 2)
            sampleVariant(sample, information_e) = "Name ~row:" & sample & " ~column:" & information_e
        Next
    Next

结果是

Name ~row:1 ~column:1,Name ~row:1 ~column:2,Name ~row:1 ~column:3
Name ~row:2 ~column:1,Name ~row:2 ~column:2,Name ~row:2 ~column:3
Name ~row:3 ~column:1,Name ~row:3 ~column:2,Name ~row:3 ~column:3

怎样才能让它像

Name ~row:1 ~column:1,Age ~row:1 ~column:2,Address ~row:1 ~column:3
Name ~row:2 ~column:1,Age ~row:2 ~column:2,Address ~row:2 ~column:3
Name ~row:3 ~column:1,Age ~row:3 ~column:2,Address ~row:3 ~column:3

我认为理解这一点的最好方法是实际查看它是如何做到的。你并没有在你的 post 中提供很多上下文,所以我只是猜测你只是不明白实际发生了什么。

那么,(x,y) 是您的多维数组,x 是行,y 是列,对吗?

因此,如果您要手动为它们分配值,您会这样做(安排在视觉上有意义,请不要使用命令分隔符)。

arr(0,0)="this": arr(0,1)="that" : arr(0,2)="other"
arr(1,0)="this": arr(1,1)="that" : arr(1,2)="other"
arr(2,0)="this": arr(2,1)="that" : arr(2,2)="other"

这是循环为它们赋值的顺序,从上到下,从左到右。我认为这里应该指出的另一件事是:

from 0 to UBOUND(array, dimension)

我觉得这是偷懒。我认为最好完全定义如:

from LBOUND(array, dimension) to UBOUND(array, dimension)

但这完全取决于你。

希望对您有所帮助。