Dear All,
I've got a weird behavior of my macro. The data is x,y,z set of points (scan of a surface); I want to analyze the surface state, so at Y fixed I draw a plot (x,z), i get a x,y scatter plot of the section.
I need to detrend it so i use a 6th degree interpolation. I extract the interpolation from the trendline (TrendLineValue function in VBA) of the grape since LINEST at high degree is not working properly (I know it is dirty but I already spend a lot of time on that).
Then detrend the data, compute root mean square and store it in an array.
And I iterate through different Y (I call them steps).
The problem is between steps, the chart is not updated so trendline equation is not updated so value extracted by TrendLineValue are wrong....
I played around with DoEvents, Wait, application. Calculate but nothing is working....
Please do not mind all code related to FFT, I'm just trying to make this coefficient extraction working.
Thank you for your help.
Here is the file:
gofile.io/?c=q9X9YE
here is the code
![]()
Sub scanPlate() Dim Step_start As Integer Dim Step_stop As Integer Dim max_peaks As New Collection Dim RMS As New Collection Application.ScreenUpdating = True 'dy Step_start = ActiveSheet.Range("AH5").Value Step_stop = ActiveSheet.Range("AI5").Value 'dx ActiveSheet.Range("M11").Value = ActiveSheet.Range("AH4").Value ActiveSheet.Range("M12").Value = ActiveSheet.Range("AI4").Value Range("M1").Value = Step_start 'extract trend line coefficients of first step TrendLineValue Application.Calculate 'iteration through differetn steps of interest Dim J As Integer For J = Step_start To Step_stop Step 2 'step no Range("M1").Value = J 'FFT_MACRO: other macro that will be implemented later 'extract trend line coefficients of step J TrendLineValue Debug.Print Range("M32").Value 'store RMS and FTT value max_peaks.Add Range("M26").Value RMS.Add Range("M32").Value Debug.Print "step no: " & J Next J Debug.Print "scan finish" 'transform collection to arrays arrPeaks = toArray(max_peaks) arrRMS = toArray(RMS) 'display in the sheet all RMS and PEAK value Range("AA1:AA119") = Application.Transpose(arrPeaks) Range("AB1:AB119") = Application.Transpose(arrRMS) Range("Z1").Value = Step_start Application.Calculation = xlAutomatic End Sub
![]()
Function toArray(col As Collection) Dim Arr() As Variant ReDim Arr(0 To col.Count - 1) As Variant Dim i As Integer For i = 1 To col.Count Arr(i - 1) = col(i) Next toArray = Arr End Function Function TrendLineValue() Dim c As Chart Dim t As Trendline Dim s As String Dim i As Integer Dim remaining As String Dim c0, c1, c2, c3, c4, c5, c6 As String Dim Arr As Variant ReDim Arr(0 To 7) As Variant Application.CalculateFull ' Get the trend line object ' this code assumes the first chart on the active sheet, ' and the first series, first trendline Set c = ActiveSheet.ChartObjects(2).Chart Set t = c.SeriesCollection(1).Trendlines(1) ' make sure equation is displayed t.DisplayRSquared = False t.DisplayEquation = True ' set number format to ensure accuracy ' adjust to suit requirements t.DataLabel.NumberFormat = "0.0000000000E+00" ' get the equation s = t.DataLabel.Text ' massage the equation string into form that will evaluate ' this code assumes 6rd order polynomial s = Replace(s, "y = ", "") s = Replace(s, " ", "") 'extract each coefficients from string equation c6 = Left(s, InStr(1, s, "x") - 1) remaining = Right(s, Len(s) - InStr(1, s, "x") - 1) Arr(0) = c6 c5 = Left(remaining, InStr(1, remaining, "x") - 1) remaining = Right(remaining, Len(remaining) - InStr(1, remaining, "x") - 1) Arr(1) = c5 c4 = Left(remaining, InStr(1, remaining, "x") - 1) remaining = Right(remaining, Len(remaining) - InStr(1, remaining, "x") - 1) Arr(2) = c4 c3 = Left(remaining, InStr(1, remaining, "x") - 1) remaining = Right(remaining, Len(remaining) - InStr(1, remaining, "x") - 1) Arr(3) = c3 c2 = Left(remaining, InStr(1, remaining, "x") - 1) remaining = Right(remaining, Len(remaining) - InStr(1, remaining, "x") - 1) Arr(4) = c2 c1 = Left(remaining, InStr(1, remaining, "x") - 1) remaining = Right(remaining, Len(remaining) - InStr(1, remaining, "x") - 0) Arr(5) = c1 c0 = remaining Arr(6) = c0 'display coefficeint on the sheets Dim Destination As Range Set Destination = ActiveSheet.Range("AU6") Set Destination = Destination.Resize(UBound(Arr), 1) Destination.Value = Application.Transpose(Arr) End Function
Bookmarks