Advertisement
Guest User

Untitled

a guest
May 25th, 2016
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. rivate Sub CommandButton1_Click()
  2.  
  3. ''
  4. ' Interpolate data in the Up direction.'
  5. ''
  6. ' This subroutine uses the existing information on the worksheet about the indiecies'
  7. ' of the corresponding train data to search for the desired distance marker. The routine'
  8. ' then performs a linear interpolation between successive points.'
  9. ''
  10. ' Note: The only real difference between the Up and Down interpolation is the Match type'
  11. ' in the match function (-1) for UP.'
  12. ''
  13. Dim a As Integer
  14. ' Declare the loop integers.'
  15. Dim startIndex As Long, endIndex As Long
  16. Dim loopIndex As Integer, searchRange As Integer
  17. Dim offset As Integer, endInterpolation As Long
  18. Dim lastCell As Range
  19.  
  20. ' The number of trains to interpolate data for.'
  21. Dim numberOfTrains As Integer
  22.  
  23. ' Declare the X and Y values for calculating the gradient and intercept.'
  24. Dim X1 As Double, X2 As Double
  25. Dim Y1 As Double, Y2 As Double
  26.  
  27. ' Gradient and intercept placeholders.'
  28. Dim gradient As Double, yIntercept As Double
  29. ' Calculated speed placeholder.'
  30. Dim interpolatedSpeed As Double
  31.  
  32. ' Switch to manual calculation for faster processing.'
  33. Application.Calculation = xlCalculationManual
  34.  
  35. ' Determine the number of trains to interpolate the data for'
  36. numberOfTrains = WorksheetFunction.Count(Sheet2.Range("I:I"))
  37. offset = WorksheetFunction.Match("Distance", Sheet3.Range("A:A"), 0) + 1
  38. Set lastCell = Sheet3.Cells(Sheet3.Rows.Count, "A").End(xlUp)
  39. endInterpolation = lastCell.Row
  40.  
  41. ' Search through each train.'
  42. For searchRange = 1 To numberOfTrains
  43.  
  44. ' Get the start and end indecies of each train.'
  45. startIndex = Sheet3.Cells(4, 2 + searchRange)
  46. endIndex = Sheet3.Cells(5, 2 + searchRange)
  47.  
  48. ' Loop through the data for interpolation.'
  49. For loopIndex = offset To endInterpolation
  50.  
  51. ' Error Handler accounts for match function not finding the desired index, or a divide by zero error.'
  52. On Error GoTo ErrHandler:
  53. ' Get the closest distance values to the interpolated distance form the data.'
  54. X1 = WorksheetFunction.Index(Sheet2.Range("G" & startIndex, "G" & endIndex), _
  55. WorksheetFunction.Match(Sheet3.Range("A" & loopIndex), Sheet2.Range("G" & startIndex, "G" & endIndex), -1))
  56. X2 = WorksheetFunction.Index(Sheet2.Range("G" & startIndex, "G" & endIndex), _
  57. WorksheetFunction.Match(Sheet3.Range("A" & loopIndex), Sheet2.Range("G" & startIndex, "G" & endIndex), -1) + 1)
  58. ' Get the closest speed values for the corresponding distance values form the data.'
  59.  
  60. Y1 = WorksheetFunction.Index(Sheet2.Range("H" & startIndex, "H" & endIndex), _
  61. WorksheetFunction.Match(Sheet3.Range("A" & loopIndex), Sheet2.Range("G" & startIndex, "G" & endIndex), -1))
  62. Y2 = WorksheetFunction.Index(Sheet2.Range("H" & startIndex, "H" & endIndex), _
  63. WorksheetFunction.Match(Sheet3.Range("A" & loopIndex), Sheet2.Range("G" & startIndex, "G" & endIndex), -1) + 1)
  64.  
  65. ' Calculate the gradient and y-intercept.'
  66. gradient = (Y2 - Y1) / (X2 - X1)
  67. yIntercept = Y1 - gradient * X1
  68.  
  69. ' Calculate the new interpolated speed.'
  70. interpolatedSpeed = gradient * Sheet3.Range("A" & loopIndex) + yIntercept
  71.  
  72. ' Place the value in the desired cell.'
  73. Result:
  74. Sheet3.Cells(loopIndex, 2 + searchRange) = interpolatedSpeed
  75.  
  76. Next loopIndex
  77.  
  78. Next searchRange
  79.  
  80. ' If there is a zero in the data or a divide by zero error, replace the speed with 0.'
  81. ErrHandler:
  82. If Err.Number <> 0 Then
  83. interpolatedSpeed = 0
  84. Resume Result:
  85. End If
  86.  
  87. ' Revert back to automatic calculation mode.'
  88. Application.Calculation = xlCalculationAutomatic
  89.  
  90. ' Save the active workbook.'
  91. ActiveWorkbook.Save
  92. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement