fork download
  1. Option Explicit
  2. Const StartRow As Long = 6 'データ開始行(1以上を指定)
  3. Const BubanCol As String = "E" '部番列
  4. Const OutColTbl As String = "L,O,R,U,X,AA,AD,AG,AJ,AM,AP,AS,AV,AY,BB,BE" '-出力対象列
  5. Public Sub 納品対象外部品()
  6. Dim bias As Long
  7. Dim ws As Worksheet
  8. Dim lastRow As Long
  9. Dim wrow As Long
  10. Dim no_blk As Long
  11. Dim str As String
  12. Dim bno As Long
  13. Dim ino As Long
  14. Dim outcols As Variant
  15. outcols = Split(OutColTbl, ",")
  16. bias = StartRow - 1
  17. Set ws = ActiveSheet
  18. lastRow = ws.Cells(Rows.Count, BubanCol).End(xlUp).row
  19. If lastRow < (bias + 3) Or (lastRow - bias) Mod 3 <> 0 Then
  20. Call error(ws, lastRow, "最終行不正")
  21. End If
  22. no_blk = (lastRow - bias) \ 3
  23. Call clear_out_cols(ws, StartRow, lastRow, outcols) '全ての出力対象列をクリア
  24. For bno = 1 To no_blk
  25. Dim rowA As Long: rowA = 0
  26. Dim rowB As Long: rowB = 0
  27. Dim rowH As Long: rowH = 0
  28. Dim row_mark As Long: row_mark = 0
  29. Dim pv_wd As String: pv_wd = ""
  30. Dim en_wd As String
  31. For ino = 1 To 3
  32. wrow = bias + (bno - 1) * 3 + ino
  33. str = ws.Cells(wrow, BubanCol).Value
  34. Dim wd As String
  35. Select Case Right(str, 1)
  36. Case "A"
  37. If rowA <> 0 Then Call error(ws, wrow, "終端文字Aが2回出現")
  38. rowA = wrow
  39. wd = Left(str, Len(str) - 1)
  40. Case "B"
  41. If rowB <> 0 Then Call error(ws, wrow, "終端文字Bが2回出現")
  42. rowB = wrow
  43. wd = Left(str, Len(str) - 1)
  44. Case "H"
  45. If rowH <> 0 Then Call error(ws, wrow, "終端文字Hが2回出現")
  46. rowH = wrow
  47. en_wd = Right(str, 2)
  48. Select Case en_wd
  49. Case "AH"
  50. Case "BH"
  51. Case Else
  52. Call error(ws, wrow, "終端文字がAH,BHの何れかでない")
  53. End Select
  54. wd = Left(str, Len(str) - 2)
  55. Case Else
  56. Call error(ws, wrow, "終端文字がA,B,Hの何れかでない")
  57. End Select
  58. If Len(wd) = 0 Then Call error(ws, wrow, "部番の共通文字が空")
  59. If pv_wd = "" Then
  60. pv_wd = wd
  61. Else
  62. If pv_wd <> wd Then Call error(ws, wrow, "部番の共通文字が不一致")
  63. End If
  64. Next
  65. If en_wd = "AH" Then row_mark = rowA
  66. If en_wd = "BH" Then row_mark = rowB
  67. If row_mark = 0 Then Call error(ws, wrow, "納品対象外行が決定できない")
  68. Call set_out_cols(ws, row_mark, outcols) '全ての出力対象列へ-設定
  69. Next
  70. MsgBox ("完了")
  71. End Sub
  72. '全出力対象列をクリア
  73. Private Sub clear_out_cols(ws As Worksheet, ByVal st As Long, ByVal en As Long, outcols As Variant)
  74. Dim ocol As Variant
  75. For Each ocol In outcols
  76. ws.Range(ocol & st & ":" & ocol & en).ClearContents
  77. Next
  78. End Sub
  79. '全出力対象列へ-を出力
  80. Private Sub set_out_cols(ws As Worksheet, ByVal mrow As Long, outcols As Variant)
  81. Dim ocol As Variant
  82. For Each ocol In outcols
  83. ws.Cells(mrow, ocol).Value = "-"
  84. Next
  85. End Sub
  86. Private Sub error(ws As Worksheet, ByVal rowNo As Long, ByVal msg As String)
  87. ws.Cells(rowNo, BubanCol).Select
  88. MsgBox (msg)
  89. End
  90. End Sub
  91.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty