This commit is contained in:
updsv7
2026-04-16 18:37:48 +09:00
parent b0c6ec6baa
commit c661373369
7 changed files with 1018 additions and 58 deletions

View File

@@ -135,6 +135,11 @@ Sub Z2_validateButton()
End If
Next r
' === Refresh Z2 cache after validation passes ===
If errorCount = 0 Then
Call RefreshZ2Cache
End If
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
End Sub

View File

@@ -143,6 +143,11 @@ Sub Z3_validateButton()
End If
Next r
' === Refresh Z3 cache after validation passes ===
If errorCount = 0 Then
Call RefreshZ3Cache
End If
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
End Sub

View File

@@ -0,0 +1,540 @@
' ====== (Tukin_C1) =======
' Commuter allowance editing sheet
' No CSV import - direct editing only
' ====== Constants ======
Const START_COL As Long = 3 ' C column (职员番号)
Const END_COL As Long = 56 ' BC column
Const ERROR_COL As Long = 57 ' BD column
Const Tukin_HEADER_ROW As Long = 6
' Column regions (for reference)
' D-H: 届出情報 (cols 4-8)
' I-J: 住所情報 (cols 9-10)
' K-O: 出勤情報 (cols 11-15)
' P-R: 自動車等情報 (cols 16-18)
' S-Y: 区間1情報 (cols 19-25)
' Z-AF: 区間2情報 (cols 26-32)
' AG-AM: 区間3情報 (cols 33-39)
' AN-AT: 区間4情報 (cols 40-46)
' AU-AX: 決定事項情報 (cols 47-50)
' AY-BA: 備考情報 (cols 51-53)
' BB-BC: 認定情報 (cols 54-56)
' ====== Event Handlers ======
Private Sub Worksheet_Change(ByVal Target As Range)
' === Column C changes: Fill address info from O1 and Z1 cache ===
If Target.Column = 3 And Target.Row >= 7 Then
Dim cell As Range
For Each cell In Target
If Trim(cell.Value) = "" Then
Call ClearRowData(Me, cell.Row)
Else
Call FillAddressFromO1(cell.Row)
' Generate transport (T) dropdown for all 4 kukan sections
Call CreateZ1TransportDropdown(Me, cell.Row, 20) ' 区間1 - T column
Call CreateZ1TransportDropdown(Me, cell.Row, 30) ' 区間2 - AD column
Call CreateZ1TransportDropdown(Me, cell.Row, 37) ' 区間3 - AK column
Call CreateZ1TransportDropdown(Me, cell.Row, 44) ' 区間4 - AR column
End If
Next
End If
' === Column T (区間1 交通機関) changes: Generate U (利用区間発) dropdown from Z1 ===
If Target.Column = 20 And Target.Row >= 7 Then
Dim cellT As Range
For Each cellT In Target
If Trim(cellT.Value) <> "" Then
Call CreateZ1StationDropdown(Me, cellT.Row, 20, 21, 1)
Else
Call ClearKukanValidation(Me, cellT.Row, 21)
End If
Next
End If
' === Column U (区間1 利用区間発) changes: Generate V (利用区間着) dropdown from M1_KukanD ===
If Target.Column = 21 And Target.Row >= 7 Then
Dim cellU As Range
For Each cellU In Target
If Trim(cellU.Value) <> "" Then
Call CreateM1KukanDDropdown(Me, cellU.Row, 20, 21, 22)
If Me.Cells(cellU.Row, 22).Validation.Formula1 = "" Then
' No dropdown data, clear V column
Call ClearKukanValidation(Me, cellU.Row, 22)
End If
Else
Call ClearKukanValidation(Me, cellU.Row, 22)
End If
Next
End If
' === Column AD (区間2 交通機関) changes ===
If Target.Column = 30 And Target.Row >= 7 Then
Dim cellAD As Range
For Each cellAD In Target
If Trim(cellAD.Value) <> "" Then
Call CreateZ1StationDropdown(Me, cellAD.Row, 30, 31)
Else
Call ClearKukanValidation(Me, cellAD.Row, 31)
End If
Next
End If
' === Column AE (区間2 利用区間発) changes ===
If Target.Column = 31 And Target.Row >= 7 Then
Dim cellAE As Range
For Each cellAE In Target
If Trim(cellAE.Value) <> "" Then
Call CreateM1KukanDDropdown(Me, cellAE.Row, 30, 31, 32)
If Me.Cells(cellAE.Row, 32).Validation.Formula1 = "" Then
Call ClearKukanValidation(Me, cellAE.Row, 32)
End If
Else
Call ClearKukanValidation(Me, cellAE.Row, 32)
End If
Next
End If
' === Column AK (区間3 交通機関) changes ===
If Target.Column = 37 And Target.Row >= 7 Then
Dim cellAK As Range
For Each cellAK In Target
If Trim(cellAK.Value) <> "" Then
Call CreateZ1StationDropdown(Me, cellAK.Row, 37, 38)
Else
Call ClearKukanValidation(Me, cellAK.Row, 38)
End If
Next
End If
' === Column AL (区間3 利用区間発) changes ===
If Target.Column = 38 And Target.Row >= 7 Then
Dim cellAL As Range
For Each cellAL In Target
If Trim(cellAL.Value) <> "" Then
Call CreateM1KukanDDropdown(Me, cellAL.Row, 37, 38, 39)
If Me.Cells(cellAL.Row, 39).Validation.Formula1 = "" Then
Call ClearKukanValidation(Me, cellAL.Row, 39)
End If
Else
Call ClearKukanValidation(Me, cellAL.Row, 39)
End If
Next
End If
' === Column AR (区間4 交通機関) changes ===
If Target.Column = 44 And Target.Row >= 7 Then
Dim cellAR As Range
For Each cellAR In Target
If Trim(cellAR.Value) <> "" Then
Call CreateZ1StationDropdown(Me, cellAR.Row, 44, 45)
Else
Call ClearKukanValidation(Me, cellAR.Row, 45)
End If
Next
End If
' === Column AS (区間4 利用区間発) changes ===
If Target.Column = 45 And Target.Row >= 7 Then
Dim cellAS As Range
For Each cellAS In Target
If Trim(cellAS.Value) <> "" Then
Call CreateM1KukanDDropdown(Me, cellAS.Row, 44, 45, 46)
If Me.Cells(cellAS.Row, 46).Validation.Formula1 = "" Then
Call ClearKukanValidation(Me, cellAS.Row, 46)
End If
Else
Call ClearKukanValidation(Me, cellAS.Row, 46)
End If
Next
End If
' === Column S changes: Fill 区間1 from M1/M2 cache ===
If Target.Column = 19 And Target.Row >= 7 Then
Dim cellS As Range
For Each cellS In Target
If Trim(cellS.Value) <> "" Then
' First check if T column has dropdown data
Call CreateZ1TransportDropdown(Me, cellS.Row, 20)
If Me.Cells(cellS.Row, 20).Validation.Formula1 <> "" Then
Call FillKukanFromM1(Me, cellS.Row, 19, Array(20, 21, 22)) ' S->T,U,V
Call CreateM2Dropdown(Me, cellS.Row, 19, 23) ' S->W dropdown (券種)
End If
Else
Call ClearKukanValidation(Me, cellS.Row, 23)
End If
Next
End If
' === Column W changes: Fill 券種 dropdown -> generate X dropdown ===
If Target.Column = 23 And Target.Row >= 7 Then
Dim cellW As Range
For Each cellW In Target
If Trim(cellW.Value) <> "" Then
Call CreateM2CodeDropdown(Me, cellW.Row, 19, 23, 24) ' kukanCode, kanshu -> X dropdown
Else
Call ClearKukanValidation(Me, cellW.Row, 24)
End If
Next
End If
' === Column Z changes: Fill 区間2 from M1/M2 cache ===
If Target.Column = 26 And Target.Row >= 7 Then
Dim cellZ As Range
For Each cellZ In Target
If Trim(cellZ.Value) <> "" Then
Call CreateZ1TransportDropdown(Me, cellZ.Row, 30)
If Me.Cells(cellZ.Row, 30).Validation.Formula1 <> "" Then
Call FillKukanFromM1(Me, cellZ.Row, 26, Array(27, 28, 29)) ' Z->AA,AB,AC
Call CreateM2Dropdown(Me, cellZ.Row, 26, 30) ' Z->AD dropdown
End If
Else
Call ClearKukanValidation(Me, cellZ.Row, 30)
End If
Next
End If
' === Column AD changes: Fill 券種 dropdown -> generate AH dropdown ===
If Target.Column = 30 And Target.Row >= 7 Then
Dim cellAD As Range
For Each cellAD In Target
If Trim(cellAD.Value) <> "" Then
Call CreateM2CodeDropdown(Me, cellAD.Row, 26, 30, 31) ' kukanCode, kanshu -> AH dropdown
Else
Call ClearKukanValidation(Me, cellAD.Row, 31)
End If
Next
End If
' === Column AG changes: Fill 区間3 from M1/M2 cache ===
If Target.Column = 33 And Target.Row >= 7 Then
Dim cellAG As Range
For Each cellAG In Target
If Trim(cellAG.Value) <> "" Then
Call CreateZ1TransportDropdown(Me, cellAG.Row, 37)
If Me.Cells(cellAG.Row, 37).Validation.Formula1 <> "" Then
Call FillKukanFromM1(Me, cellAG.Row, 33, Array(34, 35, 36)) ' AG->AH,AI,AJ
Call CreateM2Dropdown(Me, cellAG.Row, 33, 37) ' AG->AK dropdown
End If
Else
Call ClearKukanValidation(Me, cellAG.Row, 37)
End If
Next
End If
' === Column AK changes: Fill 券種 dropdown -> generate AL dropdown ===
If Target.Column = 37 And Target.Row >= 7 Then
Dim cellAK As Range
For Each cellAK In Target
If Trim(cellAK.Value) <> "" Then
Call CreateM2CodeDropdown(Me, cellAK.Row, 33, 37, 38) ' kukanCode, kanshu -> AL dropdown
Else
Call ClearKukanValidation(Me, cellAK.Row, 38)
End If
Next
End If
' === Column AN changes: Fill 区間4 from M1/M2 cache ===
If Target.Column = 40 And Target.Row >= 7 Then
Dim cellAN As Range
For Each cellAN In Target
If Trim(cellAN.Value) <> "" Then
Call CreateZ1TransportDropdown(Me, cellAN.Row, 44)
If Me.Cells(cellAN.Row, 44).Validation.Formula1 <> "" Then
Call FillKukanFromM1(Me, cellAN.Row, 40, Array(41, 42, 43)) ' AN->AO,AP,AQ
Call CreateM2Dropdown(Me, cellAN.Row, 40, 44) ' AN->AR dropdown
End If
Else
Call ClearKukanValidation(Me, cellAN.Row, 44)
End If
Next
End If
' === Column AR changes: Fill 券種 dropdown -> generate AS dropdown ===
If Target.Column = 44 And Target.Row >= 7 Then
Dim cellAR As Range
For Each cellAR In Target
If Trim(cellAR.Value) <> "" Then
Call CreateM2CodeDropdown(Me, cellAR.Row, 40, 44, 45) ' kukanCode, kanshu -> AS dropdown
Else
Call ClearKukanValidation(Me, cellAR.Row, 45)
End If
Next
End If
End Sub
' Fill kukan info from M1 cache (key: kukan code, value: 7 elements)
Private Sub FillKukanFromM1(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal codeCol As Long, ByVal fillCols As Variant)
If m1Cache Is Nothing Then Call RefreshM1Cache
Dim code As String: code = Trim(ws.Cells(rowNum, codeCol).Value)
If code = "" Then Exit Sub
If m1Cache.Exists(code) Then
Dim vals As Variant: vals = m1Cache(code)
' value(1):value(2) -> fillCols(0)
ws.Cells(rowNum, fillCols(0)).Value = Trim(vals(1)) & ": " & Trim(vals(2))
' value(3) -> fillCols(1)
ws.Cells(rowNum, fillCols(1)).Value = Trim(vals(3))
' value(4) -> fillCols(2)
ws.Cells(rowNum, fillCols(2)).Value = Trim(vals(4))
Else
ws.Cells(rowNum, codeCol).ClearContents
End If
End Sub
' Create dropdown from M2 cache for ticket type column
Private Sub CreateM2Dropdown(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal kukanCodeCol As Long, ByVal ticketCol As Long)
If m2Cache Is Nothing Then Call RefreshM2Cache
Dim kukanCode As String: kukanCode = Trim(ws.Cells(rowNum, kukanCodeCol).Value)
If kukanCode = "" Then Exit Sub
' Build dropdown list: get all kanshu (券種) for the kukanCode
Dim dropdownList As String
If m2Cache.Exists(kukanCode) Then
Dim innerDict As Object: Set innerDict = m2Cache(kukanCode)
Dim kanshu As Variant
For Each kanshu In innerDict.Keys
If dropdownList = "" Then
dropdownList = kanshu
Else
dropdownList = dropdownList & "," & kanshu
End If
Next kanshu
End If
If dropdownList <> "" Then
With ws.Range(ws.Cells(rowNum, ticketCol).Address).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.InputMessage = ""
End With
End If
End Sub
' Fill address dropdown from O1 cache
Private Sub FillAddressFromO1(ByVal rowNum As Long)
If o1Cache Is Nothing Then Call RefreshO1Cache
Dim empNo As String: empNo = Trim(Me.Cells(rowNum, 3).Value)
If empNo = "" Then Exit Sub
' Build dropdown list from O1 cache
Dim dropdownList As String
Dim key As Variant
For Each key In o1Cache.Keys
If dropdownList = "" Then
dropdownList = key
Else
dropdownList = dropdownList & "," & key
End If
Next key
' Create dropdown for I column (住所)
With Me.Range("I" & rowNum).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.InputMessage = ""
End With
End Sub
' Create transport (T) dropdown from Z1 cache
Private Sub CreateZ1TransportDropdown(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal col As Long)
If z1Cache Is Nothing Then Call RefreshZ1Cache
' Build dropdown list from Z1 cache keys
Dim dropdownList As String
Dim key As Variant
For Each key In z1Cache.Keys
If dropdownList = "" Then
dropdownList = key
Else
dropdownList = dropdownList & "," & key
End If
Next key
If dropdownList <> "" Then
With ws.Cells(rowNum, col).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.InputMessage = ""
End With
End If
End Sub
' Create station (利用区間発) dropdown from M1_KukanD cache
Private Sub CreateZ1StationDropdown(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal transportCol As Long, ByVal stationCol As Long)
If m1KukanDCache Is Nothing Then Call RefreshM1KukanDCache
Dim transport As String: transport = Trim(ws.Cells(rowNum, transportCol).Value)
If transport = "" Then Exit Sub
' Build dropdown list from M1_KukanD cache: get all F values for the transport (D)
Dim dropdownList As String
If m1KukanDCache.Exists(transport) Then
Dim innerDict As Object: Set innerDict = m1KukanDCache(transport)
Dim fValue As Variant
For Each fValue In innerDict.Keys
If dropdownList = "" Then
dropdownList = fValue
Else
dropdownList = dropdownList & "," & fValue
End If
Next fValue
End If
If dropdownList <> "" Then
With ws.Cells(rowNum, stationCol).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
End Sub
' Create destination (利用区間着) dropdown from M1_KukanD cache
' Structure: { D: { F: [G] } }
Private Sub CreateM1KukanDDropdown(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal transportCol As Long, ByVal stationFromCol As Long, ByVal stationToCol As Long)
If m1KukanDCache Is Nothing Then Call RefreshM1KukanDCache
Dim transport As String: transport = Trim(ws.Cells(rowNum, transportCol).Value)
Dim stationFrom As String: stationFrom = Trim(ws.Cells(rowNum, stationFromCol).Value)
If transport = "" Or stationFrom = "" Then Exit Sub
' Build dropdown list from M1_KukanD cache
Dim dropdownList As String
If m1KukanDCache.Exists(transport) Then
Dim innerDict As Object: Set innerDict = m1KukanDCache(transport)
If innerDict.Exists(stationFrom) Then
Dim arr As Object: Set arr = innerDict(stationFrom)
Dim gValue As Variant
For Each gValue In arr.Keys
If dropdownList = "" Then
dropdownList = gValue
Else
dropdownList = dropdownList & "," & gValue
End If
Next gValue
End If
End If
If dropdownList <> "" Then
With ws.Cells(rowNum, stationToCol).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
End Sub
' Clear validation for kukan columns
Private Sub ClearKukanValidation(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal col As Long)
ws.Cells(rowNum, col).Validation.Delete
End Sub
' Create dropdown from M2 cache: get code (J列) list for kukanCode + kanshu
Private Sub CreateM2CodeDropdown(ByVal ws As Worksheet, ByVal rowNum As Long, ByVal kukanCodeCol As Long, ByVal kanshuCol As Long, ByVal codeCol As Long)
If m2Cache Is Nothing Then Call RefreshM2Cache
Dim kukanCode As String: kukanCode = Trim(ws.Cells(rowNum, kukanCodeCol).Value)
Dim kanshu As String: kanshu = Trim(ws.Cells(rowNum, kanshuCol).Value)
If kukanCode = "" Or kanshu = "" Then Exit Sub
' Build dropdown list: get all code for kukanCode + kanshu
Dim dropdownList As String
If m2Cache.Exists(kukanCode) Then
Dim innerDict As Object: Set innerDict = m2Cache(kukanCode)
If innerDict.Exists(kanshu) Then
Dim innermostDict As Object: Set innermostDict = innerDict(kanshu)
Dim code As Variant
For Each code In innermostDict.Keys
If dropdownList = "" Then
dropdownList = code
Else
dropdownList = dropdownList & "," & code
End If
Next code
End If
End If
If dropdownList <> "" Then
With ws.Cells(rowNum, codeCol).Validation
.Delete
.Add Type:=xlValidateList, Formula1:=dropdownList
.IgnoreBlank = True
.InCellDropdown = True
End With
End If
End Sub
' Clear row data
Private Sub ClearRowData(ByVal ws As Worksheet, ByVal rowNum As Long)
ws.Range(ws.Cells(rowNum, 4), ws.Cells(rowNum, END_COL)).ClearContents
ws.Cells(rowNum, ERROR_COL).ClearContents
End Sub
' ====== Button Macros ======
Sub C1_validateButton()
Dim lastRow As Long, r As Long, errorCount As Long
lastRow = GetLastDataRowInRange(Me, START_COL, END_COL)
If lastRow < 7 Then
MsgBox "No data found.", vbExclamation
Exit Sub
End If
errorCount = 0
For r = 7 To lastRow
Call Validate(r)
If Trim(Cells(r, ERROR_COL).Value) <> "" Then
errorCount = errorCount + 1
End If
Next r
MsgBox "Validation complete. Errors: " & errorCount, vbInformation
End Sub
' Validation logic
Private Sub Validate(ByVal rowNum As Long)
Set ws = Me
' Clear background color
ws.Range(ws.Cells(rowNum, START_COL), ws.Cells(rowNum, END_COL)).Interior.Color = vbWhite
' Required columns: C-G, K-N, AW
Dim requiredCols As Variant
requiredCols = Array("C", "D", "E", "F", "G", "K", "L", "M", "N", "AW")
Dim col As Variant
For Each col In requiredCols
If Trim(ws.Range(col & rowNum).Value & "") = "" Then
ws.Cells(rowNum, ERROR_COL).Value = col & " column is required"
ws.Range(col & rowNum).Interior.Color = RGB(255, 0, 0)
Exit Sub
End If
Next col
ws.Cells(rowNum, ERROR_COL).ClearContents
End Sub
Sub C1_SortDataRowsByC()
Call SortDataRows(3)
End Sub
Sub C1_ToggleAutoFilter()
Call ToggleAutoFilter(START_COL, END_COL)
End Sub
Sub C1_AutoFitColumnWidth()
Call AutoFitColumnWidth(START_COL, END_COL)
End Sub