dポイントプレゼントキャンペーン実施中!

prefShのB列の値をKeyとして、A列・E列・M列の値をItemに格納し、それぞれ workShのA列・U列・C列に出力したいのですが、その場合はどのように書き換えたらよいか、ご教示いただけませんでしょうか。(prefShでは2行目から最終行まで値を取得し、workShでは3行目から値を入れたいです)

Findメソッドでエラーが起きたので、Dictionaryを初めて使い、現在はA列→A列のみ成功している状態です。↓ お知恵を貸していただけますよう、お願いいたします。

-------------------

Dim myDic As New Dictionary
Dim i As Long

With prefSh
For i = 2 To .Cells(.Rows.Count, 2).End(xlUp).Row
If Not myDic.Exists(.Cells(i, 2).Value) Then
myDic.Add .Cells(i, 2).Value, .Cells(i, 1).Value
End If
Next i
End With

Dim vKey As Variant
With workSh
i = 1
For Each vKey In myDic
i = i + 1
.Cells(i + 1, 2).Value = vKey
.Cells(i + 1, 1).Value = myDic.Item(vKey)
Next
End With
Set myDic = Nothing

A 回答 (2件)

初級者ですのでお手柔らかにお願いします。



Dim myDic As New Dictionary
Dim i As Long

With prefSh
For i = 2 To .Cells(.Rows.Count, 2).End(xlUp).Row
If Not myDic.Exists(.Cells(i, 2).Value) Then
myDic.Add .Cells(i, 2).Value, Array(.Cells(i, "A").Value , .Cells(i, "E").Value , .Cells(i, "M").Value)
End If
Next i
End With

Dim vKey As Variant
With workSh
i = 3
For Each vKey In myDic
.Cells(i, 2).Value = vKey
.Cells(i, "A").Value = myDic(vKey)(0)
.Cells(i, "U").Value = myDic(vKey)(1)
.Cells(i, "C").Value = myDic(vKey)(2)
i = i + 1
Next
End With
Set myDic = Nothing
    • good
    • 1
この回答へのお礼

ご丁寧にありがとうございました。
Dim i As Longの部分から全て使わせていただき、= myDic.Item(vKey)(0)に変更しなくても問題なくプログラムを実行することができました。
大変勉強にもなり、ありがとうございました。

お礼日時:2021/02/09 15:59

No.1です。



参照設定されてたのですね。
普段しないので見落としました。

もしダメな時は

= myDic.Item(vKey)(0)

などに変更してみて下さい。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!

このQ&Aを見た人はこんなQ&Aも見ています


このQ&Aを見た人がよく見るQ&A