字典后期绑定中的VBA Excel字典

时间:2018-08-02 09:04:53

标签: excel vba excel-vba dictionary

以下代码在调试语句上失败

Sub Tets()
Dim cl_data As Object
Set cl_data = CreateObject("Scripting.Dictionary")
Dim row As Object

Dim irow As Long
For irow = 11 To 12
    Set row = CreateObject("Scripting.Dictionary")
    With row
        row.Add "YN", Cells(irow, 2).Value
        row.Add "Comment", Cells(irow, 3).Value
    End With
    cl_data.Add Cells(irow, 1).Value, row
Next irow
Debug.Print cl_data(CStr(Cells(irow, 1)))("YN")
End Sub

enter image description here

我正在托盘中保存A,B和C列中的数据。“外部字典应该以A列中的值作为键,而内部是另一个字典,其中b列中的数据用键“ YN”保存并且数据从c列中保存,键为“注释”。

2 个答案:

答案 0 :(得分:1)

尝试以 row 作为对象数组,并在退出循环后将irow重置为一定范围内的某个值。

Sub Tets()

    Dim irow As Long, cl_data As Object, row(11 To 12) As Object

    Set cl_data = CreateObject("Scripting.Dictionary")

    For irow = 11 To 12
        Set row(irow) = CreateObject("Scripting.Dictionary")
        With row(irow)
            .Add "YN", Cells(irow, 2).Value
            .Add "Comment", Cells(irow, 3).Value
        End With
        cl_data.Add Key:=Cells(irow, 1).Value, Item:=row(irow)
    Next irow

    irow = 11
    Debug.Print cl_data(CStr(Cells(irow, 1)))("YN")
    irow = 12
    Debug.Print cl_data(CStr(Cells(irow, 1)))("YN")

End Sub

答案 1 :(得分:1)

这里的问题是循环之后。

$validator = Validator::make($request->only('ad_id', 'nomination_id'), [
    'ad_id' => 'required|exists:ads,id,active,1',
    'nomination_id' => 'required|exists:nominations,id,ad_id,' . $request->ad_id,
]);

if ($validator->fails()) {
    ...
}

For irow = 11 To 12 '… Next irow 返回Debug.Print irow。而且这不在您的字典中,因为您只能读取1311行。