Option Explicit
Public Sub 合計シート設定()
Dim gks As Worksheet
Dim wk_num As Variant
Dim wk_num2 As Variant
Dim ws As Worksheet
Dim wrow As Long
Dim wcol As Long
Set gks = Worksheets("合計")
gks.Range("B2:J10").ClearContents
wk_num = check_week(gks.Range("A1").Value)
If wk_num = 0 Then
MsgBox ("曜日不正:" & gks.Range("A1").Value)
Exit Sub
End If
For Each ws In Worksheets
If ws.Name = "合計" Then Exit For
wk_num2 = check_date(ws.Name)
If wk_num2 = 0 Then
MsgBox ("シートの日付不正:" & ws.Name)
Exit Sub
End If
If wk_num = wk_num2 Then
For wrow = 2 To 10
For wcol = 2 To 10
gks.Cells(wrow, wcol).Value = gks.Cells(wrow, wcol).Value + ws.Cells(wrow, wcol).Value
Next
Next
End If
Next
MsgBox ("合計シート設定完了")
End Sub
Public Sub 抽出シート設定()
Dim cys As Worksheet
Dim wk_num As Variant
Dim wk_num2 As Variant
Dim ws As Worksheet
Dim wrow As Long
Dim wcol As Long
Dim dicT As Object
Dim key As Variant
Dim maxrow As Long
Set cys = Worksheets("抽出")
cys.Range("B:ZZ").ClearContents
wk_num = check_week(cys.Range("A1").Value)
If wk_num = 0 Then
MsgBox ("曜日不正:" & cys.Range("A1").Value)
Exit Sub
End If
maxrow = cys.Cells(Rows.Count, 1).End(xlUp).Row '最大行取得
If maxrow < 3 Then
MsgBox ("抽出セル未設定")
Exit Sub
End If
Set dicT = CreateObject("Scripting.Dictionary") ' 連想配列の定義
For wrow = 3 To maxrow
key = cys.Cells(wrow, "A").Value
If dicT.exists(key) = True Then
MsgBox ("抽出セル重複:" & key)
Exit Sub
End If
If check_cellname(key) = False Then
MsgBox ("抽出セル不正:" & key)
Exit Sub
End If
dicT(key) = wrow
Next
wcol = 2
For Each ws In Worksheets
If ws.Name = "合計" Then Exit For
wk_num2 = check_date(ws.Name)
If wk_num2 = 0 Then
MsgBox ("シートの日付不正:" & ws.Name)
Exit Sub
End If
If wk_num = wk_num2 Then
cys.Cells(1, wcol).Value = cys.Cells(1, 1).Value '曜日
cys.Cells(2, wcol).Value = "'" & ws.Name 'シート名
wrow = 3
For Each key In dicT.keys
cys.Cells(wrow, wcol).Value = ws.Range(key).Value
wrow = wrow + 1
Next
wcol = wcol + 1
End If
Next
MsgBox ("抽出シート設定完了")
End Sub
'曜日チェック("日", "月", "火", "水", "木", "金", "土"の何れかであること)
Private Function check_week(ByVal wk As String) As Variant
Dim wk_arr As Variant
Dim i As Integer
check_week = 0
wk_arr = Array("日", "月", "火", "水", "木", "金", "土")
For i = 0 To UBound(wk_arr)
If wk = wk_arr(i) Then
check_week = i + 1
Exit Function
End If
Next
End Function
'日付チェック(西暦下2桁+月2桁+日2桁であること、暦日上存在すること)
Private Function check_date(ByVal yymmdd As String) As Variant
Dim wdate As Date
Dim wdate_str As String
check_date = 0
If Len(yymmdd) <> 6 Then Exit Function
If IsNumeric(yymmdd) = False Then Exit Function
wdate_str = "20" & Left(yymmdd, 2) & "/" & Mid(yymmdd, 3, 2) & "/" & Right(yymmdd, 2)
If IsDate(wdate_str) = False Then Exit Function
wdate = CDate(wdate_str)
check_date = Weekday(wdate)
End Function
'抽出セル名チェック(B2~J10であること)
Private Function check_cellname(ByVal cellname As String)
Dim wlen As Long
Dim wstr1 As String
Dim wstr2 As Long
check_cellname = False
wlen = Len(cellname)
If wlen < 2 Or wlen > 3 Then Exit Function
wstr1 = Left(cellname, 1)
wstr2 = Right(cellname, wlen - 1)
If wstr1 < "B" Or wstr1 > "J" Then Exit Function
If IsNumeric(wstr2) = False Then Exit Function
If CLng(wstr2) < 2 Or CLng(wstr2) > 10 Then Exit Function
check_cellname = True
End Function