fork download
  1. Option Explicit
  2. Public Sub ボックス転置()
  3. Dim ws As Worksheet
  4. Dim rg As Range
  5. Dim maxrow As Long
  6. Dim maxcol As Long
  7. Dim rowb As Long
  8. Dim colb As Long
  9. Dim arr1 As Variant
  10. Dim arr2 As Variant
  11. Dim wrow As Long
  12. Dim wcol As Long
  13. Set ws = ActiveSheet
  14. Set rg = ws.Range("A1").CurrentRegion
  15. maxrow = rg.Rows.count
  16. maxcol = rg.Columns.count
  17. If maxrow Mod 4 <> 0 Then
  18. MsgBox ("行数が4の倍数でない")
  19. Exit Sub
  20. End If
  21. If maxcol Mod 4 <> 0 Then
  22. MsgBox ("列数が4の倍数でない")
  23. Exit Sub
  24. End If
  25. 'セルをarr1に転送
  26. arr1 = rg.Value
  27. ReDim arr2(1 To maxcol, 1 To maxrow)
  28. 'arr1からarr2へ転送
  29. For rowb = 1 To maxrow \ 4
  30. For colb = 1 To maxcol \ 4
  31. Call arr_move(rowb, colb, arr1, arr2)
  32. Next
  33. Next
  34. '罫線を消す
  35. rg.Borders.LineStyle = xlLineStyleNone
  36. 'シートクリア
  37. ws.Cells.ClearContents
  38. '配列2をセルへ転送
  39. ws.Range("A1").Resize(maxcol, maxrow).Value = arr2
  40. '罫線を引く
  41. With ws.Range("A1").Resize(maxcol, maxrow).Borders
  42. .LineStyle = True
  43. .Weight = xlThin '細線
  44. End With
  45. For wrow = 1 To maxcol Step 4
  46. For wcol = 1 To maxrow Step 4
  47. ws.Range(ws.Cells(wrow, wcol), ws.Cells(wrow, wcol + 3)).Borders.LineStyle = xlLineStyleNone
  48. ws.Range(ws.Cells(wrow, wcol), ws.Cells(wrow + 3, wcol + 3)).BorderAround True, xlMedium '太線
  49. Next
  50. Next
  51. End Sub
  52. 'ブロック単位でarr1からarr2へ転送
  53. Private Sub arr_move(ByVal rowb As Long, ByVal colb As Long, ByRef arr1 As Variant, ByRef arr2 As Variant)
  54. Dim row1 As Long
  55. Dim row2 As Long
  56. Dim col1 As Long
  57. Dim col2 As Long
  58. Dim r As Long
  59. Dim c As Long
  60. Dim temp As Variant
  61. For r = 1 To 4
  62. For c = 1 To 4
  63. row1 = (rowb - 1) * 4 + r
  64. col1 = (colb - 1) * 4 + c
  65. row2 = (colb - 1) * 4 + r
  66. col2 = (rowb - 1) * 4 + c
  67. arr2(row2, col2) = arr1(row1, col1)
  68. Next
  69. Next
  70. End Sub
  71.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty