プロが教えるわが家の防犯対策術!

はじめまして。
中小企業の工場で働いている者です。

一月の工場の仕事の負荷を把握したのですが、
何百部品もあり、マンパワーでは把握できそうにありません。
そこで、エクセル(2010)のリストよりVBAを使って解決したいのですが、
VBA初心者の私にはプログラムを作れそうにありません。

誰かお力添え頂けませんでしょうか。

●リスト1に部品毎に負荷(作業時間h)が分かっています。
    A      B      C
1   部品名    注番      負荷(作業時間)
2   部品ア    イ        2
3   部品ウ            0.06
4   部品              0.005
・   ・              ・
・   ・              ・
・   ・              ・


●リスト2にその月の受注部品の一覧があります。
     A       B      C
1     部品名   注番    負荷(作業時間)
2   部品ア    イ    
3   部品カ     キ     
4   部品         
・   ・            
・   ・            
マクロを実行すると、
リスト2の部品ア、注番イを、リスト1から検索し、その行のC列の負荷(作業時間)を
リスト2の部品ア、注番イのC列に張り付ける。
次は、3行目、次は4行目その次は・・・と繰り返すプログラムを組みたいと思っております。
新規の部品は、リスト2にあって、リスト1にない場合もありますので、
その場合は、-で表示したいと思っております。

誰か、このVBA初心者にお力添えください。
よろしくお願いいたします。

ちなみに、ここまでは、できています。
Public Sub 負荷一覧作成()
Dim sh1, sh2 As Worksheet
Dim maxRow1 As Long
Dim maxRow2 As Long
Dim row As Long
Dim dicT As Object '連想配列(部品名の記憶)
Dim key As Variant
Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
Set sh1 = Worksheets("Sheet1") ' リスト1のシート名
Set sh2 = Worksheets("Sheet2") ' リスト2のシート名
maxRow1 = sh1.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める
maxRow2 = sh2.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める
'部品一覧を作成し、部品ごとの時間を記憶する
For row = 2 To maxRow1
key = sh1.Cells(row, 1).Value
dicT(key) = sh1.Cells(row, 2).Value
Next
'部品毎の時間を検索し設定する
For row = 2 To maxRow2
key = sh2.Cells(row, 1).Value
'部品一覧にこの部品があれば、その時間を設定
If dicT.exists(key) = True Then
sh2.Cells(row, 2).Value = dicT(key)
Else
'なければ、"-"を設定する
sh2.Cells(row, 2).Value = "-"
End If
Next
MsgBox ("処理完了")
End Sub

A 回答 (2件)

Dim ですが、確かに変数を , で繋げて宣言できますが、変数ごとに型を書かないと、variant 型での宣言になります。


Dim sh1, sh2 As Worksheet の場合は sh1 は variant 型宣言しています。

Public Sub 負荷一覧作成()
Dim sh1 as worksheet, sh2 As Worksheet
Dim maxRow1 As Long, maxRow2 As Long, RowIndex As Long, SearchIndex as Long
Dim PartsNameArray() as variant, PartNumberArray() as Variant

Set sh1 = Worksheets("Sheet1") ' リスト1のシート名
Set sh2 = Worksheets("Sheet2") ' リスト2のシート名

maxRow1 = sh1.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める
maxRow2 = sh2.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める

PartsNameArray() = Range(sh1.cells(2, 1), sh1.cells(maxrow1, 1))
PartsNumberArray() = Range(sh1.cells(2, 2), sh1.cells(maxrow1, 2))

For RowIndex = 2 to maxRow2
For SearchIndex = 2 to maxRw1
if sh1.cells(SearchIndex, 1) = sh2.cells(RowIndex, 1) and sh1.cells(SearchIndex, 2) = sh2.cells(RowIndex, 2) then
sh2.cells(RowIndex, 3) = sh1.cells(SearchIndex)
exit for
endif
next searchindex
next RowIndex
MsgBox ("処理完了")
End Sub
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

お礼日時:2018/10/02 22:07

以下のようにしてください。


--------------------------------------------------------------
Public Sub 負荷一覧作成()
Dim sh1 As Worksheet
Dim sh2 As Worksheet
Dim maxRow1 As Long
Dim maxRow2 As Long
Dim wrow As Long
Dim dicT As Object '連想配列(部品名の記憶)
Dim key As Variant
Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
Set sh1 = Worksheets("Sheet1") ' リスト1のシート名
Set sh2 = Worksheets("Sheet2") ' リスト2のシート名
maxRow1 = sh1.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める
maxRow2 = sh2.Cells(Rows.Count, 1).End(xlUp).row ' 最終行を求める
'部品一覧を作成し、部品名+注番毎の時間を記憶する
For wrow = 2 To maxRow1
key = sh1.Cells(wrow, 1).Value & "|" & sh1.Cells(wrow, 2).Value
dicT(key) = sh1.Cells(wrow, 3).Value
Next
'部品名+注番毎の時間を検索し設定する
For wrow = 2 To maxRow2
key = sh2.Cells(wrow, 1).Value & "|" & sh2.Cells(wrow, 2).Value
'部品一覧にこの部品があれば、その時間を設定
If dicT.exists(key) = True Then
sh2.Cells(wrow, 3).Value = dicT(key)
Else
'なければ、"-"を設定する
sh2.Cells(wrow, 3).Value = "-"
End If
Next
MsgBox ("処理完了")
End Sub
    • good
    • 0
この回答へのお礼

ご回答ありがとうございます。

お礼日時:2018/10/02 22:07

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