回答編集履歴

2

追記

2021/09/29 09:52

投稿

jinoji
jinoji

スコア4592

test CHANGED
@@ -1,10 +1,70 @@
1
- こういう感じですかね。
1
+ ```VBA
2
2
 
3
- ```VBA
3
+ Sub sample1()
4
+
5
+ Dim wb As Workbook, saki As Worksheet
6
+
7
+ Set wb = Workbooks("都道府県.xlsx")
8
+
9
+ Set saki = wb.Sheets(1)
10
+
11
+
4
12
 
5
13
  Dim moto As Worksheet
6
14
 
15
+ Set moto = ThisWorkbook.Worksheets("A")
16
+
17
+
18
+
19
+ Dim cnt As Long
20
+
21
+ cnt = saki.Range("A:A").Find("東京").Row
22
+
23
+
24
+
25
+ Dim i
26
+
27
+ For i = 2 To moto.Cells(Rows.Count, 1).End(xlUp).Row
28
+
29
+ saki.Range("L" & cnt) = moto.Range("J" & i)
30
+
31
+ saki.Range("M" & cnt) = moto.Range("L" & i)
32
+
33
+ saki.Range("N" & cnt) = moto.Range("M" & i)
34
+
35
+ saki.Range("O" & cnt) = moto.Range("K" & i)
36
+
37
+ cnt = cnt + 1
38
+
39
+ Next
40
+
41
+ End Sub
42
+
43
+
44
+
45
+
46
+
47
+ Sub sample2()
48
+
49
+ Dim wb As Workbook, saki As Worksheet
50
+
51
+ Set wb = Workbooks("都道府県.xlsx")
52
+
53
+ Set saki = wb.Sheets(1)
54
+
55
+
56
+
7
- Dim saki As Worksheet
57
+ Dim moto As Worksheet
58
+
59
+ Set moto = ThisWorkbook.Worksheets("A")
60
+
61
+
62
+
63
+ Dim lastRow As Long
64
+
65
+ lastRow = moto.Range("A" & moto.Rows.Count).End(xlUp).Row
66
+
67
+
8
68
 
9
69
  Dim i, j
10
70
 
@@ -18,10 +78,14 @@
18
78
 
19
79
  j = j + i
20
80
 
81
+ If j > lastRow Then Exit For
82
+
21
83
  End If
22
84
 
23
85
  Next
24
86
 
87
+ End Sub
88
+
25
89
 
26
90
 
27
91
  ```

1

修正

2021/09/29 09:52

投稿

jinoji
jinoji

スコア4592

test CHANGED
@@ -1,15 +1,27 @@
1
- ひとまず東京を目指すのはどうでしょう
1
+ いう感じすかね
2
2
 
3
3
  ```VBA
4
4
 
5
- With wb.Sheets(1)
5
+ Dim moto As Worksheet
6
6
 
7
- For cnt = 2 To .Range("A" & .Rows.Count).End(xlUp).Row
7
+ Dim saki As Worksheet
8
8
 
9
- If InStr(.Range("A" & cnt).Value, "東京") > 0 Then Exit For
9
+ Dim i, j
10
10
 
11
- Next
11
+ j = 2
12
12
 
13
+ For i = 1 To saki.Range("A" & saki.Rows.Count).End(xlUp).Row
14
+
15
+ If saki.Range("A" & i).Value = "東京" Then
16
+
17
+ saki.Range("L" & i).Value = moto.Range("J" & j).Value
18
+
19
+ j = j + i
20
+
13
- End With
21
+ End If
22
+
23
+ Next
24
+
25
+
14
26
 
15
27
  ```