▽1
●
a-kuma3 ●300ポイント ベストアンサー |
こんな感じでどうでしょう。
' クリップボードのデータを、行単位の配列で返す Function get_clipboard_data() As Variant Dim ret() Dim temp_sheet, target_sheet Set target_sheet = ActiveSheet Set temp_sheet = Worksheets.Add temp_sheet.Paste Range("a1") last_row = Cells(Rows.Count, 1).End(xlUp).Row ReDim ret(last_row) For r = 1 To last_row ret(r) = Cells(r, 1).Value Next get_clipboard_data = ret Application.DisplayAlerts = False temp_sheet.Delete Application.DisplayAlerts = True target_sheet.Activate End Function Sub set_data() ' クリップボードのデータを取得 Data = get_clipboard_data() ' 日付を切り出し Set re = CreateObject("VBScript.RegExp") re.Pattern = "\[([^\]]+)\](.*)" Set m = re.Execute(Data(1)) If m.Count = 0 Then Exit Sub End If report_date = m(0).submatches(0) ' 日付の行を探す last_row = Cells(Rows.Count, 1).End(xlUp).Row target_row = -1 For r = 2 To last_row If Cells(r, 1).Text = report_date Then target_row = r Exit For End If Next If target_row = -1 Then Exit Sub End If ' 行タイトルと列の対応表を作る Set column_map = CreateObject("Scripting.Dictionary") last_col = Cells(1, Columns.Count).End(xlToLeft).Column For c = 2 To last_col column_map.Add Cells(1, c).Value, c Next ' 日付以外のデータをシートに埋め込む For i = 2 To UBound(Data) Set m = re.Execute(Data(i)) If m.Count > 0 Then k = m(0).submatches(0) v = m(0).submatches(1) If column_map.exists(k) Then Cells(target_row, column_map.Item(k)).Value = v End If End If Next End Sub
set_data サブルーチンを、該当のシートを選択した状態で実行です。
クリップボードからデータを取得するところが関数になってるのは、前の質問で id:ken3memo さんが Microsoft Forms を使う回答をしていたので、そちらの方がお好みなら、この関数の部分を差し替えてください、ということで。