首页 > 解决方案 > 为什么将我的数组读入 VBA 中的单元格时,它会重复每一行?

问题描述

我的代码简单地写着:

Board = Array(1,2,3,4,5,6,7,8,9)
Range("A1:C3") = Board

但是当执行时,在 A1 到 C1、A2 到 C2 和 A3 到 C3 行中重复 1、2、3。我该如何解决?

标签: excelvba

解决方案


使用二维数组:

Dim Board(1 To 3, 1 To 3) As Variant 'or As Long

Dim i As Long, j As Long
For i = LBound(Board, 1) To UBound(Board, 1)
    For j = LBound(Board, 2) To UBound(Board, 2)
        Dim counter As Long
        counter = counter + 1
        Board(i, j) = counter
    Next
Next

Range("A1:C3").Value = Board

推荐阅读