0% found this document useful (0 votes)
261 views

VB Script For Peak Detection

This VBA code performs the following steps: 1. Imports data from columns A and B into arrays X and Y. 2. Plots the data on a scatter plot graph with the values in columns A and B on the x and y axes. 3. Calculates a rolling average in column C and identifies peaks in the data that meet a threshold condition. 4. Outputs the peak values and their corresponding x-values to columns D through F. 5. Reformats the data arrays and adds the peak values to the scatter plot graph.

Uploaded by

Terence
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
261 views

VB Script For Peak Detection

This VBA code performs the following steps: 1. Imports data from columns A and B into arrays X and Y. 2. Plots the data on a scatter plot graph with the values in columns A and B on the x and y axes. 3. Calculates a rolling average in column C and identifies peaks in the data that meet a threshold condition. 4. Outputs the peak values and their corresponding x-values to columns D through F. 5. Reformats the data arrays and adds the peak values to the scatter plot graph.

Uploaded by

Terence
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Sub DrawGraph()

Dim X
Dim lngRow As Long
Dim lngDestRow As Long
Dim lngSize As Long
X = Range([A1], Cells(Rows.Count, "B").End(xlUp))
lngDestRow = UBound(X, 1) + 1
ActiveSheet.Cells(1, 17).Value = "Rows = " & lngDestRow
X = Range([J2], Cells(Rows.Count, "K").End(xlUp))
ActiveSheet.Cells(1, "D").Value = "Peak kN)"
For lngRow = 1 To UBound(X, 1)
ActiveSheet.Cells(lngDestRow, "A").Value = X(lngRow, 1)
ActiveSheet.Cells(lngDestRow, "D").Value = X(lngRow, 2)
lngDestRow = lngDestRow + 1
Next lngRow

'

Columns("A:D").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlXYScatterLinesNoMarkers
ActiveChart.SetSourceData Source:=Range("$A:$D")
ActiveChart.SeriesCollection(3).Select
With Selection
.MarkerStyle = xlMarkerStyleX
.MarkerSize = 5
'.MarkerBackgroundColorIndex = xlColorIndexNone
'.MarkerForegroundColorIndex = xlColorIndexAutomatic
End With
Selection.MarkerStyle = 5
Selection.Format.Line.Visible = msoFalse

End Sub
Sub NewGraph()
Const Threshold = 10
Dim
Dim
Dim
Dim
Dim
Dim
Dim
Dim
Dim
Dim

X
Y
lngRow As Long
lngCnt As Long
Chr As ChartObject
lngPeak As Long
lngTrough As Long
Flag As Boolean
Peak, Trough As Double
lastRow As Long

Flag = False
'Remove header row if present
If ActiveSheet.Cells(2, 1).Value = "(mm)" Then
Range([A2], [B2]).Delete Shift:=xlUp
End If

lastRow = ActiveSheet.Cells(65536, 1).End(xlUp).Row


'Ensure extension data starts at zero
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("A1").Select
Selection.Copy
Range("B1").Select
ActiveSheet.Paste
Range("B2").Select
Application.CutCopyMode = False
ActiveCell.FormulaR1C1 = "=RC[-1]-R2C1"
Range("B2").Select
Selection.AutoFill Destination:=Range([B2], ActiveSheet.Cells(lastRow, 2))
Columns("B:B").Select
Selection.Copy
Columns("A:A").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Columns("B:B").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
'Calculate rolling average
Range("C10").Select
ActiveCell.FormulaR1C1 = "=AVERAGE(R[-8]C[-1]:RC[-1])"
Selection.AutoFill Destination:=Range([C10], ActiveSheet.Cells(lastRow, 3))
'Find peaks
X = Range([A1], Cells(Rows.Count, "c").End(xlUp))
Y = Application.Transpose(X)
lngCnt = 2
ActiveSheet.Cells(1, 10).Value = "Extension (mm)"
ActiveSheet.Cells(1, 11).Value = "Peak (N)"

'
'
'
/

For lngRow = 10 To UBound(X, 1) - 1


If X(lngRow, 3) > X(lngRow - 1, 3) Then
If X(lngRow, 3) > X(lngRow + 1, 3) Then
lngPeak = lngRow
Peak = X(lngRow, 2)
Flag = True
Else
Flag = False
End If
Else
If X(lngRow, 3) < X(lngRow + 1, 3) Then
If X(lngRow, 3) < X(lngRow - 1, 3) Then
If Flag Then
ActiveSheet.Cells(lngRow, 12).Value = X(lngRow, 2)
Trough = X(lngRow, 2)
ActiveSheet.Cells(lngRow, 13).Value = Peak - Trough
ActiveSheet.Cells(lngRow, 14).Value = Peak * (Threshold
100)
If (Peak - Trough) >= (Peak * (Threshold / 100)) Then
ActiveSheet.Cells(lngPeak, 4).Value = X(lngPeak, 2)
ActiveSheet.Cells(lngCnt, 10).Value = X(lngPeak, 1)
ActiveSheet.Cells(lngCnt, 11).Value = X(lngPeak, 2)

lngCnt = lngCnt + 1
End If
Flag = False

'
'
'

End If
End If
lngCnt = lngCnt + 1
Y(1, lngCnt) = X(lngRow, 1)
Y(2, lngCnt) = X(lngRow, 2)
End If
End If
Next lngRow
DrawGraph

'

ReDim Preserve Y(1 To 2, 1 To lngCnt)

'
'

Set Chr = ActiveSheet.ChartObjects.Add(250, 175, 275, 200)


With Chr.Chart

'
'
'
'
'
'

With .SeriesCollection.NewSeries
.XValues = Application.Index(Application.Transpose(Y), 0, 1)
.Values = Application.Index(Application.Transpose(Y), 0, 2)
End With
.ChartType = xlXYScatter
End With

End Sub

You might also like