|
こんなのでは?
Option Explicit
Public Sub Test_2()
Dim MyD As Object
Dim i As Long
Dim TBL As Variant
Dim MyVal As Variant
Dim MyKey As Variant
' Dim MyItem As Variant
' Dim MyVal2 As Variant
'Dictionaryのインスタンスを取得
Set MyD = CreateObject("Scripting.Dictionary")
With Workbooks("ブック1.xls").Worksheets("Sheet1")
'"ブック1.xls"のSheet1のA2:Dnまでを配列に取得
TBL = .Range(.Range("A2"), .Range("A" & Rows.Count).End(xlUp)).Resize(, 4).Value
'配列先頭行〜最終行まで繰り返し
For i = 1 To UBound(TBL, 1)
' MyVal = TBL(i, 2) & "_" & TBL(i, 4)
If Not TBL(i, 1) = Empty Then
If Not MyD.Exists(TBL(i, 1)) Then
'B列、C列を配列に代入
MyVal = Array(TBL(i, 2), TBL(i, 4))
'Dictionaryに登録
MyD.Add TBL(i, 1), MyVal
End If
End If
Next i
End With
'↑ここまで、配列に格納
'↓ここから、よく分かりません
' MyKey = MyD.keys
' MyItem = MyD.items
' ThisWorkbook.Activate
' With ThisWorkbook.Worksheets("Sheet1")
With Workbooks("ブック2.xls").Worksheets("Sheet1")
For i = 1 To .Range("A" & Rows.Count).End(xlUp).Row
MyKey = .Cells(i, 1).Value
If MyD.Exists(MyKey) Then
' MyVal2 = Split(MyItem(i), "_")
' Range("B" & i).Value = MyVal2(0)
' Range("C" & i).Value = MyVal2(1)
.Cells(i, 2).Resize(, 2).Value = MyD.Item(MyKey)
End If
Next i
End With
End Sub
|
|