输入不匹配的vba 2d数组

时间:2017-04-03 03:08:10

标签: vba excel-vba excel

strData = "{""INTEREST EARNING ASSETS"", """";" & _
        """Interbank Placement"", ""goku"";" & _
        """Corporate Bonds"", ""goku"";" & _
        """Government Bonds and T-Bills"", ""goku"";" & _
        """Customer Loans"", ""cow"";" & _
        """"", ""soccer"";" & _
        """Customer Loans"", ""cow"";" & _
        """Customer Loans"", ""cow"";" & _
        """test"", 444444;" & _
        """drinks"", ""goal""}"

    varData = Evaluate(strData)

    For intCounter1 = 1 To UBound(varData, 1)
        For intCounter2 = 1 To UBound(varData, 2)
            ActiveCell.Value = varData(intCounter1, intCounter2)
            ActiveCell.Offset(0, 1).Select
        Next intCounter2
        ActiveCell.Offset(0, -2).Select
        ActiveCell.Offset(1, 0).Select
    Next intCounter1

上面的代码有效,它打印出给定值的2列。例如

INTEREST EARNING ASSETS *BLANK*
Interbank Placement      goku 
Corporate bonds          goku

等等

但是,当我在strData的第3行添加额外的行时,如下所示,它表示类型不匹配。我真的不明白为什么?事实上,如果我在下面的任何一行添加一条额外的行,它会给我同样的错误。为什么会这样?

strData = "{""INTEREST EARNING ASSETS"", """";" & _
            """Interbank Placement"", ""goku"";" & _
            """Interbank Placement"", ""goku"";" & _
            """Corporate Bonds"", ""goku"";" & _
            """Government Bonds and T-Bills"", ""goku"";" & _
            """Customer Loans"", ""cow"";" & _
            """"", ""soccer"";" & _
            """Customer Loans"", ""cow"";" & _
            """Customer Loans"", ""cow"";" & _
            """test"", 444444;" & _
            """drinks"", ""goal""}"

1 个答案:

答案 0 :(得分:0)

使用Range().Resize(n,m).Value =

使用表/数组的直接值赋值
Dim varData() as Variant
ReDim varData(1 to 4, 1 to 2)

varData(1,1) = "INTEREST EARNING ASSETS"
varData(1,2) = "*BLANK*"
varData(2,1) = "Interbank Placement"
varData(2,2) = 1.2344#
varData(3,1) = "Corporate bonds"
varData(3,2) = 1.04305#
varData(4,1) = "Customer Loans"
varData(4,2) = 1.10383#

ActiveCell.Resize(4,2).Value = varData

PS。永远不需要使用select语句来赋值。只需选择正确的单元格引用并直接使用它来读/写值。