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