回答編集履歴

1

追記

2021/03/04 11:15

投稿

jinoji
jinoji

スコア4585

test CHANGED
@@ -1 +1,211 @@
1
1
  today=Date ?
2
+
3
+
4
+
5
+ ---
6
+
7
+ こんな感じですか。
8
+
9
+
10
+
11
+ ```VBA
12
+
13
+ Sub Alert()
14
+
15
+ ' 設定シート
16
+
17
+ Dim settingSheet As Worksheet
18
+
19
+ Set settingSheet = Worksheets("設定")
20
+
21
+
22
+
23
+ ' 対象シート
24
+
25
+ Dim targetSheet As Worksheet
26
+
27
+ Set targetSheet = Worksheets(settingSheet.Cells(1, 2).Value)
28
+
29
+
30
+
31
+ Dim resultSheet As Worksheet
32
+
33
+ Set resultSheet = Worksheets(settingSheet.Cells(8, 2).Value)
34
+
35
+
36
+
37
+ ' 対象列
38
+
39
+ Dim targetColStr As String
40
+
41
+ targetColStr = settingSheet.Cells(2, 2).Value
42
+
43
+
44
+
45
+ ' 対象列の最終行を取得
46
+
47
+ Dim targetColLastRow As Long
48
+
49
+ targetColLastRow = targetSheet.Cells(Rows.Count, targetColStr).End(xlUp).Row
50
+
51
+
52
+
53
+ ' 今日の日付を取得
54
+
55
+ Dim today As Date
56
+
57
+ today = Date
58
+
59
+
60
+
61
+ 'データ開始行を取得
62
+
63
+ Dim dataStartRow As Integer
64
+
65
+ dataStartRow = settingSheet.Cells(3, 2).Value
66
+
67
+
68
+
69
+ ' 対象列を全件チェック
70
+
71
+ Dim i As Integer ' 行数ループカウンタ
72
+
73
+ Dim v As Variant ' セルからの値受け取り変数
74
+
75
+
76
+
77
+ Dim alertCount As Integer ' 期限の過ぎている数
78
+
79
+ alertCount = 0
80
+
81
+
82
+
83
+ Dim checkCount As Integer ' チェック対象の数
84
+
85
+ checkCount = 0
86
+
87
+
88
+
89
+ Dim notDateCount As Integer ' 日付以外の数
90
+
91
+ notDateCount = 0
92
+
93
+
94
+
95
+ Dim targetCell As Range
96
+
97
+
98
+
99
+ For i = 0 To targetColLastRow - dataStartRow
100
+
101
+
102
+
103
+ Set targetCell = targetSheet.Columns(targetColStr).Rows(dataStartRow + i)
104
+
105
+ v = targetCell.Value
106
+
107
+
108
+
109
+ With Union(targetCell, targetCell.Offset(, -11))
110
+
111
+
112
+
113
+ If IsDate(v) Then
114
+
115
+ checkCount = checkCount + 1
116
+
117
+
118
+
119
+ Select Case DateDiff("d", today, v)
120
+
121
+ Case Is < settingSheet.Cells(7, 2).Value '期日超過
122
+
123
+ alertCount = alertCount + 1
124
+
125
+ .Font.Color = RGB(255, 255, 255)
126
+
127
+ .Interior.Color = RGB(0, 0, 0)
128
+
129
+ Case Is <= settingSheet.Cells(6, 2).Value
130
+
131
+ alertCount = alertCount + 1
132
+
133
+ .Font.Color = RGB(255, 255, 255)
134
+
135
+ .Interior.Color = RGB(255, 0, 0)
136
+
137
+ Case Is <= settingSheet.Cells(5, 2).Value
138
+
139
+ alertCount = alertCount + 1
140
+
141
+ .Font.Color = RGB(255, 255, 255)
142
+
143
+ .Interior.Color = RGB(255, 153, 0)
144
+
145
+ Case Is <= settingSheet.Cells(4, 2).Value 'settingSheet.Cellsの値は30で設定
146
+
147
+ alertCount = alertCount + 1
148
+
149
+ .Font.Color = RGB(0, 0, 0)
150
+
151
+ .Interior.Color = RGB(255, 204, 0)
152
+
153
+ Case Else
154
+
155
+ '上記に当てはらまない
156
+
157
+ .Font.Color = RGB(0, 0, 0)
158
+
159
+ .Interior.Color = RGB(255, 255, 255)
160
+
161
+ End Select
162
+
163
+
164
+
165
+ Else
166
+
167
+ notDateCount = notDateCount + 1
168
+
169
+ .Font.Color = RGB(0, 0, 0)
170
+
171
+ .Interior.Color = RGB(255, 255, 0)
172
+
173
+ End If
174
+
175
+
176
+
177
+ End With
178
+
179
+
180
+
181
+ Next
182
+
183
+
184
+
185
+ ' 結果シートを更新
186
+
187
+ resultSheet.Cells(3, 19).Value = checkCount
188
+
189
+ resultSheet.Cells(4, 19).Value = alertCount
190
+
191
+ resultSheet.Cells(5, 19).Value = notDateCount
192
+
193
+
194
+
195
+ If alertCount > 0 Then
196
+
197
+ MsgBox "期限チェック完了しました。1か月以内に有効期限の切れるものがあります。"
198
+
199
+ Else
200
+
201
+ MsgBox "期限チェック完了しました。期限切れの項目はありません。"
202
+
203
+ End If
204
+
205
+
206
+
207
+ End Sub
208
+
209
+
210
+
211
+ ```