回答編集履歴

2

コード修正

2022/07/28 11:58

投稿

hatena19
hatena19

スコア33751

test CHANGED
@@ -19,13 +19,13 @@
19
19
  'コピー元データをDictionaryに登録
20
20
  With wb2.Worksheets("コピー元").Cells(1).CurrentRegion
21
21
  For i = 2 To .Rows.Count
22
- Set dic(Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 1), "hh:mm:ss")) = .Rows(i)
22
+ Set dic(Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 2), "hh:mm:ss")) = .Rows(i)
23
23
  Next
24
24
  End With
25
25
  'Dictionaryを検索して該当データをコピー先にコピー
26
26
  With wb1.Worksheets("コピー先").Cells(1).CurrentRegion
27
27
  For i = 2 To .Rows.Count
28
- Dim key As String: key = Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 1), "hh:mm:ss")
28
+ Dim key As String: key = Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 2), "hh:mm:ss")
29
29
  If dic.Exists(key) Then dic(key).Copy .Cells(i, 7)
30
30
  Next
31
31
  End With

1

おまけ追加

2022/07/27 12:28

投稿

hatena19
hatena19

スコア33751

test CHANGED
@@ -1,3 +1,35 @@
1
1
  `wb2.Range("A1")` の部分でシートを指定してないのでエラーになります。
2
2
 
3
3
  `wb2.Worksheets("コピー元").Range("A1")`というようにシートを指定しましょう。
4
+
5
+ おまけ
6
+ ---
7
+ Dictinary を使って検索するサンプルコード
8
+
9
+ ```vba
10
+ Sub 改めて日付時間を表示形式にする繰り返し_Dictinary()
11
+ Dim i As Long
12
+ Dim wb1 As Workbook
13
+ Dim wb2 As Workbook
14
+ Set wb1 = ThisWorkbook
15
+ Set wb2 = Workbooks.Open(FileName:=ThisWorkbook.Path & "\実験用コピー元.xlsx")
16
+
17
+ Dim dic As Object
18
+ Set dic = CreateObject("Scripting.Dictionary")
19
+ 'コピー元データをDictionaryに登録
20
+ With wb2.Worksheets("コピー元").Cells(1).CurrentRegion
21
+ For i = 2 To .Rows.Count
22
+ Set dic(Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 1), "hh:mm:ss")) = .Rows(i)
23
+ Next
24
+ End With
25
+ 'Dictionaryを検索して該当データをコピー先にコピー
26
+ With wb1.Worksheets("コピー先").Cells(1).CurrentRegion
27
+ For i = 2 To .Rows.Count
28
+ Dim key As String: key = Format(.Cells(i, 1), "yyyy/m/d") & " " & Format(.Cells(i, 1), "hh:mm:ss")
29
+ If dic.Exists(key) Then dic(key).Copy .Cells(i, 7)
30
+ Next
31
+ End With
32
+
33
+ End Sub
34
+
35
+ ```