▽1
●
a-kuma3 ●1800ポイント ベストアンサー |
以下のコードをブックA の標準モジュールに貼り付けて、サブルーチンの先頭の変数 ref_book_filename にブックB のファイル名をフルパスで設定してください。
そして、set_data4 サブルーチンを実行してください。
Sub set_data4() ref_book_filename = "d:\foo\bar.xlsx" ' 参照先の Book (フルパス) ref_key_column = 2 ' B列 ref_value_column = 11 ' K列 search_column = 26 ' Z列 write_column = search_column + 12 ' AL列 Set this_book = ActiveWorkbook Set this_sheet = ActiveSheet Set ref_book = Workbooks.Open(ref_book_filename) ' 別シートの値を読み込む Set ref_sheet = ref_book.Sheets(1) ' ひとつめのシート last_row = ref_sheet.Cells(Rows.Count, ref_key_column).End(xlUp).Row Set Map = CreateObject("Scripting.Dictionary") For r = last_row To 1 Step -1 Set cell = ref_sheet.Cells(r, ref_key_column) If Not IsEmpty(cell) And Not cell.Value = "" Then If Not Map.Exists(cell.Value) Then Map.Add cell.Value, r End If End If DoEvents Next this_book.Activate this_sheet.Activate ' 対象シートから探して、値を書き込む last_row = Cells(Rows.Count, search_column).End(xlUp).Row For r = 1 To last_row Set cell = Cells(r, search_column) If Not IsEmpty(cell) And Not cell.Value = "" Then ref_r = Map.Item(cell.Value) If Not IsEmpty(ref_r) Then ref_sheet.Range( _ ref_sheet.Cells(ref_r, ref_value_column), _ ref_sheet.Cells(ref_r, ref_value_column + 4)).Copy _ Range(Cells(r + 1, write_column), Cells(r + 1, write_column + 4)) Map.Remove cell.Value End If End If DoEvents Next Set Map = Nothing ref_book.Close Set ref_book = Nothing End Sub
質問の文面では冒頭に「半角全角を区別して」とありますが、特に気にしなくて良いですよね?