fork download
  1. Option Explicit
  2.  
  3. Private lastrow As Long
  4. Private maxcol As Long
  5. Private maxrow As Long
  6. Private saverow As Long
  7. Private sortedFlag As Boolean
  8. Private gmakedFlag As Boolean
  9. Const baseSheetName As String = "成績表"
  10. Const workSheetName As String = "作業"
  11. Const No_koutei As Long = 19 '工程数
  12. Const X_ratio As Double = 1# 'グラフ横軸倍率
  13. Const Y_ratio As Double = 1# 'グラフ縦軸倍率
  14. Const Y_unit As Long = 5 'グラフ縦軸目盛単位
  15.  
  16. Private Sub UserForm_Initialize()
  17. Dim i As Long
  18. Call CopyData
  19. For i = 3 To maxrow
  20. 名前リスト.AddItem Cells(i, 1)
  21. Next i
  22. End Sub
  23.  
  24. Private Sub 名前リスト_Click()
  25. Dim i As Long
  26. Dim j As Long
  27. Dim row As Long
  28. Dim col As Long
  29. Dim no As Long
  30. Call CopyData
  31. no = 名前リスト.ListIndex
  32. row = no + 3
  33. '選択した氏名と成績をコピー
  34. Range(Cells(saverow, 1), Cells(saverow, maxcol)).Value = Range(Cells(row, 1), Cells(row, maxcol)).Value
  35. '順位を設定(1期~4期)
  36. For i = 1 To 4
  37. For j = 1 To No_koutei
  38. col = GetCol2(i, j)
  39. If Cells(saverow, col).Value <> "" Then
  40. Cells(saverow + 1, col).Value = WorksheetFunction.Rank(Cells(saverow, col).Value, Range(Cells(3, col), Cells(maxrow, col)), 0)
  41. End If
  42. Next
  43. Next
  44. '選択された氏名の背景色を設定(1期~4期)
  45. For i = 1 To 4
  46. col = GetCol1(i)
  47. Range(Cells(row, col), Cells(row, col + No_koutei)).Interior.Color = 15773696
  48. Next
  49. lastrow = row
  50. End Sub
  51. '並べ替えボタンクリック
  52. Private Sub btnsort_Click()
  53. Dim i As Long
  54. Dim j As Long
  55. Dim col As Long
  56. If sortedFlag = True Then
  57. MsgBox ("既に並べ替え済み")
  58. Exit Sub
  59. End If
  60. If lastrow = 0 Then
  61. MsgBox ("名前を選択して下さい")
  62. Exit Sub
  63. End If
  64. '1期~4期まで繰り返す
  65. For i = 1 To 4
  66. For j = 1 To No_koutei
  67. col = GetCol2(i, j)
  68. Range(Cells(3, col), Cells(maxrow, col)).Sort key1:=Cells(3, col), order1:=xlDescending
  69. Next
  70. Next
  71. sortedFlag = True
  72. End Sub
  73.  
  74. '期(i=1~4)を与えて、そのカラム位置(氏名のカラム位置)を返す
  75. Private Function GetCol1(ByVal i As Long) As Long
  76. GetCol1 = (i - 1) * (No_koutei + 2) + 1
  77. End Function
  78. '期(i=1~4)と工程(j=1~19)を与えて、そのカラム位置を返す
  79. Private Function GetCol2(ByVal i As Long, ByVal j As Long) As Long
  80. GetCol2 = (i - 1) * (No_koutei + 2) + j + 1
  81. End Function
  82. '期(i=1~4)を与えて、その期にデータがあるか否かを返す(true:有 false:無)
  83. Private Function HasData(ByVal i As Long) As Boolean
  84. Dim col As Long
  85. Dim rg As Range
  86. col = GetCol1(i)
  87. HasData = False
  88. For Each rg In Range(Cells(3, col + 1), Cells(maxrow, col + No_koutei))
  89. If rg.Value <> "" Then
  90. HasData = True
  91. Exit For
  92. End If
  93. Next
  94. End Function
  95. '元データを作業シートへコピー
  96. Private Sub CopyData()
  97. Dim i As Long
  98. Dim rg As String
  99. Dim end_colstr As String
  100. maxcol = (No_koutei + 1) * 4 + 3
  101. end_colstr = ConvertToLetter(maxcol)
  102. maxrow = Worksheets(baseSheetName).Cells(Rows.Count, 1).End(xlUp).row
  103. '前回表示された内容を全てクリア
  104. Worksheets(workSheetName).Cells.Clear
  105. rg = "A1:" & end_colstr & maxrow
  106. '表の全領域をコピー(A1~CEx)(x=A列の最大行)
  107. Worksheets(baseSheetName).Range(rg).Copy Worksheets(workSheetName).Range(rg)
  108. '作業シートをアクティベイト
  109. Worksheets(workSheetName).Activate
  110. 'グラフを削除
  111. Worksheets(workSheetName).Cells(1, 1).Select
  112. If ActiveSheet.ChartObjects.Count > 0 Then
  113. ActiveSheet.ChartObjects.Delete
  114. End If
  115. 'フラグ及び変数の初期化
  116. lastrow = 0
  117. sortedFlag = False
  118. gmakedFlag = False
  119. saverow = maxrow + 2
  120. End Sub
  121. 'グラフボタンクリック
  122. Private Sub BtnGraph_Click()
  123. Dim left As Double
  124. Dim top As Double
  125. Dim width As Double
  126. Dim height As Double
  127. Dim col As Long
  128. Dim i As Long
  129. If gmakedFlag = True Then
  130. MsgBox ("既に作成済み")
  131. Exit Sub
  132. End If
  133. If lastrow = 0 Then
  134. MsgBox ("名前を選択して下さい")
  135. Exit Sub
  136. End If
  137. '左位置設定(=B列の開始位置)
  138. left = ActiveSheet.Range("A1").width
  139. '上位置設定(=A列最大行+5行目)
  140. top = ActiveSheet.Range("A1:A" & maxrow + 4).height
  141. '高さ
  142. height = 300 * Y_ratio
  143. '幅
  144. width = ActiveSheet.Range("B2").width * No_koutei * X_ratio
  145. '空のグラフ作成
  146. With ActiveSheet.ChartObjects.Add(left, top, width, height).Chart
  147. End With
  148. '作成したグラフのアクティベイト
  149. ActiveSheet.ChartObjects(1).Activate
  150. '1期から4期まで作成
  151. For i = 1 To 4
  152. '該当期のデータが全て空なら終了する
  153. If HasData(i) = False Then Exit For
  154. '新しくデータ系列を作成
  155. ActiveChart.SeriesCollection.NewSeries
  156. 'グラフの種類・データの範囲・凡例の使用を指定
  157. With ActiveChart.SeriesCollection(i)
  158. 'グラフの種類を設定 折れ線グラフ
  159. .ChartType = xlLineMarkers
  160. 'X軸の項目軸を指定
  161. .XValues = Range(Cells(2, 2), Cells(2, No_koutei + 1))
  162. 'データの指定
  163. col = GetCol1(i)
  164. .Values = Range(Cells(saverow + 1, col + 1), Cells(saverow + 1, col + No_koutei))
  165. '凡例の指定
  166. .Name = i & "期順位"
  167. End With
  168. Next
  169. 'スケール情報設定
  170. ActiveChart.Axes(xlValue, xlPrimary).MinimumScale = 1
  171. ActiveChart.Axes(xlValue, xlPrimary).MajorUnit = Y_unit
  172. ActiveChart.Axes(xlValue, xlPrimary).MaximumScale = GetMaxScale(maxrow - 2, Y_unit)
  173. ActiveChart.Axes(xlValue, xlPrimary).ReversePlotOrder = True
  174. 'タイトル設定
  175. With ActiveChart
  176. .HasTitle = True
  177. .ChartTitle.Text = Cells(saverow, 1).Value 'グラフ対象者の氏名
  178. End With
  179. gmakedFlag = True
  180. End Sub
  181.  
  182. '列番号を英文字に変換
  183. Function ConvertToLetter(ByVal iCol As Integer) As String
  184. Dim iAlpha As Integer
  185. Dim iRemainder As Integer
  186. iAlpha = Int((iCol - 1) / 26)
  187. iRemainder = iCol - (iAlpha * 26)
  188. If iAlpha > 0 Then
  189. ConvertToLetter = Chr(iAlpha + 64)
  190. End If
  191. If iRemainder > 0 Then
  192. ConvertToLetter = ConvertToLetter & Chr(iRemainder + 64)
  193. End If
  194. End Function
  195.  
  196. '人数とメモリ単位を与えてスケールの最大値を計算する
  197. Private Function GetMaxScale(no_person, unit)
  198. Dim i As Long
  199. i = 0
  200. Do While True
  201. GetMaxScale = i * unit + 1
  202. If GetMaxScale >= no_person Then Exit Do
  203. i = i + 1
  204. Loop
  205. End Function
  206.  
  207.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty