Advertisement
Guest User

Untitled

a guest
Mar 21st, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Sub COCMacroV1point1()
  2.  
  3.   Application.ScreenUpdating = False
  4.   Application.DisplayAlerts = False
  5.  
  6.   Dim track, startT, endT, lastmin, inta, intb As Integer
  7.   Dim origin As Variant
  8.   Dim dirloc, data_id, COCfile As String
  9.  
  10.   'Use these defaults for testing
  11.  ' lastmin = 150
  12.  ' startT = 1
  13.  ' endT = 2
  14.  ' dirloc = "/Users/moberemk/Desktop/macros"
  15.  ' data_id = "test"
  16.  dirloc = InputBox("Where are your files located?") '"F:\Camille at WC\Colour Project\Expt 1\Pilot-Finger-p1"
  17.  data_id = InputBox("What is your data set I.D.? e.g. the date (Do not use spaces)")
  18.   startT = InputBox("What is the first track number?")
  19.   endT = InputBox("What is the last track number?")
  20.   lastmin = InputBox("How many minutes are there in this experiment?")
  21.  
  22.   COCfile = Format(dirloc) + Application.PathSeparator + "Common Origin Coordinates_" + Format(data_id) + ".xls"
  23.  
  24.   'Build the final workbook
  25.  Workbooks.Add
  26.   'Generate the track headers
  27.  For track = startT To endT
  28.       Cells(1, (2 * track) - 1) = "Track " + Format(track)
  29.       Cells(1, 2 * track) = "Track " + Format(track)
  30.       Cells(2, (2 * track) - 1) = "X"
  31.       Cells(2, 2 * track) = "Y"
  32.   Next
  33.   'Save the workbook; there's no need to close it as Excel is smart enough to
  34.  'switch between windows when they're already open
  35.  ActiveWorkbook.SaveAs Filename:=Format(COCfile), FileFormat:=xlExcel8
  36.  
  37.   'Open all the track sheets and copy out the data
  38.  For track = startT To endT
  39.       'Open the workbook to read time series data out
  40.      'Call MainCalc(dirloc, endtrl, part, colseq, namefile, pfile, samplingrate, startp, endp)
  41.      'MsgBox  "Opening " + Format(dirloc) + Application.PathSeparator + "Track " + Format(track) + ".csv"
  42.      Workbooks.Open Filename:=Format(dirloc) + Application.PathSeparator + "Track " + Format(track) + ".csv"
  43.       Range("C3", "D" + Format(lastmin + 2)).Select
  44.       Selection.Copy
  45.       ActiveWorkbook.Close
  46.  
  47.       'Copy data from the workbook into the combined file
  48.      Workbooks.Open Filename:=Format(COCfile)
  49.       Cells(3, (track * 2) - 1).Select
  50.       ActiveSheet.Paste
  51.       'NOTE The save here is necessary and if removed causes all data to be lost;
  52.      'it can probably be omitted by fixing the .Close call above
  53.      ActiveWorkbook.Save
  54.   Next track
  55.  
  56.   Workbooks.Open Filename:=Format(COCfile)
  57.  
  58.   ' Generate cell header rows
  59.  Cells(lastmin + 4, 1) = "Common Origin Coordinates"
  60.  
  61.   For track = startT To endT
  62.       Cells(lastmin + 5, (2 * track) - 1) = "Track " + Format(track)
  63.       Cells(lastmin + 6, (2 * track) - 1) = "X"
  64.       Cells(lastmin + 5, 2 * track) = "Track " + Format(track)
  65.       Cells(lastmin + 6, 2 * track) = "Y"
  66.   Next
  67.  
  68.   ' Write the origin-accounted-for second part of the sheet
  69.  Dim xAverage as Double
  70.   xAverage = 0.0
  71.   Dim yAverage as Double
  72.   yAverage = 0.0
  73.   For inta = 1 To (endT * 2)
  74.       Dim finalValue As Double
  75.       origin = Cells(3, inta)
  76.  
  77.       ' Iterate over the cells
  78.      For intb = 3 To lastmin + 2
  79.           newVal = (Cells(intb, inta) - origin) * 1.024
  80.           Cells(intb + lastmin + 4, inta) = newVal
  81.           finalValue = newVal
  82.       Next
  83.  
  84.       ' Format the final value
  85.      Cells((lastmin * 2) + 6 + 1, inta) = finalValue
  86.       Cells((lastmin * 2) + 6 + 1, inta).Font.Bold = true
  87.  
  88.       'Add to the rolling average
  89.      MsgBox "inta:" + Format(inta) + "xAverage:" + Format(xAverage) + "finalValue:" + Format(finalValue)
  90.       If (inta Mod 2) = 0 Then
  91.         If yAverage = 0 Then
  92.           yAverage = finalValue
  93.         Else
  94.           yAverage = (yAverage + finalValue) / 2
  95.         End If
  96.       Else
  97.         If xAverage = 0 Then
  98.           xAverage = finalValue
  99.         Else
  100.           xAverage = (xAverage + finalValue) / 2
  101.         End If
  102.       End If
  103.   Next inta
  104.  
  105.   Dim bottomOfCalculations as Integer
  106.   bottomOfCalculations = (lastmin * 2) + 6 + 2
  107.  
  108.   'Write the averages
  109.  Cells(bottomOfCalculations + 1, 1) = "X Average"
  110.   Cells(bottomOfCalculations + 2, 1) = xAverage
  111.   Cells(bottomOfCalculations + 1, 2) = "Y Average"
  112.   Cells(bottomOfCalculations + 2, 2) = yAverage
  113.  
  114.   ActiveWorkbook.Save
  115.   'ActiveWorkbook.Close
  116.  
  117.   MsgBox "All tracks have been analyzed.", Title:="COC Macro v2.1"
  118.  
  119. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement