I'm logging data from an embedded platform over UART. This gets saved to a log file and I want to process the saved log with Excel.
I have no idea whether what I did was even remotely a good solution. My VBA experience so far consists mainly of code only used once. However, since I'll have to use this for every measurement series I conduct it's probably a good idea to clean it up. Not in the least because I can't stand ugly code, I'm about to expand on it and colleagues may want to use it in the near future as well.
Example input:
00032
00016
00016
00016
00032
00064
00080
00096
00112
00128
00160
00192
The size of the input can be thousands of values. All leading zeroes get dropped as intended. Values can be negative.
Option Explicit
Public Sub Stats()
'
' Stats Macro
' Give stats to column A and draw chart with trendline.
'
' Keyboard Shortcut: Ctrl+Shift+M
'
Columns("A:A").Select
Selection.FormatConditions.AddColorScale ColorScaleType:=3
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
Selection.FormatConditions(1).ColorScaleCriteria(1).Type = _
xlConditionValueLowestValue
With Selection.FormatConditions(1).ColorScaleCriteria(1).FormatColor
.Color = 7039480
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(2).Type = _
xlConditionValuePercentile
Selection.FormatConditions(1).ColorScaleCriteria(2).Value = 50
With Selection.FormatConditions(1).ColorScaleCriteria(2).FormatColor
.Color = 8711167
.TintAndShade = 0
End With
Selection.FormatConditions(1).ColorScaleCriteria(3).Type = _
xlConditionValueHighestValue
With Selection.FormatConditions(1).ColorScaleCriteria(3).FormatColor
.Color = 8109667
.TintAndShade = 0
End With
ActiveSheet.Shapes.AddChart2(227, xlLine).Select
ActiveChart.SetSourceData Source:=Range("Sheet1!$A:$A")
ActiveChart.FullSeriesCollection(1).Trendlines.Add Type:=xlMovingAvg, Period _
:=2, Forward:=0, Backward:=0, DisplayEquation:=0, DisplayRSquared:=0, _
Name:="2 per. Mov. Avg. (Series1)"
ActiveChart.ClearToMatchStyle
ActiveChart.ChartStyle = 233
ActiveChart.FullSeriesCollection(1).Trendlines(1).Select
With Selection.Format.Line
.Visible = msoTrue
.ForeColor.ObjectThemeColor = msoThemeColorAccent2
.ForeColor.TintAndShade = 0
.ForeColor.Brightness = -0.25
.Transparency = 0
End With
Range("C1").Select
ActiveCell.FormulaR1C1 = "Average:"
Range("D1").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(C[-3])"
Range("C2").Select
ActiveCell.FormulaR1C1 = "Minimum:"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=MIN(C[-3])"
Range("C3").Select
ActiveCell.FormulaR1C1 = "Maximum:"
Range("D3").Select
ActiveCell.FormulaR1C1 = "=MAX(C[-3])"
Range("C4").Select
ActiveCell.FormulaR1C1 = "Median:"
Range("D4").Select
ActiveCell.FormulaR1C1 = "=MEDIAN(C[-3])"
End Sub
Result looks like this:
I'm mainly worried about the lack of being generic. While ActiveCell
and ActiveSheet
and ActiveChart
work wonders, I haven't figured out how to apply the same logic everywhere. There are still values like Sheet1
which should be set automatically for the current sheet/object/etc..