Sub FEMacro()
Application.ScreenUpdating = True 'If false, then stops screen updating (aviods screen flicker, if any)
'Variable declaration (It seems it is not a necessary step, as I am able to get away without defining variable types, and directly using them as needed)
Dim FilePath As String 'File path where the current file is saved
Dim NewFileName As String 'Name of newly saved file (It is the file with data analysis)
Dim FileName As String 'Name of current file
Dim File4Ana As String 'Address and filename of the other excel file whose analysis is to be performed
TimeCycle = 1180 'Standard Cycle time for NEDC90 (units s)
IdlePost = 22 'Idling time at the very end of the cycle
IdlePre = 11 'Idling time at the very start of the cycle
'Default headings as obtained form CAN signal, they will be used to identify in which column data is store
SheetAna = "s201 g175 warm up behaviour tri" 'Sheet name where the data is present, checking previous data, this sheet name seems to be constant throught tests
TextOdo = "ODO_DISTANCE_EMS" 'Default heading for odo column
TextSpe = "VEHICLE_SPEED_EMS" 'Default heading for vehicle speed column
TextCon = "TotalConsumption" 'Default heading for total fuel consumption column
TNR = "Times New Roman" 'Font
Black = RGB(0, 0, 0) 'Colour for font
XaxTime = "Time (s)" 'X axis title for time
YaxFuel = "Fuel Consumption (l)" 'Y axis title for Fuel Consumption Chart
YaxSped = "Speed (km/h)" 'Y axis title for Speed Chart
SpedvTi = "Speed vs Time (NEDC 90)" 'Chart Title for Speed vs time
'Saves file names and path for file handling (opening and closing files)
FilePath = Application.ActiveWorkbook.Path 'File path where the current file is saved
FileName = Application.ActiveWorkbook.Name 'Name of this workbook (the one with macro written in it, may be redundant)
FileNameAna = Dir(FilePath & "" & "*.xlsx") 'Identifies name of the second
File4Ana = (FilePath & "" & FileNameAna) 'Saves full file path of file to be analysied
Range("A1") = File4Ana
'Opens file and switches focus to the corret sheet
Workbooks.Open FileName:=(File4Ana) 'Opens the file used for analysis
Workbooks(FileNameAna).Worksheets(SheetAna).Activate 'Switches focus to the excel sheet in excel file where Analysis is to be conducted
'Checks if the last row has data points for all columns, if not then it delets the last row
'Last row may not have data points for all columns because data logging could have stopped before their data was logged
Range("A1").Select 'Selects first heading cell
ActiveCell.Offset(3, 0).Select 'Moves down 3 spaces (based on how the excel file is generated, data starts from 3 cells below the heading)
Selection.End(xlDown).Select 'Moves down to the last cell and selects it
FCLR = ActiveCell.Address 'Gets cell address of first column last row
Range("A1").Select 'Selects first heading cell
Selection.End(xlToRight).Select 'Executes Ctrl + --> and moves to last heading, (assumes that there are no blank columns in between)
LastHead = ActiveCell.Address 'Gets cell address of last heading
'Performs the check here, if data is not uniformly present, it delets the last row
'This would help later when we plot various graphs, we wouldn't have to deal with mismatch in x and y data points
If IsEmpty(Cells(Range(FCLR).Row, Range(LastHead).Column)) = True Then
Rows(Range(FCLR).Row).Select
Selection.ClearContents
End If
'Finds Odo data, and stores address of first and last cell of the data column
Set HeadOdo = Range("A1", "CC1").Find(TextOdo, Lookat:=1) 'Searches for TextOdo in cells defined in Range, Lookat:=1 tells to perform a complete search and not to return value if a partial match is found. This is important otherwise, we would get seconds data and not the actual data
Range(HeadOdo.Address).Select 'Selects Heading Cell containing Odo data
ActiveCell.Offset(3, 0).Select 'Moves down 3 spaces (based on how the excel file is generated, data starts from 3 cells below the heading)
OdoStart = ActiveCell.Address 'Cell from where Odo readings start (units m)
Range(OdoStart).Select 'Selects OdoStart as the active cell (used to identify last cell in odo column)
Selection.End(xlDown).Select 'Moves down to the last cell and selects it
OdoEnd = ActiveCell.Address '(ReferenceStyle:=xlR1C1) 'Stores the address of last cell of odo column
'Finds fuel consumption data, and stores address of first and last cell of the data column
Set HeadCon = Range("A1", "CC1").Find(TextCon, Lookat:=1) 'Searches for TextCon in cells defined in Range, Lookat:=1 tells to perform a complete search and not to return value if a partial match is found. This is important otherwise, we would get seconds data and not the actual data
Range(HeadCon.Address).Select 'Selects Heading Cell containing fuel consumption data
ActiveCell.Offset(3, 0).Select 'Moves down 3 spaces (based on how the excel file is generated, data starts from 3 cells below the heading)
FuelStart = ActiveCell.Address 'Cell from where fuel consumption readings start (units l)
Range(FuelStart).Select 'Selects FuelStart as the active cell (used to identify last cell in fuel consumption column)
Selection.End(xlDown).Select 'Moves down to the last cell and selects it
FuelEnd = ActiveCell.Address '(ReferenceStyle:=xlR1C1) 'Stores the address of last cell of fuel consumption column
'Finds vehicle speed data, and stores cells address of first and last non zero speeds in the cycle
Set HeadSpe = Range("A1", "CC1").Find(TextSpe, Lookat:=1) 'Searches for TextCon in cells defined in Range, Lookat:=1 tells to perform a complete search and not to return value if a partial match is found. This is important otherwise, we would get seconds data and not the actual data
Range(HeadSpe.Address).Select 'Selects Heading Cell containing vehicle speed data
ActiveCell.Offset(3, 0).Select 'Moves down 3 spaces (based on how the excel file is generated, data starts from 3 cells below the heading)
MoveStart = ActiveCell.Address 'Cell from where fuel consumption readings start (units l)
'Keeps going one cell down at a time till it encounters a non zero cell
Do While Range(MoveStart) = 0
ActiveCell.Offset(1, 0).Select
MoveStart = ActiveCell.Address
Loop
Selection.End(xlDown).Select 'Moves down to the last cell and selects it
MoveEnd = ActiveCell.Address
'Keeps going one cell up at a time till it encounters a non zero cell
Do While Range(MoveEnd) = 0
ActiveCell.Offset(-1, 0).Select
MoveEnd = ActiveCell.Address
Loop
'Calculates time period Vehicle Movement Start to Vehicle Movement End (THIS CALCULATION SHOULD BE DONE ON TIME COLUMN WHICH SHOULD BE PROPERLY IDENTIFIED AND NOT HARD CODED)
Range(MoveStart).Select 'Selects MoveStart cell
ActiveCell.Offset(0, -1).Select 'Moves one column left, on to time column
MoveStartTime = ActiveCell.Address 'Stores address of time cell when movement starts
Range(MoveEnd).Select 'Selects MoveStart cell
ActiveCell.Offset(0, -1).Select 'Moves one column left, on to time column
MoveEndTime = ActiveCell.Address 'Stores address of time cell when movement starts
TimeMovement = Range(MoveEndTime) - Range(MoveStartTime) 'Movement time period
'FEs Calculation (FE is calculated by different methods)
'FE for the complete test
DistCoveredTotal = Range(OdoEnd) - Range(OdoStart) 'Total distance covered by the vehcile (m)
FuelConsumedTotal = Range(FuelEnd) - Range(FuelStart) 'Total fuel consumed by the vehicle (l)
FETotal = DistCoveredTotal / (1000 * FuelConsumedTotal) 'FE based on above values
Range("I2") = FETotal
'FE for EUDC (Initial idling is not considered in EUDC Cycle)
'Identifies point where EUDC Starts and ends
Range(MoveEnd).Select 'Selects cell when vehicle stops
ActiveCell.Offset(-3, 0).Select 'Selects 3 cell above when the vehicle stops, (we will now move up till we encouter 0 speed)
MoveEUDCStart = ActiveCell.Address 'Sets MoveEUDCStart as the current selected cell
Do While Range(MoveEUDCStart) > 0
ActiveCell.Offset(-1, 0).Select
MoveEUDCStart = ActiveCell.Address
Loop
'Determinates where EUDC should end (ideally after IdlePost number of cells after vehicle stops, but if data is not present for that region, it just selects the last cell)
If IsEmpty(Cells(Range(MoveEnd).Row + IdlePost, Range(MoveEnd).Column)) = True Then
MoveEUDCEnd = Cells(Range(OdoEnd).Row, Range(MoveEnd).Column).Address
Else
MoveEUDCEnd = Cells(Range(MoveEnd).Row + IdlePost, Range(MoveEnd).Column).Address
End If
DistCoveredEUDC = Cells(Range(MoveEUDCEnd).Row, Range(OdoStart).Column) - _
Cells(Range(MoveEUDCStart).Row, Range(OdoStart).Column) 'Calculates total distance covered in EUDC phase of NEDC
FuelConsumptionEUDC = Cells(Range(MoveEUDCEnd).Row, Range(FuelStart).Column) - _
Cells(Range(MoveEUDCStart).Row, Range(FuelStart).Column) 'Calculates total fuel consumed in EUDC phase of NEDC
FEEUDC = DistCoveredEUDC / (1000 * FuelConsumptionEUDC) 'FE in the EUDC cycle
Range("L2") = FEEUDC
'FE for UDC
MoveUDCEnd = MoveEUDCStart '(UDC ends when EUDC begins)
'Determines when UDC should start (it looks up IdlePre number of Cells above the first time vehicle started moving,
'if it finds data, them UDC starts from that point, if not then it assumes that cycle started from when the data logging started)
If (Range(MoveStart).Row - IdlePre) < 4 Then
MoveUDCStart = Cells(4, Range(MoveStart).Column).Address
Else
MoveUDCStart = Cells(Range(MoveStart).Row - IdlePre, Range(MoveStart).Column).Address
End If
DistCoveredUDC = Cells(Range(MoveUDCEnd).Row, Range(OdoStart).Column) - _
Cells(Range(MoveUDCStart).Row, Range(OdoStart).Column) 'Calculates total distance covered in EUDC phase of NEDC
FuelConsumptionUDC = Cells(Range(MoveUDCEnd).Row, Range(FuelStart).Column) - _
Cells(Range(MoveUDCStart).Row, Range(FuelStart).Column) 'Calculates total fuel consumed in EUDC phase of NEDC
FEUDC = DistCoveredUDC / (1000 * FuelConsumptionUDC) 'FE in the UDC cycle
Range("O2") = FEUDC
'Plotting all the relevant graphs
'Plotting speed vs time
Dim data As Worksheet
Set data = ActiveSheet
Range("C2") = ActiveSheet.Name
'Worksheets(SheetAna).Activate
Range("E2") = data.Name
Range("D2") = ActiveWorkbook.Name
''''''Range(Range(MoveUDCStart).Address, Range(MoveEUDCEnd).Offset(0, -1).Address).Select 'Selects range of cells from begining of UDC to end of EUDC
''''''Workbooks(FileNameAna).Worksheets(SheetAna).Activate
''''''ActiveSheet.Shapes.AddChart2(XlChartType:=xlXYScatterLinesNoMarkers).Select 'Creates a chart of XY Scatter Lines No Markers type
''''''Set SpeedvsTime = ActiveChart 'Gives a name to the selected chart
''''''With SpeedvsTime
'''''' With .ChartTitle 'Selects chart title for formatting
'''''' .Text = SpedvTi 'Sets title text
'''''' .Font.Name = TNR 'Sets title font
'''''' .Font.Color = Black 'Sets font colour as black
'''''' End With
'''''' With .Axes(xlCategory, xlPrimary) 'X axis
'''''' .HasTitle = True 'Enables X axis
'''''' With .AxisTitle
'''''' .Text = XaxTime 'Sets X axis title
'''''' .Font.Name = TNR 'Sets font
'''''' .Font.Size = 12 'Sets font size
'''''' .Font.Color = Black 'Sets font colour
'''''' End With
'''''' End With
'''''' With .Axes(xlValue, xlPrimary) 'Y axis
'''''' .HasTitle = True 'Enables Y axis
'''''' With .AxisTitle
'''''' .Text = YaxFuel 'Sets Y axis title
'''''' .Font.Name = TNR 'Sets font
'''''' .Font.Size = 12 'Sets font size
'''''' .Font.Color = Black 'Sets font colour
'''''' End With
'''''' End With
'''''' .Parent.Height = 325
'''''' .Parent.Width = 500
'''''' .Parent.Top = 100
'''''' .Parent.Left = 100
''''''End With
End Sub