请求VBA高手帮忙翻译一小段程序。

2025-03-04 22:56:05
推荐回答(1个)
回答1:

et d = CreateObject("Scripting.Dictionary")'创建字典
With Sheet2 'Sheet2工作表中操作
    arr = .Range(.Cells(2, 1), .Cells(.Cells(Rows.Count, 1).End(3).Row, 3))'建立数组A2:Cn,N是数据的最大行
    For i = 1 To UBound(arr)'循环数组
        If arr(i, 3) = 1 Then  '判断数组的第列的值是否为1
            d(arr(i, 1)) = arr(i, 2)'如果为1,则以数组的第一列为KEY,第二列为ITEM加到字典
        End If
    Next
End With
y = d.keys ' 将字典的KEYS赋值给Y  这句可以不用
t = d.items '将字典的KEYS赋值给t  这句可以不用
With Sheet1   '在Sheet1操作
    For i = 2 To .Cells(.Cells(Rows.Count, 1).End(3).Row, 1) '循环
        If d.Exists(.Cells(i, 1).Value) Then  '判断字典是不是存在A列数据
            .Cells(i, 2) = d(.Cells(i, 1).Value) '如果存在则对应B列的值等于字典对应的ITEM
        Else
            .Cells(i, 2) = "" '其他为空
        End If
    Next
End With