VBA自动画图

来源:互联网 发布:json对象和json字符串 编辑:程序博客网 时间:2024/05/17 07:08
  1. Private Sub CommandButton1_Click()
  2.     Call doit
  3. End Sub
  4. Public column_range As String
  5. Public counter_num As Integer
  6. Sub getLinenum(range_num)
  7.     
  8.     column_range = column_range + CStr(range_num)
  9.     For Each c In Worksheets("TOP").range(column_range)
  10.         If c.Value <> "" Then
  11.             counter_num = counter_num + 1
  12.         Else
  13.             counter_num = range_num - 1000 + counter_num - 2
  14.             Exit For
  15.             
  16.         End If
  17.         
  18.         If counter_num = 1001 Then
  19.             column_range = "M"
  20.             column_range = column_range + CStr(range_num + 1)
  21.             range_num = range_num + 1000
  22.             column_range = column_range + ":M"
  23.             counter_num = 1
  24.             Call getLinenum(range_num)
  25.         End If
  26.     Next c
  27.     
  28. End Sub
  29. Sub nameParse(sheetname)
  30.     sheetname = Replace(sheetname, ":"" ")
  31.     sheetname = Replace(sheetname, "/", " ")
  32.     sheetname = Replace(sheetname, "/"" ")
  33.     sheetname = Replace(sheetname, "?"" ")
  34.     sheetname = Replace(sheetname, "*"" ")
  35.     sheetname = Replace(sheetname, "["" ")
  36.     sheetname = Replace(sheetname, "]"" ")
  37. End Sub
  38. Sub fillNewData(sheetname, start_line, end_line, new_range)
  39.     new_range = 1
  40.     row_range = end_line - start_line + 1
  41.     
  42.     merge_start = start_line
  43.     merge_end = merge_start
  44.     
  45.     Do While row_range > 0
  46.         cur_time = Format(Sheets("TOP").Cells(merge_end, 1).Value, "hh:mm:ss")
  47.         next_time = Format(Sheets("TOP").Cells(merge_end + 1, 1).Value, "hh:mm:ss")
  48.         If cur_time = next_time Then
  49.             merge_end = merge_end + 1
  50.         Else
  51.             new_range = new_range + 1
  52.             Sheets(sheetname).range("A" + CStr(new_range)).Formula = "=TOP!A" + CStr(merge_start)
  53.             Sheets(sheetname).range("C" + CStr(new_range)).Formula = "=SUM(TOP!" + "C" + CStr(merge_start) + ":C" + CStr(merge_end) + ")"
  54.             Sheets(sheetname).range("D" + CStr(new_range)).Formula = "=SUM(TOP!" + "D" + CStr(merge_start) + ":D" + CStr(merge_end) + ")"
  55.             Sheets(sheetname).range("E" + CStr(new_range)).Formula = "=SUM(TOP!" + "E" + CStr(merge_start) + ":E" + CStr(merge_end) + ")"
  56.             
  57.             merge_start = merge_end + 1
  58.             merge_end = merge_start
  59.         End If
  60.         row_range = row_range - 1
  61.     Loop
  62. End Sub
  63. Sub CreateSheet(sheetname, start_line, end_line)
  64.     
  65.     'check the sheetname
  66.     If Len(sheetname) > 31 Then
  67.         sheetname = Left(sheetname, 31)
  68.     End If
  69.     Call nameParse(sheetname)
  70.     
  71.     'create new sheet
  72.     Sheets.Add.Name = sheetname
  73.     
  74.     'copy data
  75.     'Sheets("TOP").Select
  76.     'Rows(CStr(start_line) + ":" + CStr(end_line)).Select
  77.     'Selection.Copy
  78.     'Sheets(sheetname).Select
  79.     'Rows("2:2").Select
  80.     'ActiveSheet.Paste
  81.     
  82.     'copy title
  83.     Sheets("TOP").Select
  84.     Rows("1:1").Select
  85.     Selection.Copy
  86.     Sheets(sheetname).Select
  87.     Rows("1:1").Select
  88.     ActiveSheet.Paste
  89.     
  90.     Call fillNewData(sheetname, start_line, end_line, new_range)
  91.     
  92.     'format data
  93.     row_num = new_range
  94.     For Each c In Sheets(sheetname).range("D2:" + "E" + CStr(row_num))
  95.         c.Value = c.Value * 0.01
  96.     Next c
  97.     Sheets(sheetname).range("D2:" + "E" + CStr(row_num)).Select
  98.     Selection.NumberFormatLocal = "0.00%"
  99.     
  100.     'create graph
  101.     graphname = sheetname + " CPU"
  102.     Charts.Add.Name = graphname
  103.     ActiveChart.ChartType = xlAreaStacked
  104.     range_string = "A1:" + "A" + CStr(row_num) + "," + "D1:" + "E" + CStr(row_num)
  105.     ActiveChart.SetSourceData Source:=Sheets(sheetname).range(range_string), _
  106.         PlotBy:=xlColumns
  107.     ActiveChart.Location Where:=xlLocationAsObject, Name:=sheetname
  108.     With ActiveChart
  109.         .HasTitle = True
  110.         .ChartTitle.Characters.Text = "CPU"
  111.         .Axes(xlCategory, xlPrimary).HasTitle = False
  112.         .Axes(xlValue, xlPrimary).HasTitle = False
  113.     End With
  114. End Sub
  115. Sub doit()
  116.     column_range = "M2:M"
  117.     counter_num = 2
  118.     range_num = 1000
  119.     
  120.     Call getLinenum(range_num)
  121.     MsgBox "There are " + CStr(counter_num) + " records in Top sheet."
  122.     
  123.     column_range = "M3:M"
  124.     column_range = column_range + CStr(counter_num + 2)
  125.     
  126.     cur_command = Worksheets("TOP").range("M2").Value
  127.     start_line = 2
  128.     end_line = start_line
  129.     
  130.     'sort
  131.     sortString = "A1:P" + CStr(counter_num + 1)
  132.     Sheets("TOP").Select
  133.     Worksheets("TOP").range("A1:P11").sort Key1:=range("M2"), Order1:=xlAscending, Key2:=range( _
  134.         "A2"), Order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase _
  135.         :=False, Orientation:=xlTopToBottom, SortMethod:=xlPinYin, DataOption1:= _
  136.         xlSortNormal, DataOption2:=xlSortNormal
  137.     For Each c In Worksheets("TOP").range(column_range)
  138.         If c.Value = cur_command Then
  139.             end_line = end_line + 1
  140.         Else
  141.             Call CreateSheet(cur_command, start_line, end_line)
  142.             cur_command = Worksheets("TOP").range("M" + CStr(end_line + 1)).Value
  143.             start_line = end_line + 1
  144.             end_line = start_line
  145.         End If
  146.     Next c
  147. End Sub

该程序功能为根据nmon的分析结果,将各个进程的分析数据制作成图形。

1,读TOP sheet, 获得记录的数目。

2,将TOP sheet 排序, first: command, second: time

3,为当前处理的command 创建新的sheet

4, 回到Top sheet 计算同一时间点上的user CPU占用率和sys CPU占用率的和

5,将该记录写入新创建的sheet, 根据数据画图

 

原创粉丝点击