回答編集履歴

3

For Eachを使う方法を追記

2024/02/20 00:27

投稿

YellowGreen
YellowGreen

スコア791

test CHANGED
@@ -78,4 +78,43 @@
78
78
 
79
79
  End Sub
80
80
  ```
81
+ 上をFor Eachを使う方法に変更すると、
82
+ ```vba
83
+ Sub sample33()
81
84
 
85
+ Dim row_c1 As Long
86
+ Dim row_c2 As Long
87
+ Dim wb As Workbook
88
+ Dim wb_out As Workbook
89
+ Dim ws As Worksheet
90
+ Dim ws_out As Worksheet
91
+
92
+ Set wb = Workbooks("Xブック.xlsx")
93
+ Set wb_out = Workbooks("Yブック.xlsm")
94
+ Set ws = wb.Worksheets("Xシート") 'データ元
95
+ Set ws_out = wb_out.Worksheets("Yシート") '出力先
96
+
97
+ row_c1 = 3
98
+ row_c2 = 10
99
+
100
+ Dim col As Variant
101
+ col = [{"D", "F", "H"}]
102
+ Dim v As Variant
103
+ Dim rng As Range
104
+
105
+ Do While CStr(ws.Cells(row_c1, "A").Value) <> "END"
106
+ For Each v In col
107
+ Set rng = ws.Range(v & row_c1)
108
+ If IsEmpty(rng) Or IsEmpty(rng.Offset(0, 1)) Then
109
+ MsgBox "Xシート," & row_c1 & "行目は画像名称が未入力です"
110
+ row_c2 = row_c2 + 1
111
+ End If
112
+ Next v
113
+
114
+ row_c1 = row_c1 + 1
115
+
116
+ Loop
117
+
118
+ End Sub
119
+ ```
120
+

2

空かどうかの判定をIsEmptyに

2024/02/20 00:08

投稿

YellowGreen
YellowGreen

スコア791

test CHANGED
@@ -66,7 +66,7 @@
66
66
 
67
67
  Do While CStr(ws.Cells(row_c1, "A").Value) <> "END"
68
68
  For i = 1 To UBound(col, 1)
69
- If CStr(ws.Cells(row_c1, col(i, 1)).Value) = "" Or CStr(ws.Cells(row_c1, col(i, 2)).Value) = "" Then
69
+ If IsEmpty(ws.Cells(row_c1, col(i, 1))) Or IsEmpty(ws.Cells(row_c1, col(i, 2))) Then
70
70
  MsgBox "Xシート," & row_c1 & "行目は画像名称が未入力です"
71
71
  row_c2 = row_c2 + 1
72
72
  End If

1

ちょっと違った方法も加えました

2024/02/20 00:03

投稿

YellowGreen
YellowGreen

スコア791

test CHANGED
@@ -27,17 +27,55 @@
27
27
  row_c2 = 10
28
28
  Dim i As Long
29
29
 
30
- Do While CStr(values(row_c1, 1)) <> "END"
30
+ Do While CStr(values(row_c1, 1)) <> "END"
31
- For i = 4 To 8 Step 2
31
+ For i = 4 To 8 Step 2
32
- If IsEmpty(values(row_c1, i)) Or IsEmpty(values(row_c1, i + 1)) Then
32
+ If IsEmpty(values(row_c1, i)) Or IsEmpty(values(row_c1, i + 1)) Then
33
- MsgBox "Xシート," & row_c1 & "行目は画像名称が未入力です"
33
+ MsgBox "Xシート," & row_c1 & "行目は画像名称が未入力です"
34
- row_c2 = row_c2 + 1
34
+ row_c2 = row_c2 + 1
35
- End If
35
+ End If
36
- Next i
36
+ Next i
37
37
 
38
- row_c1 = row_c1 + 1
38
+ row_c1 = row_c1 + 1
39
39
 
40
- Loop
40
+ Loop
41
41
 
42
42
  End Sub
43
43
  ```
44
+ 配列の使い方が違いますが、こんなことも...という例です。
45
+ ```vba
46
+ Sub sample33()
47
+
48
+ Dim row_c1 As Long
49
+ Dim row_c2 As Long
50
+ Dim wb As Workbook
51
+ Dim wb_out As Workbook
52
+ Dim ws As Worksheet
53
+ Dim ws_out As Worksheet
54
+
55
+ Set wb = Workbooks("Xブック.xlsx")
56
+ Set wb_out = Workbooks("Yブック.xlsm")
57
+ Set ws = wb.Worksheets("Xシート") 'データ元
58
+ Set ws_out = wb_out.Worksheets("Yシート") '出力先
59
+
60
+ row_c1 = 3
61
+ row_c2 = 10
62
+
63
+ Dim col As Variant
64
+ col = [{"D", "E"; "F", "G"; "H", "I"}]
65
+ Dim i As Long
66
+
67
+ Do While CStr(ws.Cells(row_c1, "A").Value) <> "END"
68
+ For i = 1 To UBound(col, 1)
69
+ If CStr(ws.Cells(row_c1, col(i, 1)).Value) = "" Or CStr(ws.Cells(row_c1, col(i, 2)).Value) = "" Then
70
+ MsgBox "Xシート," & row_c1 & "行目は画像名称が未入力です"
71
+ row_c2 = row_c2 + 1
72
+ End If
73
+ Next i
74
+
75
+ row_c1 = row_c1 + 1
76
+
77
+ Loop
78
+
79
+ End Sub
80
+ ```
81
+