▽1
●
a-kuma3 ●3000ポイント ベストアンサー |
自動化したい作業のイメージを再確認です。
bookA の AT?AU列に間違ったデータがあって、それを bookB の L?M列のデータで書き換えたい。
正しいデータは、bookA の一行下の AO列と bookB の G列とが一致する行で、その一行上のもの。
... | AO | ... | AT | AU | ... |
---|---|---|---|---|---|
りんごごりらへび | こぶたたぬき柴犬 | ||||
ST-010203 | |||||
... | G | ... | L | M | ... |
---|---|---|---|---|---|
りんごごりららっぱ | こぶたたぬき狐 | ||||
ST-010203 |
... | AO | ... | AT | AU | ... |
---|---|---|---|---|---|
りんごごりららっぱ | こぶたたぬき狐 | ||||
ST-010203 | |||||
作業のイメージがあってるとして、こんな感じでどうでしょうか。
Const REF_BOOK = "D:\data\bookB.xlsx" ' bookB のフルパス Sub copy_data() search_word = "りんごごりら" ' 検索ワード target_column = 46 ' AT列 ref_column = 7 ' G列 Set this_book = ActiveWorkbook Set bookB = Workbooks.Open(REF_BOOK) this_book.Activate Set ref_sheet = bookB.Sheets(1) ' bookB の 1番目のシート last_row = Cells(Rows.Count, target_column).End(xlUp).Row last_row_b = ref_sheet.Cells(Rows.Count, ref_column).End(xlUp).Row For r = 1 To last_row If InStr(Cells(r, target_column).Value, search_word) > 0 Then Key = Cells(r + 1, target_column - 5).Value rr = -1 For r2 = 1 To last_row_b If ref_sheet.Cells(r2, ref_column).Value = Key Then rr = r2 Exit For End If DoEvents Next If rr <> -1 Then Set s_range = ref_sheet.Range(ref_sheet.Cells(rr - 1, ref_column + 5), ref_sheet.Cells(rr - 1, ref_column + 6)) Set d_range = Range(Cells(r, target_column), Cells(r, target_column + 1)) s_range.Copy d_range End If End If DoEvents Next bookB.Close SaveChanges:=False End Sub
上記のマクロは、bookA の標準モジュールにはりつけてください。
マクロ中の上記を適切なものに変えて、bookA の修正したいシートを選択した状態で、サブルーチン copy_data を実行してください。
# 念のため書いておきますが、バックアップを取ってから実行してください
bookB の、どのシートを見るかという指定がなかったので、一番目のシートから探すようにしています。
処理が終了すると、bookB は自動的に閉じます。
bookB に変更点があっても、無視して閉じるようにしてあるので、bookB に手を加えた場合には、注意してください。
検索対象の AT列の文字の長さにもよりますが、bookA の行数というよりは置き換えるデータの数で処理時間が決まってくると思うので、それほど時間はかからずに終了すると思います。
上記のマクロは、bookA で該当データが見つかるたびに、bookB を探しているのですが、bookB のシートの行数が多いと遅いかもしれません。
コメントで、修正するデータは一種類だけのようなことを書かれていたので、bookB の正しいデータを覚えておくようにしたのが、次のマクロです。
Const REF_BOOK = "D:\data\bookB.xlsx" ' bookB のフルパス Sub copy_data2() search_word = "りんごごりら" ' 検索ワード target_column = 46 ' AT列 ref_column = 7 ' G列 Set this_book = ActiveWorkbook Set bookB = Workbooks.Open(REF_BOOK) this_book.Activate Set ref_sheet = bookB.Sheets(1) ' bookB の 1番目のシート last_row = Cells(Rows.Count, target_column).End(xlUp).Row last_row_b = ref_sheet.Cells(Rows.Count, ref_column).End(xlUp).Row prev_key = "" Set ref_range = Nothing For r = 1 To last_row If InStr(Cells(r, target_column).Value, search_word) > 0 Then Key = Cells(r + 1, target_column - 5).Value If prev_key <> Key Then Set ref_range = Nothing For r2 = 1 To last_row_b If ref_sheet.Cells(r2, ref_column).Value = Key Then prev_key = Key Set ref_range = ref_sheet.Range(ref_sheet.Cells(r2 - 1, ref_column + 5), ref_sheet.Cells(r2 - 1, ref_column + 6)) Exit For End If DoEvents Next End If If Not ref_range Is Nothing Then Set d_range = Range(Cells(r, target_column), Cells(r, target_column + 1)) ref_range.Copy d_range End If End If DoEvents Next bookB.Close SaveChanges:=False End Sub
こちらは、必要ないかもしれませんが、一応おいときます。