Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sub COCMacroV1point1()
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim track, startT, endT, lastmin, inta, intb As Integer
- Dim origin As Variant
- Dim dirloc, data_id, COCfile As String
- 'Use these defaults for testing
- ' lastmin = 150
- ' startT = 1
- ' endT = 2
- ' dirloc = "/Users/moberemk/Desktop/macros"
- ' data_id = "test"
- dirloc = InputBox("Where are your files located?") '"F:\Camille at WC\Colour Project\Expt 1\Pilot-Finger-p1"
- data_id = InputBox("What is your data set I.D.? e.g. the date (Do not use spaces)")
- startT = InputBox("What is the first track number?")
- endT = InputBox("What is the last track number?")
- lastmin = InputBox("How many minutes are there in this experiment?")
- COCfile = Format(dirloc) + Application.PathSeparator + "Common Origin Coordinates_" + Format(data_id) + ".xls"
- 'Build the final workbook
- Workbooks.Add
- 'Generate the track headers
- For track = startT To endT
- Cells(1, (2 * track) - 1) = "Track " + Format(track)
- Cells(1, 2 * track) = "Track " + Format(track)
- Cells(2, (2 * track) - 1) = "X"
- Cells(2, 2 * track) = "Y"
- Next
- 'Save the workbook; there's no need to close it as Excel is smart enough to
- 'switch between windows when they're already open
- ActiveWorkbook.SaveAs Filename:=Format(COCfile), FileFormat:=xlExcel8
- 'Open all the track sheets and copy out the data
- For track = startT To endT
- 'Open the workbook to read time series data out
- 'Call MainCalc(dirloc, endtrl, part, colseq, namefile, pfile, samplingrate, startp, endp)
- 'MsgBox "Opening " + Format(dirloc) + Application.PathSeparator + "Track " + Format(track) + ".csv"
- Workbooks.Open Filename:=Format(dirloc) + Application.PathSeparator + "Track " + Format(track) + ".csv"
- Range("C3", "D" + Format(lastmin + 2)).Select
- Selection.Copy
- ActiveWorkbook.Close
- 'Copy data from the workbook into the combined file
- Workbooks.Open Filename:=Format(COCfile)
- Cells(3, (track * 2) - 1).Select
- ActiveSheet.Paste
- 'NOTE The save here is necessary and if removed causes all data to be lost;
- 'it can probably be omitted by fixing the .Close call above
- ActiveWorkbook.Save
- Next track
- Workbooks.Open Filename:=Format(COCfile)
- ' Generate cell header rows
- Cells(lastmin + 4, 1) = "Common Origin Coordinates"
- For track = startT To endT
- Cells(lastmin + 5, (2 * track) - 1) = "Track " + Format(track)
- Cells(lastmin + 6, (2 * track) - 1) = "X"
- Cells(lastmin + 5, 2 * track) = "Track " + Format(track)
- Cells(lastmin + 6, 2 * track) = "Y"
- Next
- ' Write the origin-accounted-for second part of the sheet
- Dim xAverage as Double
- xAverage = 0.0
- Dim yAverage as Double
- yAverage = 0.0
- For inta = 1 To (endT * 2)
- Dim finalValue As Double
- origin = Cells(3, inta)
- ' Iterate over the cells
- For intb = 3 To lastmin + 2
- newVal = (Cells(intb, inta) - origin) * 1.024
- Cells(intb + lastmin + 4, inta) = newVal
- finalValue = newVal
- Next
- ' Format the final value
- Cells((lastmin * 2) + 6 + 1, inta) = finalValue
- Cells((lastmin * 2) + 6 + 1, inta).Font.Bold = true
- 'Add to the rolling average
- MsgBox "inta:" + Format(inta) + "xAverage:" + Format(xAverage) + "finalValue:" + Format(finalValue)
- If (inta Mod 2) = 0 Then
- If yAverage = 0 Then
- yAverage = finalValue
- Else
- yAverage = (yAverage + finalValue) / 2
- End If
- Else
- If xAverage = 0 Then
- xAverage = finalValue
- Else
- xAverage = (xAverage + finalValue) / 2
- End If
- End If
- Next inta
- Dim bottomOfCalculations as Integer
- bottomOfCalculations = (lastmin * 2) + 6 + 2
- 'Write the averages
- Cells(bottomOfCalculations + 1, 1) = "X Average"
- Cells(bottomOfCalculations + 2, 1) = xAverage
- Cells(bottomOfCalculations + 1, 2) = "Y Average"
- Cells(bottomOfCalculations + 2, 2) = yAverage
- ActiveWorkbook.Save
- 'ActiveWorkbook.Close
- MsgBox "All tracks have been analyzed.", Title:="COC Macro v2.1"
- End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement