VBAでDataTableに似たものを利用したいと思っていろいろと調べているとRecordsetがいい感じとのことだったので調べて必要そうなところを抜粋してメモしている。
Private Sub Sample()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
'▽Recordse定義
Dim myRS As ADODB.Recordset
Set myRS = New ADODB.Recordset
With myRS.Fields
.Append "商品番号", adVarWChar, -1
.Append "番号", adVarWChar, -1
End With
myRS.Open
'▽データ取り込み-----------------------------------------
Dim i As Long
Dim FieldList As Variant: FieldList = Array("商品番号", "番号")
i = 2
Do
myRS.AddNew FieldList, CreateArrayData(Sheet2.Range("A" & i & ":B" & i).Resize(1, 2).Value)
i = i + 1
Loop Until IsEmpty(Sheet2.Cells(i, 1))
myRS.MoveFirst
'Recordsetデータ検索-----------------------------------------
i = 2
Do
myRS.Filter = "商品番号 = '" & Sheet1.Cells(i, 1).Value & "'"
Sheet1.Cells(i, 2) = myRS!番号
i = i + 1
If i Mod 1000 = 0 Then
End If
Loop Until IsEmpty(Sheet1.Cells(i, 1))
'閉じる
Dim FieldList As Variant: FieldList = Array("商品番号", "番号")
i = 2
Do
myRS.AddNew FieldList, CreateArrayData(Sheet2.Range("A" & i & ":B" & i).Resize(1, 2).Value)
i = i + 1
Loop Until IsEmpty(Sheet2.Cells(i, 1))
myRS.MoveFirst
'Recordsetデータ検索-----------------------------------------
i = 2
Do
myRS.Filter = "商品番号 = '" & Sheet1.Cells(i, 1).Value & "'"
Sheet1.Cells(i, 2) = myRS!番号
i = i + 1
If i Mod 1000 = 0 Then
End If
Loop Until IsEmpty(Sheet1.Cells(i, 1))
'閉じる
myRS.Close
Set myRS = Nothing
Set myRS = Nothing
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
Application.Calculation = xlCalculationAutomatic
End Sub
Function CreateArrayData(SecDimArray As Variant) As Variant
Dim myVar As Variant
Dim i As Long
ReDim myVar(0 To UBound(SecDimArray, 2) - 1)
For i = 0 To UBound(myVar)
myVar(i) = SecDimArray(1, i + 1)
Next
CreateArrayData= myVar
End Function