fork download
  1. Option Explicit
  2.  
  3. Dim sh1 As Worksheet 'バラシシート
  4. Dim sh2 As Worksheet '短冊シート
  5. Public Sub 短冊シート設定4()
  6. Dim maxrow1 As Long
  7. Dim maxrow2 As Long
  8. Dim max_box As Long
  9. Dim i As Long
  10. Dim wrow As Long
  11. Dim boxNo As Long
  12. Dim seqNo As Long
  13. Dim box_row As Long
  14. Dim box_col As Long
  15. Dim pv As String
  16. Dim y As Long
  17. Set sh1 = Worksheets("バラシ")
  18. Set sh2 = Worksheets("短冊")
  19. maxrow1 = sh1.Cells(Rows.count, "S").End(xlUp).row 'S列の最大行取得
  20. If maxrow1 < 2 Then Exit Sub
  21. maxrow2 = sh2.Cells(Rows.count, "A").End(xlUp).row 'A列の最大行取得
  22. If (maxrow2 + 1) Mod 7 <> 0 Then
  23. MsgBox ("マス番号の行が不正")
  24. Exit Sub
  25. End If
  26. max_box = ((maxrow2 + 1) \ 7) * 3
  27. '短冊シートのマスをクリア
  28. For i = 1 To max_box
  29. Call clear_box(i)
  30. Next
  31. 'バラシシートを処理
  32. boxNo = 1
  33. seqNo = 0
  34. pv = ""
  35. For wrow = 2 To maxrow1
  36. If sh1.Cells(wrow, "S").Value = "" Then
  37. If seqNo > 0 Then
  38. boxNo = boxNo + 2
  39. seqNo = 0
  40. pv = ""
  41. End If
  42. Else
  43. seqNo = seqNo + 1
  44. If (seqNo Mod 4) <> 1 And pv <> sh1.Cells(wrow, "S").Value Then
  45. y = (seqNo - 1) \ 4
  46. seqNo = (y + 1) * 4 + 1
  47. End If
  48. If seqNo > 24 Then
  49. boxNo = boxNo + 1
  50. seqNo = 1
  51. End If
  52. 'マス番号とマス内番号に対応する位置を取得
  53. Call get_pos_in_box(boxNo, seqNo, box_row, box_col)
  54. '該当位置へS列データを設定
  55. sh2.Cells(box_row, box_col).Value = sh1.Cells(wrow, "S").Value
  56. sh2.Cells(box_row, box_col).Interior.Color = sh1.Cells(wrow, "S").Interior.Color
  57. pv = sh1.Cells(wrow, "S").Value
  58. End If
  59. Next
  60. MsgBox ("完了")
  61. End Sub
  62.  
  63. '指定マスクリア
  64. Private Sub clear_box(ByVal box_no As Long)
  65. Dim box_row As Long
  66. Dim box_col As Long
  67. Dim i As Long
  68. For i = 1 To 24
  69. Call get_pos_in_box(box_no, i, box_row, box_col)
  70. sh2.Cells(box_row, box_col).ClearContents
  71. sh2.Cells(box_row, box_col).Interior.Pattern = xlNone
  72. Next
  73. End Sub
  74. '指定マス内の指定位置取得
  75. Private Sub get_pos_in_box(ByVal box_no As Long, ByVal seq_no As Long, ByRef box_row As Long, ByRef box_col As Long)
  76. Dim new_seq_no As Long
  77. new_seq_no = 25 - seq_no
  78. Call get_pos_in_box_org(box_no, new_seq_no, box_row, box_col)
  79. End Sub
  80.  
  81. '指定マス内の指定位置取得(オリジナル)
  82. Private Sub get_pos_in_box_org(ByVal box_no As Long, ByVal seq_no As Long, ByRef box_row As Long, ByRef box_col As Long)
  83. Dim x1 As Long
  84. Dim x2 As Long
  85. Dim y1 As Long
  86. Dim y2 As Long
  87. y1 = (box_no - 1) \ 3
  88. y2 = (seq_no - 1) \ 4
  89. box_row = y1 * 7 + 1 + y2
  90. x1 = (box_no - 1) Mod 3
  91. x2 = (seq_no - 1) Mod 4
  92. box_col = x1 * 5 + 2 + x2
  93. End Sub
  94.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty