質問するログイン新規登録

回答編集履歴

1

追記

2020/04/12 09:04

投稿

meg_
meg_

スコア11027

answer CHANGED
@@ -1,3 +1,45 @@
1
+ ループ回数を減らしました。どのぐらい速くなるかは分かりませんが、どうでしょうか?
2
+
3
+ ```VBA
4
+ Sub test()
5
+
6
+ Dim txt As String, txt2 As String
7
+ Dim i As Long, j As Long
8
+ Dim ws As Worksheet, ws2 As Worksheet
9
+ Set ws = Sheets("Sheet1")
10
+ Set ws2 = Sheets("Sheet2")
11
+ With ws.Sort
12
+ With .SortFields
13
+ .Clear
14
+ .Add Key:=ws.Range("A1"), Order:=xlAscending
15
+ .Add Key:=ws.Range("E1"), Order:=xlDescending
16
+ End With
17
+ .SetRange ws.Range("A1:F" & Cells(Rows.Count, 1).End(xlUp).Row)
18
+ .Header = xlYes
19
+ .Apply
20
+ End With
21
+
22
+ With ws
23
+ txt = .Cells(2, 1).Value
24
+ txt2 = .Cells(2, 5).Value
25
+ For i = 3 To .Cells(Rows.Count, 1).End(xlUp).Row
26
+ If .Cells(i, 1).Value <> txt Then
27
+ txt = .Cells(i, 1).Value
28
+ txt2 = .Cells(i, 5).Value
29
+ ElseIf .Cells(i, 5).Value <> txt2 Then
30
+ .Cells(i, 6) = "旧版"
31
+ ws.Rows(i).Copy
32
+ ws2.Rows(ws2.Cells(Rows.Count, 1).End(xlUp).Row).Offset(1, 0).Insert Shift:=xlDown
33
+ ws.Rows(i).Delete Shift:=xlUp
34
+ i = i - 1
35
+ End If
36
+ Next i
37
+ End With
38
+
39
+ End Sub
40
+ ```
41
+
42
+ ---
1
43
  Forループを2回しているので、1回にしてはどうでしょうか?
2
44
  検証はしていませんが下記の様になるかと思います。
3
45