回答編集履歴

1

追記

2020/04/12 09:04

投稿

meg_
meg_

スコア10579

test CHANGED
@@ -1,3 +1,87 @@
1
+ ループ回数を減らしました。どのぐらい速くなるかは分かりませんが、どうでしょうか?
2
+
3
+
4
+
5
+ ```VBA
6
+
7
+ Sub test()
8
+
9
+
10
+
11
+ Dim txt As String, txt2 As String
12
+
13
+ Dim i As Long, j As Long
14
+
15
+ Dim ws As Worksheet, ws2 As Worksheet
16
+
17
+ Set ws = Sheets("Sheet1")
18
+
19
+ Set ws2 = Sheets("Sheet2")
20
+
21
+ With ws.Sort
22
+
23
+ With .SortFields
24
+
25
+ .Clear
26
+
27
+ .Add Key:=ws.Range("A1"), Order:=xlAscending
28
+
29
+ .Add Key:=ws.Range("E1"), Order:=xlDescending
30
+
31
+ End With
32
+
33
+ .SetRange ws.Range("A1:F" & Cells(Rows.Count, 1).End(xlUp).Row)
34
+
35
+ .Header = xlYes
36
+
37
+ .Apply
38
+
39
+ End With
40
+
41
+
42
+
43
+ With ws
44
+
45
+ txt = .Cells(2, 1).Value
46
+
47
+ txt2 = .Cells(2, 5).Value
48
+
49
+ For i = 3 To .Cells(Rows.Count, 1).End(xlUp).Row
50
+
51
+ If .Cells(i, 1).Value <> txt Then
52
+
53
+ txt = .Cells(i, 1).Value
54
+
55
+ txt2 = .Cells(i, 5).Value
56
+
57
+ ElseIf .Cells(i, 5).Value <> txt2 Then
58
+
59
+ .Cells(i, 6) = "旧版"
60
+
61
+ ws.Rows(i).Copy
62
+
63
+ ws2.Rows(ws2.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Insert Shift:=xlDown
64
+
65
+ ws.Rows(i).Delete Shift:=xlUp
66
+
67
+ i = i - 1
68
+
69
+ End If
70
+
71
+ Next i
72
+
73
+ End With
74
+
75
+
76
+
77
+ End Sub
78
+
79
+ ```
80
+
81
+
82
+
83
+ ---
84
+
1
85
  Forループを2回しているので、1回にしてはどうでしょうか?
2
86
 
3
87
  検証はしていませんが下記の様になるかと思います。