设置数组值时键入不匹配13错误13

时间:2018-11-27 20:42:24

标签: excel vba excel-vba

我不确定发生了什么,尽管它只是一个简单的代码。

基本上,我只是基于单元格值是否为True创建一个数组列表。但是将数组设置为单元格值时出现错误。我错过了什么吗?

Sub test()
    Dim chklst() As String
    Dim i As Long

    With Worksheets("Select")
        For i = 1 To 10
            If .Cells(14 + i, 12).Value = "True" Then
                chklst() = .Cells(14 + i, 13).Value 'This is where the error shows up
            End If
        Next i
    End With
End Sub

添加手表时,发现.Cells(14 + i, 13).Value是变量/字符串。将变量更改为Variant无效。将.Value更改为.Text也不起作用。.帮助。

1 个答案:

答案 0 :(得分:3)

您尚未声明数组的大小。没有声明尺寸,没有地方放任何东西。

Sub test()

    With Worksheets("Select")

        Dim chklst() As String
        ReDim chklst(1 To Application.CountIf(.Range(.Cells(15, 12), .Cells(24, 12)), "True"))


        Dim i As Long
        For i = 1 To 10
            If .Cells(14 + i, 12).Value = "True" Then
                chklst(i) = .Cells(14 + i, 13).Value 'This is where the error shows up
            End If
        Next i
    End With
End Sub
相关问题