'v3.00 - 2017 NPRM New LPM
'v3.10 - New Diagnostic
'        Redundant code removed from calc_LPM
'v3.20 - Unlimited Packages
'v3.21 - Additional documentation
'      - Removal of commented out code
'      - TURB 1,2,3 changed to TURB 21,24,27 in calc_LPM
'      - LPM result changed from A28 to A29
'      - Baseline vehicle types remapped into LPM vehicle types
'      - HE alternator and alternator regen added to IACC for LPM
'      - LRR Tires changed to LRRT1 & LRRT2
'      - Fixed scenario pointer to work with unlimited packages
'v3.22 - Weight reduction algorithm works with cap in baseline
'v3.23 - Moved columns Q and W to allow HEG
'v3.24 - Mass cap and technology penalty routine complete
'      - Added Atkinson Technology
'      - Remapped vehicle Types into LPM
'v3.25 - Expanded to 50 packages
'      - PHEV now has correct effectiveness
'v3.26 - Variable motor size based on LPM vehicle type complete
'v3.27 - Integers changed to Long to allow more than 32767 Scenario Packages
'v3.28 - LUB turned off from baseline vehicle if package contains EFR2
'      - Bypass Mass Caps option added
'v3.29 - Cam change (L37) implemented in LPM
'v3.30 - Leave baseline vehicles alone that are more effective than package
'      - Overall progress added to UI
'v3.31 - Mass rounded corrected
'v3.32 - Technology mass corrected
'v3.33 - Weight penalty for Diesel implemented
'      - Added bypass option for penalty
'v3.34 - Downsize %'s revised
'      - Added option to keep manual transmissions
'v3.35 - Do not allow HEG, ASL, or TORQ when a manual transmission is selected
'v3.36 - Added Cost Analysis Year option on control tab
'v3.37 - Hybrids and EVs work correctly with Ignore Weight Penalty option
'v3.38 - HEG now allowed on manual transmissions
'v3.39 - HEG scaler added in LPM
'      - No DOHC, LUB, or EFR for Diesel in LPM
'v3.40 - Add function to generate scenario cost for multiple years
'v3.41 - Corrected mass algorithm interferring with engine ranking in LPM
'      - Baseline EVs are no longer allowed to improve
'v3.42 - Added VE and VC sheet
'v3.43 - TEB and CEB not capped at 1
'      - ASL1 and IACC1 accepted into LPM
'v3.44 - MHEV technology added
'v3.45 - FRM Vehicle Types
'v3.46 - Use technology mass during scenario calculation
'v3.47 - Calculate credits
'v3.48 - New mild HEV effectiveness numbers from NHTSA
'v3.49 - Add more transmission rankings
'      - Reformat "OUTPUT TEB CEB" sheet
'v3.50 - Build diesel pkgs as shown in scenario with respect to WR
'v3.51 - Eliminate use of clipboard
'v3.52 - Attempt to bypass use of array in the copy and paste process
'      - VBA would not function correctly - No changes made to code
'v3.53 - Remove VE VC code and add program timer
'v3.54 - Not used
'v3.55 - New cost sheet structure for calculation efficiency
'v3.56 - Calculation efficiency for LPM
'v3.57 - eng_tech_load speed improvement
'v3.58 - LPM speed improvement 1
'v3.59 - replace_package speed improvement
'v3.60 - String test experiment in LPM - no speed improvement - revert to 3.59
'v3.61 - Add autosave and end feature
'v3.62 - Add run minimized feature
'v3.63 - Change from TEB CEB to actual cost and co2
'V3.64 - Add TECH X to Output TEB CEB page
'v3.65 - Reformat TECH X values
'v3.66 - LP model changed to accept Powersplit and Power-split
'v3.67 - Only calculate packages that exist in scenario file
'v3.68 - Clean up unused code
'v3.69 - MHEV now allowed to improve
'      - Baseline HEV's allowed to have mass reduction when not improved by package
'v3.70 - Added 3 columns of %WRcaps to Baseline tab and code to Initalize (sic) sub to set which range of caps to use
'v3.71 - Added WRcap_year variable to control WR cap year independently from cost_analysis_year
'v3.75 - Fix Wr items in pipe delimited strings
'v3.76 - Added global effectiveness adjuster to cell B8 of Control tab
'v3.77 - Fix leading 0 issue when populating cost sheet
'v3.80 - First new version for TAR with new columns for EGA and ATKCS
'      - EGR now in column R - ATKCS2 technology added - Miller cycle added
'v3.81 - DCT-tc technology added - EV200 technology added
'v3.82 - in the initialize sub, extended WRcap/penalty ranges through row 2500
'v3.83 - Debugging first machine with 2014 baseline
'      - Variables not clearing correctly in 2013 version between runs corrected
'      - Screen updating issue corrected
'v3.84 - New LPM as of 11/4/2015
'      - Atkinson delimited strings modified for multiple levels
'      - TURBM delimited strings added
'      - MHEV48V added
'      - Code modified for HEG1 and HEG2
'v4.00 - Major revision flagged for use with LPM V2
'v4.01 - Atkinson codes changed to ATK1 and ATK2
'v4.02 - EGR (column R) in pipe delimited strings stops reading at dash
'v4.03 - Baseline mass accounting working properly
'v4.04 - Baseline electrification maintained if not in package
'v4.05 - Doevents and cursor commands removed
'v4.06 - StoreWRcaps range extened from 2500 to 3500
'v4.07 - Add EV200
'v4.08 - Add MS & RS controllers
'v4.09 - minor tweaks to MS & RS controllers
'v4.10 - additional tweaks to save MS & RS controller inputs to separate files
'v4.11 - removed MS_generator sub and moved code to separate file
'v4.12 - Implement TRX transmissions
'v4.13 - Develop new LPM
'v4.14 - New VBA for LPM
'v4.15 - Updated MHEV and SHEV values for LPM
'v4.16 - Changed ordering of MHEV & MHEV48V so that MHEV48V comes first where important
'v4.17 - Changed TURBM downsize to 42% & new lines in Atkinson to select 2014 vs. 2020+ maps
'v4.18 - Add feature to keep baseline electified vehicles constant
'v4.19 - Added check for calculations to complete before continuing
'        Updated WR tracking logic to better match intended results
'v4.20 - Corrected Atkinson factor for TURBM packages
'v4.21 - New LPM interface and OEB modifications for MT
'v4.22 - LPM 4.0 and new control page
'v4.23 - StoreCO2 code added for unique CO2 depending on cost_analysis_year selection
'v4.24 - multiple MS controller now runs # of VehTypes as entered on input sheet rather than a hardcoded 19
'v4.25 - LPM 4.1 support
'v4.26 - LPM 4.2 support
'v4.27 - LPM 4.3 support
'v4.28 - Adjustment factors for PW added


Private userpic1 As String
Private loop_counter As Long
Private baseline_rank As Long
Private package_rank As Long
Private rank_input As String
Private rank_output As Long
Private copy_output As String
Private paste_output As String
Private str1 As String
Private str2 As String
Private str3 As String
Private str4 As String
Private vehicle_class As String
Private current_sheet As String
Private cost_sheet As String
Private cost_row_pointer As Long
Private temp_pointer As Long
Private scenario_pointer As Long
Private vehicle_counter As Long
Private package_number As Long
Private package_numberp As Long
Private technology_package As Long
Private progress_counter As Long
Private package_sheet As String
Private package_sheet_1 As String
Private vehicle_type As Long
Private reference_package As Long
Private vehicle_types As String
Private vc_column As String
Private ve_column As String
Private tc_column As String
Private te_column As String
Private vc_column_vevc As String
Private ve_column_vevc As String
Private tc_column_vevc As String
Private te_column_vevc As String
Private ppvc_column_vevc As String
Private ppve_column_vevc As String
Private pptc_column_vevc As String
Private ppte_column_vevc As String
Private vc_column_p As String
Private ve_column_p As String
Private tc_column_p As String
Private te_column_p As String
Private ceb_column As String
Private teb_column As String
Private mapping_column As String
Private mass_tech As String
Private ceb As Single
Private teb As Single
Private float1 As Single
Private float2 As Single
Private float3 As Single
Private float4 As Single
Private float5 As Single
Private float6 As Single
Private mass_cost As Single
Private technology_cost As Single
Private mass_effectiveness As Single
Private bl_row_pointer As Long
Private vehicle_counter_low As Long
Private vehicle_counter_high As Long
Private total_runs_complete As Long
Private mtflag As Integer
Private sub_type As Long
Private sub_type_temp As Long
Private int1 As Long
Private int2 As Long
Private int3 As Long
Private int4 As Long
Private int5 As Long
Private test As Long
Private ignore_penalty As Integer
Private cost_analysis_year As String
Private new_package As Integer
Private package_loop As Long
Private number_of_packages As Long
Private vcm(2000) As String
Private vem(2000) As String
Private tcm(2000) As String
Private tem(2000) As String
Private lpm_eff As Single
Private temp_sheet As String
Private temp_counter As Long
Private base_eff As Single
Private replace_package As Long
Private base_plugin As String
Private package_plugin As String
Private version_number As String
Private vehicle_type_pointer(30) As Long
Private vehicle_type_length(30) As Long
Private cost_year_counter As Integer
Private cost_year_iterations As Integer
Private mass_zero As Integer
Private credit_tech As String
Private credit_row As Long
Private credit_category As String
Private credit_total As Single
Dim array1 As Variant
Dim array2 As String
Private mapping_column_low As String
Private mapping_column_high As String
Private tech_total As String
Private WRcap_year As Integer
Private LPM_Vehicle_Type As String
Private Custom_Select As String
Private TPs As Long
Private min_VehType As Integer
Private max_VehType As Integer
Private safety_mass As Single
Private baseline_mass As Single
Private tech_mass As Single
Private cap_mass As Single
Private wrnet As Single
Private wrpen As Single
Private wrtech As Single
Private pw_type As Integer
Private pw_vehicle As Single
Private atkpc As Single
Private atkfc As Single
Private turbc As Single
Private pw_calc As Integer








Sub Button3_Click() 'Subroutine to handle "Calculate TEB-CEB Button"
    If Sheets("Control").Range("I18") = "True" Then
        Application.WindowState = xlMinimized
    End If
    program_timer = Timer
    Range("H1") = ""
    Range("H2") = ""
    Range("H3") = ""
    Range("H1") = "Start = " + Str(Time)
    Call initalize
    number_of_packages = Sheets("Control").Range("B4")
    Call calc_scenario
    If number_of_packages = 0 Then GoTo skip1
    Call calc_baseline
    Call calc_packages
    Call output_teb_ceb
skip1:
    Call finish
    Range("H2") = "End = " + Str(Time)
    Range("H3") = "Time (min) = " + Str((Timer - program_timer) / 60)
    
    If Sheets("Control").Range("I16") = "True" Then
        ActiveWorkbook.Save
        Application.Quit
    End If
    If Sheets("Control").Range("I16") = "False" And Sheets("Control").Range("I18") = "True" Then
        Application.WindowState = xlMaximized
    End If
    'End
End Sub

Sub Button4_Click() 'Subroutine to handle "Calculate Cost-Year Button"

    Sheets("Cost Year").Select 'clear cost year sheet
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
    
    cost_year_iterations = 0
    
    
    int1 = Sheets("Control").Range("B21")
    int2 = Sheets("Control").Range("B22")
    For cost_year_counter = int1 To int2
    
        cost_year_iterations = cost_year_iterations + 1
        
        Call initalize
        Sheets("Package Costs").Range("H15") = cost_year_counter
        Sheets("Control").Range("H21") = cost_year_counter
        Call calc_scenario
        If cost_year_iterations = 1 Then
            Sheets("Cost Year").Range("A1") = "Package" 'populate package column of cost year sheet
            str1 = "A" + Mid(Str(vehicle_counter_high - vehicle_counter_low + 2), 2)
            str1 = "A2:" + str1
            array1 = Sheets("Output").Range(str1)
            Sheets("Cost Year").Range(str1) = array1
        End If
        str1 = Chr(65 + cost_year_iterations) + "1"
        Sheets("Cost Year").Range(str1) = cost_year_counter 'populate cost column of cost year sheet
        str1 = Mid(Str(vehicle_counter_high - vehicle_counter_low + 2), 2)
        str2 = "B2:" + "B" + str1
        array1 = Sheets("Output").Range(str2)
        str2 = Chr(65 + cost_year_iterations) + "2:" + Chr(65 + cost_year_iterations) + str1
        Sheets("Cost Year").Range(str2) = array1
        
        Call finish
    Next
    End
End Sub

Sub calc_scenario()

    package_number = 0
    package_sheet = "Scenario Packages" 'calculate cost and effectiveness of packages
    vehicle_counter_low = 23
    vehicle_counter_high = 300000 'maximum rows
    sub_type = 1
    Call calc_cost_and_effectiveness
    
    'str2 = "D4:D23"
    'array1 = Sheets("Output").Range(str2)
    'str2 = "L2:L21"
    'Sheets("Cal1").Range(str2) = array1
    'Worksheets("Cal1").Calculate
    
    

End Sub

Sub calc_baseline()
    package_sheet = "Baseline" 'calculate cost and effectiveness of baseline vehicles
    vehicle_counter_low = 2
    vehicle_counter_high = 10000 'maximum rows
    sub_type = 2
    For package_number = 0 To 0
        Call calc_cost_and_effectiveness
    Next
End Sub

Sub calc_packages()
    number_of_packages = Sheets("Control").Range("B4")
    package_sheet = "Baseline" 'calculate cost and effectiveness of packages applied to baseline vehicles
    vehicle_counter_low = 2
    vehicle_counter_high = 10000 'maximum rows
    sub_type = 3
    For package_number = 1 To number_of_packages
        Call calc_cost_and_effectiveness
    Next
End Sub

 Sub calc_cost_and_effectiveness() 'main cost and effeciveness loop
    
    On Error GoTo error_handler_2

        For int5 = 1 To 25 'generate headings for "mapping" sheet
            int1 = package_number * 26 + int5  'calculate mapping column
            int2 = Int(int1 / 702) 'high letter
            int3 = Int((int1 - (702 * int2)) / 26) 'middle letter
            int4 = int1 - (702 * int2) - (int3 * 26) 'low letter
            
            If int1 > 702 Then
                str2 = Chr(64 + int2)
                str3 = Chr(65 + int3)
                str4 = Chr(64 + int4)
                str1 = str2 + str3 + str4
            ElseIf int1 > 26 Then
                str3 = Chr(64 + int3)
                str4 = Chr(64 + int4)
                str1 = str3 + str4
            Else
                str1 = Chr(64 + int4)
            End If
            
            int3 = int5
            str3 = str1
            Select Case int3
                Case 1
                    mapping_column = str3
                    Sheets("mapping").Range(str3 + "1") = "Cam" + Str(package_number)
                Case 2
                    Sheets("mapping").Range(str3 + "1") = "LUB" + Str(package_number)
                Case 3
                    Sheets("mapping").Range(str3 + "1") = "EFR" + Str(package_number)
                Case 4
                    Sheets("mapping").Range(str3 + "1") = "VVT" + Str(package_number)
                Case 5
                    Sheets("mapping").Range(str3 + "1") = "VVTL" + Str(package_number)
                Case 6
                    Sheets("mapping").Range(str3 + "1") = "DEAC" + Str(package_number)
                Case 7
                    Sheets("mapping").Range(str3 + "1") = "AFT" + Str(package_number)
                Case 8
                    Sheets("mapping").Range(str3 + "1") = "Fuel" + Str(package_number)
                Case 9
                    Sheets("mapping").Range(str3 + "1") = "FIS" + Str(package_number)
                Case 10
                    Sheets("mapping").Range(str3 + "1") = "EI" + Str(package_number)
                Case 11
                    Sheets("mapping").Range(str3 + "1") = "Boost" + Str(package_number)
                Case 12
                    Sheets("mapping").Range(str3 + "1") = "DS" + Str(package_number)
                Case 13
                    Sheets("mapping").Range(str3 + "1") = "HEG" + Str(package_number)
                Case 14
                    Sheets("mapping").Range(str3 + "1") = "ASL" + Str(package_number)
                Case 15
                    Sheets("mapping").Range(str3 + "1") = "TORQ" + Str(package_number)
                Case 16
                    Sheets("mapping").Range(str3 + "1") = "Trans" + Str(package_number)
                Case 17
                    Sheets("mapping").Range(str3 + "1") = "Hybrid" + Str(package_number)
                Case 18
                    Sheets("mapping").Range(str3 + "1") = "Plug" + Str(package_number)
                Case 19
                    Sheets("mapping").Range(str3 + "1") = "IACC" + Str(package_number)
                Case 20
                    Sheets("mapping").Range(str3 + "1") = "EPS" + Str(package_number)
                Case 21
                    Sheets("mapping").Range(str3 + "1") = "AERO" + Str(package_number)
                Case 22
                    Sheets("mapping").Range(str3 + "1") = "ROLL" + Str(package_number)
                Case 23
                    Sheets("mapping").Range(str3 + "1") = "LDB" + Str(package_number)
                Case 24
                    Sheets("mapping").Range(str3 + "1") = "AXLD" + Str(package_number)
                Case 25
                    Sheets("mapping").Range(str3 + "1") = "Mass" + Str(package_number)
            End Select
        Next
        
        int1 = package_number * 7 + 5 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "VC" + Str(package_number)
        vc_column = str3
        vcm(package_number) = str3
        int1 = package_number * 7 + 6 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "VE" + Str(package_number)
        ve_column = str3
        vem(package_number) = str3
        
        int1 = package_number * 7 + 7 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "TC" + Str(package_number)
        tc_column = str3
        tcm(package_number) = str3
        int1 = package_number * 7 + 8 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "TE" + Str(package_number)
        te_column = str3
        tem(package_number) = str3
        int1 = package_number * 7 + 9 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "CEB" + Str(package_number)
        ceb_column = str3
        int1 = package_number * 7 + 10 'calculate output column headings
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        Sheets("output").Range(str3 + "1") = "TEB" + Str(package_number)
        teb_column = str3
        Sheets("output").Range("A1") = "Package"
        Sheets("output").Range("B1") = "P Cost"
        Sheets("output").Range("C1") = "P Effect"
        Sheets("output").Range("E1") = "Vehicle"
        
    'this loop stores a pointer to package 0 for each vehicle type in the "scenario" sheet
    '    and determines the length of the "scenario" and "baseline" sheets
    int1 = 0
    For vehicle_counter = vehicle_counter_low To vehicle_counter_high 'get number of rows in sheet
        str1 = "B" + Mid(Str(vehicle_counter), 2) 'see if row is valid
        If Sheets(package_sheet).Range(str1) = "" Then
            If sub_type = 1 Then
                vehicle_type_length(Val(str4)) = vehicle_counter - int1 'set number of packages for vehicle type
            End If
            Exit For 'exit if EOF
        End If
        If sub_type = 1 Then 'get pointers to package 0 for each vehicle type in scenario file
            str4 = "C" + Mid(Str(vehicle_counter), 2) 'get vehicle type #
            str4 = Sheets(package_sheet).Range(str4)
            str2 = "E" + Mid(Str(vehicle_counter), 2) 'get omega package #
            str2 = Sheets(package_sheet).Range(str2)
            If str2 = "0" Then
                vehicle_type_pointer(Val(str4)) = vehicle_counter 'set vehicle type package 0 row pointer
                vehicle_type_length(Val(str4) - 1) = vehicle_counter - int1 'set number of packages for vehicle type
                int1 = vehicle_counter
            End If
        End If
    Next
    vehicle_counter_high = vehicle_counter - 1 'row valid
    
    For vehicle_counter = vehicle_counter_low To vehicle_counter_high 'calculate all packages
    
        total_runs_complete = total_runs_complete + 1
        If sub_type = 1 Then total_runs_complete = 0
        
        int1 = CInt(vehicle_counter / vehicle_counter_high * 100)  'Post progress
        If int1 <> progress_counter Or vehicle_counter = vehicle_counter_low Then
            Application.ScreenUpdating = True
            Sheets("Control").Range("H5") = Str(int1) + "%"
            If sub_type = 1 Then
                str1 = "Scenario"
            End If
            If sub_type = 2 Then
                str1 = "Baseline"
            End If
            If sub_type = 3 Then
                str1 = "Package" + Str(package_number)
            End If
            Sheets("Control").Range("H4") = str1
            If number_of_packages = 0 Then
                Sheets("Control").Range("H6") = Str(int1) + "%"
            Else
                float1 = (number_of_packages + 1) * (vehicle_counter_high - vehicle_counter_low + 1)
                float1 = CInt(total_runs_complete / float1 * 100)
                Sheets("Control").Range("H6") = Str(float1) + "%"
            End If
            DoEvents
            Application.ScreenUpdating = False
        End If
        progress_counter = int1
        Select Case sub_type 'see what sub called this routine
            Case 1 'Scenario
                str1 = "C" + Mid(Str(vehicle_counter), 2) 'Get vehicle type
                vehicle_type = Sheets(package_sheet).Range(str1)
            Case 2, 3 'baseline
                str1 = "I" + Mid(Str(vehicle_counter), 2) 'Get vehicle type
                vehicle_type = Sheets(package_sheet).Range(str1)
            Case Else
                Stop
        End Select
        
        

'**************************************
        
        If package_number > vehicle_type_length(vehicle_type) - 1 Then GoTo skip_vehicle

'**************************************
      
        ' Determine what page of LPM to use
        vehicle_class = "LP MODEL"  'Point to LPM sheet
        
        Select Case vehicle_type
            'Select Vehicle Type in LPM
            Case 1, 12, 14
                Sheets(vehicle_class).Range("A4") = "LPW_LRL"
                LPM_Vehicle_Type = "LPW_LRL"
            Case 2, 3, 5, 13, 16
                Sheets(vehicle_class).Range("A4") = "MPW_LRL"
                LPM_Vehicle_Type = "MPW_LRL"
            Case 10, 15, 18, 20, 23, 26, 27
                Sheets(vehicle_class).Range("A4") = "HPW"
                LPM_Vehicle_Type = "HPW"
            Case 4, 6, 7, 17
                Sheets(vehicle_class).Range("A4") = "LPW_HRL"
                LPM_Vehicle_Type = "LPW_HRL"
            Case 11, 19, 21, 24, 28
                Sheets(vehicle_class).Range("A4") = "MPW_HRL"
                LPM_Vehicle_Type = "MPW_HRL"
            Case 8, 9, 22, 25, 29
                Sheets(vehicle_class).Range("A4") = "Truck"
                LPM_Vehicle_Type = "Truck"
            Case Else
                Stop
        End Select
         
        If vehicle_class = "" Then Stop
        
        'Apply Package
        
        scenario_pointer = vehicle_type_pointer(vehicle_type) + package_number 'point to package
        
        'Calculate Cost
        

        
        'Point to proper row for cost calculation - TSh
        cost_row_pointer = 23 'TSh
        Sheets("Package costs").Range("D23") = vehicle_type 'TSh
        cost_sheet = "Package costs"
        
        temp_counter = vehicle_counter
        temp_sheet = package_sheet
        Call eng_tech_load ' Load base engine technologies
            
        If sub_type = 3 Then 'Rank Engine Technology
            mass_zero = 1 'Do not allow mass in LPM calculation
            Call calc_LPM 'Get baseline engine technology effectiveness
            base_eff = lpm_eff
            
            temp_counter = scenario_pointer
            temp_sheet = "Scenario Packages"
            Call eng_tech_load ' Load scenario engine technologies
            mass_zero = 1 'Do not allow mass in LPM calculation
            Call calc_LPM 'Get baseline engine technology effectiveness
            
            'If vehicle_counter = 663 Then Stop
            
            If base_eff > lpm_eff Then 'Keep base engine
                temp_counter = vehicle_counter
                temp_sheet = package_sheet
                Call eng_tech_load ' Reload base engine technologies
            End If
        End If 'proper engine technologies (columns K-V) are now in cost sheet
        'columns L and M (LUB and EFR)are now handled below
        
               
        str1 = "L" + Mid(Str(vehicle_counter), 2) 'LUB
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "L" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Put LUB in cost and eff if vehicle or package has LUB
            str2 = "L" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "L" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "L" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "L" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "M" + Mid(Str(vehicle_counter), 2) 'EFR
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "M" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Put EFR in cost and eff if vehicle or package has EFR
            str2 = "M" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "M" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str2 <> "" Then
                str3 = "M" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
                If InStr(str2, "EFR2") <> 0 Then 'Clear LUB if EFE2
                    str2 = "L" + Mid(Str(cost_row_pointer), 2) 'clear LUB
                    Sheets(cost_sheet).Range(str2) = ""
                End If
            ElseIf str1 <> "" Then
                str3 = "M" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
                If InStr(str1, "EFR2") <> 0 Then 'Clear LUB if EFE2
                    str2 = "L" + Mid(Str(cost_row_pointer), 2) 'clear LUB
                    Sheets(cost_sheet).Range(str2) = ""
                End If
            End If
        End If
        
        str1 = "Z" + Mid(Str(vehicle_counter), 2) 'Transmission
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "Z" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Rank Transmission
            str2 = "Z" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "Z" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            rank_input = str1 'calculate baseline rank
            Call rank_transmission
            baseline_rank = rank_output
            rank_input = str2 'calculate package rank
            Call rank_transmission
            package_rank = rank_output
            If baseline_rank > package_rank Then 'use baseline transmission
                str3 = "Z" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            Else 'use package
                str3 = "Z" + Mid(Str(cost_row_pointer), 2) 'package transmission
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        str3 = "Z" + Mid(Str(cost_row_pointer), 2) 'get chosen transmission
        str2 = Sheets(cost_sheet).Range(str3)
        If InStr(str2, "MT") <> 0 Then 'flag manual transmission if chosen for package
            'mtflag = 1 'this is no longer needed for TRXX transmissions
        Else
            mtflag = 0
        End If
        
        str1 = "W" + Mid(Str(vehicle_counter), 2) 'HEG
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "W" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Put HEG in cost and eff if vehicle or package has HEG
            str2 = "W" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "W" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "W" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "W" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If

        End If
        
        str1 = "X" + Mid(Str(vehicle_counter), 2) 'ASL
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "X" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Put ASL in cost and eff if vehicle or package has ASL
            str2 = "X" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "X" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "X" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "X" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
            If mtflag = 1 Then 'do not allow ASL if manual transmission
                str3 = "X" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = ""
            End If
        End If
        
        str1 = "Y" + Mid(Str(vehicle_counter), 2) 'TORQ
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "Y" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Put TORQ in cost and eff if vehicle or package has TORQ
            str2 = "Y" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "Y" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "Y" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "Y" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
            If mtflag = 1 Then 'do not allow TORQ if manual transmission
                str3 = "Y" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = ""
            End If
        End If
        
        
        str1 = "AB" + Mid(Str(vehicle_counter), 2) 'Plug-in
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AB" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        base_plugin = ""
        package_plugin = ""
        If sub_type = 3 Then
            str2 = "AB" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AB" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AB" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
                base_plugin = "plug"
            End If
            If str2 <> "" Then
                str3 = "AB" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
                package_plugin = "plug"
            End If
        End If
        
        'If vehicle_counter = 24 Then Stop
        
        str1 = "AA" + Mid(Str(vehicle_counter), 2) 'Stop-Start/Hybrid/Full EV
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AA" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then 'Rank Hybrid
            str2 = "AA" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AA" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            rank_input = str1 + base_plugin 'calculate baseline rank
            Call rank_hybrid
            baseline_rank = rank_output
            
            If baseline_rank = 8 Then baseline_rank = 9 'Do not improve baseline EV's
            
            rank_input = str2 + package_plugin 'calculate package rank
            Call rank_hybrid
            package_rank = rank_output
            replace_package = 0
            
            If package_rank >= baseline_rank Then
                Select Case package_rank
                    Case 1 '12v SS
                        str3 = "AA" + Mid(Str(cost_row_pointer), 2)
                        Sheets(cost_sheet).Range(str3) = str2
                    Case 2 'HV SS
                        str3 = "AA" + Mid(Str(cost_row_pointer), 2)
                        Sheets(cost_sheet).Range(str3) = str2
                    Case 3, 4, 5, 6, 7, 8 'Hybrid
                        replace_package = 2 'replace entire vehicle with package
                End Select
            ElseIf baseline_rank > package_rank Then
                Select Case baseline_rank
                    Case 1 '12v SS
                        str3 = "AA" + Mid(Str(cost_row_pointer), 2)
                        Sheets(cost_sheet).Range(str3) = str1
                    Case 2 'HV SS
                        str3 = "AA" + Mid(Str(cost_row_pointer), 2)
                        Sheets(cost_sheet).Range(str3) = str1
                    Case 3, 4, 5, 6, 7, 8, 9 'Hybrid
                        replace_package = 1 'replace entire vehicle with baseline
                End Select
            End If
        End If
              
        str1 = "AC" + Mid(Str(vehicle_counter), 2) 'Alternator & electrification
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AC" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then
            str2 = "AC" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AC" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AC" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "AC" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "AD" + Mid(Str(vehicle_counter), 2) 'EPS
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AD" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then
            str2 = "AD" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AD" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AD" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "AD" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "AE" + Mid(Str(vehicle_counter), 2) 'Aero
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AE" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then
            str2 = "AE" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AE" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AE" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "AE" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "AF" + Mid(Str(vehicle_counter), 2) 'Roll
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AF" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then
            str2 = "AF" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AF" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AF" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "AF" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "AG" + Mid(Str(vehicle_counter), 2) 'LDB
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AG" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If sub_type = 3 Then
            str2 = "AG" + Mid(Str(cost_row_pointer), 2) 'clear value
            Sheets(cost_sheet).Range(str2) = ""
            str2 = "AG" + Mid(Str(scenario_pointer), 2) 'get scenario
            str2 = Sheets("Scenario Packages").Range(str2)
            If str1 <> "" Then
                str3 = "AG" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str1
            End If
            If str2 <> "" Then
                str3 = "AG" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str3) = str2
            End If
        End If
        
        str1 = "AH" + Mid(Str(vehicle_counter), 2) 'Axle
        str1 = Sheets(package_sheet).Range(str1)
        str2 = "AH" + Mid(Str(cost_row_pointer), 2)
        Sheets(cost_sheet).Range(str2) = str1
        If InStr(str1, "NA") = 0 Then 'if "NA" in baseline do not apply scenario technology
            If sub_type = 3 Then
                str2 = "AH" + Mid(Str(cost_row_pointer), 2) 'clear value
                Sheets(cost_sheet).Range(str2) = ""
                str2 = "AH" + Mid(Str(scenario_pointer), 2) 'get scenario
                str2 = Sheets("Scenario Packages").Range(str2)
                If str1 <> "" Then
                    str3 = "AH" + Mid(Str(cost_row_pointer), 2)
                    Sheets(cost_sheet).Range(str3) = str1
                End If
                If str2 <> "" Then
                    str3 = "AH" + Mid(Str(cost_row_pointer), 2)
                    Sheets(cost_sheet).Range(str3) = str2
                End If
            End If
        End If
        
        ignore_penalty = 0
        sub_type_temp = sub_type
        str1 = Sheets("Control").Range("I10")
        If Sheets("Control").Range("I10") = "True" Then 'Ignore Weight Penalty and use scenario if selected
            If sub_type = 3 Then
                sub_type_temp = 4
                ignore_penalty = 1
            End If
        End If
        Select Case sub_type_temp
            Case 1 'scenario
                str2 = "AA" + Mid(Str(vehicle_counter), 2) 'get scenario Hybrid/EV
                str2 = Sheets(package_sheet).Range(str2)
                str3 = "AB" + Mid(Str(vehicle_counter), 2) 'get scenario Plug-in
                str3 = Sheets(package_sheet).Range(str3)
                str2 = ":" + str2 + ":" + str3 'combine for testing
                str3 = "Q" + Mid(Str(vehicle_counter), 2) 'get scenario Diesel
                str3 = Sheets(package_sheet).Range(str3)
                str2 = str2 + ":" + str3 + ":" 'combine for testing
                
                If InStr(str2, "P2") <> 0 Then 'get weight penalty from scenario package
                    mass_tech = "P2-"
                ElseIf InStr(str2, "MHEV48V") <> 0 Then
                    mass_tech = "MHEV48V-"
                ElseIf InStr(str2, "MHEV") <> 0 Then
                    mass_tech = "MHEV-"
                ElseIf InStr(str2, "REEV20") <> 0 Then
                    mass_tech = "REEV20-"
                ElseIf InStr(str2, "REEV40") <> 0 Then
                    mass_tech = "REEV40-"
                ElseIf InStr(str2, "EV75") <> 0 Then
                    mass_tech = "EV75-"
                ElseIf InStr(str2, "EV100") <> 0 Then
                    mass_tech = "EV100-"
                ElseIf InStr(str2, "EV150") <> 0 Then
                    mass_tech = "EV150-"
                ElseIf InStr(str2, "EV200") <> 0 Then
                    mass_tech = "EV200-"
                Else
                    mass_tech = "CONV"  'conventional
                End If
            
                int1 = InStr(str2, mass_tech)
                If int1 <> 0 Then
                    int1 = InStr(int1, str2, mass_tech)
                    int3 = Len(mass_tech)
                    int2 = InStr(int1 + int3, str2, ":")
                    str1 = Mid(str2, int1 + int3, int2 - int1 - int3)
                    mass_effectiveness = Val(str1) 'save for LPM later
                    str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from scenario
                    str1 = Sheets(package_sheet).Range(str1)
                    str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    Sheets(cost_sheet).Range(str2) = str1
                Else
                    str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from scenario
                    str1 = Sheets(package_sheet).Range(str1)
                    str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    Sheets(cost_sheet).Range(str2) = str1
                    mass_effectiveness = Val(str1) 'save for LPM later
                End If
                
            Case 8 'bypass for now
                str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from baseline
                str1 = Sheets(package_sheet).Range(str1) 'get "Weight Factors %" string
                str2 = "BASE:"
                int1 = InStr(str1, str2)
                If int1 = 0 Then Stop 'stop if not found
                int2 = InStr(int1 + Len(str2), str1, ",")
                str1 = Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2))) 'base% in str1
                If Val(str1) = 0 Then str1 = ""
                
                baseline_mass = Val(str1)
                
                str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from baseline
                str1 = Sheets(package_sheet).Range(str1) 'get "Weight Factors %" string
                str2 = "CAP:"
                int1 = InStr(str1, str2)
                If int1 = 0 Then Stop 'stop if not found
                int2 = InStr(int1 + Len(str2), str1, ",")
                float2 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'cap% in float2
                If Sheets("Control").Range("I12") = "True" Then 'Bypass mass caps if option selected
                    float2 = 100
                End If
                
                cap_mass = float2
                
                'str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                'Sheets(cost_sheet).Range(str2) = str1
                
                'mass_effectiveness = Int(Val(str1) * 10) / 10 'save for LPM later
                
            Case 2, 3 'packages
            
            'If vehicle_counter = 24 Then Stop
                        
                str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from baseline
                str1 = Sheets(package_sheet).Range(str1) 'get "Weight Factors %" string
                
                str2 = "BASE:"
                int1 = InStr(str1, str2)
                If int1 = 0 Then Stop 'stop if not found
                int2 = InStr(int1 + Len(str2), str1, ",")
                float1 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'base% in float1
                
                baseline_mass = float1
                
                str2 = "CAP:"
                int1 = InStr(str1, str2)
                If int1 = 0 Then Stop 'stop if not found
                int2 = InStr(int1 + Len(str2), str1, ",")
                float2 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'cap% in float2
                If Sheets("Control").Range("I12") = "True" Then 'Bypass mass caps if option selected
                    float2 = 100
                End If
                
                cap_mass = float2
                
                str2 = "AI" + Mid(Str(scenario_pointer), 2) 'tech mass
                str2 = Sheets("Scenario packages").Range(str2)
                tech_mass = Val(str2)
                
           
                
                
            Case 4 'Ignore mass caps
                str1 = "AI" + Mid(Str(scenario_pointer), 2) 'mass from scenario
                str1 = Sheets("Scenario Packages").Range(str1)
                str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                Sheets(cost_sheet).Range(str2) = str1
                mass_effectiveness = Val(str1) 'save for LPM later
                
        End Select

        If sub_type = 3 Then
        
        'If vehicle_counter = 2 Then Stop
        
        If Sheets("Control").Range("D18") = True Then   'added in V4.18
            str2 = "AA" + Mid(Str(vehicle_counter), 2)
            str1 = Sheets("Baseline").Range(str2)
            If (InStr(str1, "EV") <> 0) And (InStr(str1, "HEV") = 0) Then replace_package = 3
            str2 = "AB" + Mid(Str(vehicle_counter), 2)
            str1 = Sheets("Baseline").Range(str2)
            If InStr(str1, "REEV") <> 0 Then replace_package = 3
        End If
        
            Select Case replace_package
                Case 1 'replace with baseline package
                    temp_counter = vehicle_counter
                    temp_sheet = package_sheet
                    new_package = 0
                    Call replace_package1
                    
                    str1 = "AI" + Mid(Str(vehicle_counter), 2) 'Replace with mass from baseline
                    str1 = Sheets("Baseline").Range(str1) 'get "Weight Factors %" string
                    str2 = "BASE:"
                    int1 = InStr(str1, str2)
                    If int1 = 0 Then Stop 'stop if not found
                    int2 = InStr(int1 + Len(str2), str1, ",")
                    str1 = Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2))) 'base% in str1
                    If Val(str1) = 0 Then str1 = ""
                    str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    tech_mass = Val(str1)
                    'Sheets(cost_sheet).Range(str2) = str1
                                        
                Case 2 'replace with new package
                    temp_counter = scenario_pointer
                    temp_sheet = "Scenario Packages"
                    new_package = 0
                    Call replace_package1
                    
                    str1 = "AI" + Mid(Str(scenario_pointer), 2) 'mass from scenario
                    str1 = Sheets("Scenario Packages").Range(str1)
                    str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    Sheets(cost_sheet).Range(str2) = str1
                    tech_mass = Val(str1)
                    
                    'str1 = "AI" + Mid(Str(vehicle_counter), 2) 'Replace with mass from baseline
                    'str1 = Sheets("Baseline").Range(str1) 'get "Weight Factors %" string
                    'str2 = "BASE:"
                    'int1 = InStr(str1, str2)
                    'If int1 = 0 Then Stop 'stop if not found
                    'int2 = InStr(int1 + Len(str2), str1, ",")
                    'str1 = Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2))) 'base% in str1
                    'If Val(str1) = 0 Then str1 = ""
                    'str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    'Sheets(cost_sheet).Range(str2) = str1
                    'tech_mass = Val(str2)
                    
                Case 3 'replace with baseline package including mass
                    temp_counter = vehicle_counter
                    temp_sheet = package_sheet
                    new_package = 0
                    Call replace_package1
                    
                    str1 = "AI" + Mid(Str(vehicle_counter), 2) 'Replace with mass from baseline
                    str1 = Sheets("Baseline").Range(str1) 'get "Weight Factors %" string
                    str2 = "BASE:"
                    int1 = InStr(str1, str2)
                    If int1 = 0 Then Stop 'stop if not found
                    int2 = InStr(int1 + Len(str2), str1, ",")
                    str1 = Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2))) 'base% in str1
                    If Val(str1) = 0 Then str1 = ""
                    str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                    Sheets(cost_sheet).Range(str2) = str1
                    tech_mass = Val(str1)
                    
            End Select
        End If
                'If vehicle_counter = 24 Then Stop
                
                str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from baseline
                str1 = Sheets(package_sheet).Range(str1) 'get "Weight Factors %" string
                
                If sub_type = 2 Or sub_type = 3 Then
                    str2 = "AA" + Mid(Str(cost_row_pointer), 2) 'get scenario Hybrid/EV
                    str2 = Sheets("Package costs").Range(str2)
                    str3 = "AB" + Mid(Str(cost_row_pointer), 2) 'get scenario Plug-in
                    str3 = Sheets("Package costs").Range(str3)
                    str2 = str2 + str3 'combine for testing
                    str3 = "Q" + Mid(Str(cost_row_pointer), 2) 'get scenario Diesel
                    str3 = Sheets("Package costs").Range(str3)
                    str2 = str2 + str3 'combine for testing
                
                
                
                If InStr(str2, "P2") <> 0 Then 'get weight penalty from "Weight Factors"
                    str2 = "P2HEV:"
                    mass_tech = "P2"
                ElseIf InStr(str2, "MHEV48V") <> 0 Then
                    str2 = "MHEV48V:"
                    mass_tech = "MHEV48V"
                ElseIf InStr(str2, "MHEV") <> 0 Then
                    str2 = "MHEV:"
                    mass_tech = "MHEV"
                ElseIf InStr(str2, "REEV20") <> 0 Then
                    str2 = "PHEV20:"
                    mass_tech = "REEV20"
                ElseIf InStr(str2, "REEV40") <> 0 Then
                    str2 = "PHEV40:"
                    mass_tech = "REEV40"
                ElseIf InStr(str2, "EV75") <> 0 Then
                    str2 = "EV75:"
                    mass_tech = "EV75"
                ElseIf InStr(str2, "EV100") <> 0 Then
                    str2 = "EV100:"
                    mass_tech = "EV100"
                ElseIf InStr(str2, "EV150") <> 0 Then
                    str2 = "EV150:"
                    mass_tech = "EV150"
                ElseIf InStr(str2, "EV200") <> 0 Then
                    str2 = "EV200:"
                    mass_tech = "EV200"
                ElseIf InStr(str2, "DSL") <> 0 Then
                    str2 = "DSL:"
                    mass_tech = "DSL"
                Else
                    str2 = ""
                    'conventional
                End If
                
                If str2 <> "" Then
                    int1 = InStr(str1, str2)
                    If int1 = 0 Then Stop 'stop if not found
                    int2 = InStr(int1 + Len(str2), str1, ",")
                    float3 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'penalty% in float3
                    If Int(float3) = float3 Then 'Round if necessary
                        float3 = float3
                    Else
                        float3 = Round(float3 + 0.5, 0) - 0.5
                    End If
                    If float3 < 0 Then float3 = 0
                Else
                    float3 = 0 'no penalty for conventional
                End If
                
                safety_mass = float3
                
                wrpen = safety_mass
                
                'If vehicle_counter = 24 And package_number = 27 Then Stop
                
                If tech_mass <= cap_mass Then
                    If tech_mass <= baseline_mass Then
                        wrtech = baseline_mass
                        wrnet = wrtech - wrpen
                    Else
                        wrtech = tech_mass
                        wrnet = wrtech - wrpen
                    End If
                Else
                    If tech_mass <= (cap_mass + wrpen) Then
                        wrtech = tech_mass
                        wrnet = wrtech - wrpen
                    Else
                        wrnet = cap_mass
                        wrtech = wrnet + wrpen
                    End If
                End If
                
                'If vehicle_counter = 24 Then Stop
                
                str2 = "AA" + Mid(Str(cost_row_pointer), 2) 'see if tech with weight factor
                str3 = Sheets("Package costs").Range(str2)
                int1 = InStr(str3, ".")
                If int1 <> 0 Then
                    int1 = InStr(str3, "-")
                    str4 = Left(str3, int1 - 1) + "-"
                    str1 = Format(wrnet, "####.0")
                    str4 = str4 + str1
                    Sheets("Package costs").Range(str2) = str4
                End If
                
                str2 = "AB" + Mid(Str(cost_row_pointer), 2) 'see if tech with weight factor
                str3 = Sheets("Package costs").Range(str2)
                int1 = InStr(str3, ".")
                If int1 <> 0 Then
                    int1 = InStr(str3, "-")
                    str4 = Left(str3, int1 - 1) + "-"
                    str1 = Format(wrnet, "####.0")
                    str4 = str4 + str1
                    Sheets("Package costs").Range(str2) = str4
                End If
                    
                
                
                
                
                'str2 = "AI" + Mid(Str(scenario_pointer), 2) 'get scenario mass
                'float4 = Val(Sheets("Scenario Packages").Range(str2)) 'scenario mass in float4
                
                'If float1 > float4 Then 'if baseline mass > package mass
                '    float4 = float1 'change package mass to baseline mass
                'End If
                
                'float5 = float2 + float3 'float5=Mass Cost
                'float6 = float2 'float6=Mass Effectiveness
                
                'If float5 > float4 Then 'see if scenario mass needs to reduce values
                '    float6 = float6 - float5 + float4 'reduce mass effectiveness as needed
                '    If float6 < 0 Then Stop 'stop if scenario mass too low to allow penalty
                '    float5 = float4 'reduce mass cost to scenario
                'End If
                
                'mass_cost = Int(float5 * 10) / 10 'save for later
                'mass_effectiveness = Int(float6 * 10) / 10 'save for LPM later
                'technology_cost = Int((float5 - float3) * 10) / 10
                    
                mass_cost = wrtech
                mass_effectiveness = Int(wrnet * 10) / 10
                '*****************************************************************
                
                str2 = "AI" + Mid(Str(cost_row_pointer), 2) 'put in cost sheet
                str3 = Str(mass_cost)
                str3 = Mid(str3, 2)
                If Val(str3) = 0 Then str3 = "" 'cost sheet does not allow 0 in mass reduction tech
                If Left(str3, 1) = "." Then str3 = "0" + str3
                Sheets(cost_sheet).Range(str2) = str3
                
                'str2 = "AA" + Mid(Str(scenario_pointer), 2) 'get scenario Hybrid/EV
                'str2 = Sheets("Scenario Packages").Range(str2)
                'If str2 <> "" And str2 <> "Stop-Start" Then 'if Hybrid or EV add mass cost to technology
                '    If Int(technology_cost) = technology_cost Then
                '        str1 = Str(technology_cost) + ".0"
                '    Else
                '        str1 = Str(technology_cost)
                '    End If
                '    str1 = Mid(str1, 2)
                '    If Left(str1, 1) = "." Then str1 = "0" + str1
                '    str1 = mass_tech + "-" + str1
                '    str2 = "AA" + Mid(Str(cost_row_pointer), 2)
                '    Sheets(cost_sheet).Range(str2) = str1
                'End If
                
                'str2 = "AB" + Mid(Str(scenario_pointer), 2) 'get scenario Plug-in
                'str2 = Sheets("Scenario Packages").Range(str2)
                'If str2 <> "" Then 'if Plug-in add mass cost to technology
                '    If Int(technology_cost) = technology_cost Then
                '        str1 = Str(technology_cost) + ".0"
                '    Else
                '        str1 = Str(technology_cost)
                '    End If
                '    str1 = Mid(str1, 2)
                '    If Left(str1, 1) = "." Then str1 = "0" + str1
                '    str1 = mass_tech + "-" + str1
                '    str2 = "AB" + Mid(Str(cost_row_pointer), 2)
                '    Sheets(cost_sheet).Range(str2) = str1
                'End If
                
                End If
        
        
        
        pw_calc = 1 ' calc pw for vehicle
        Call calc_LPM
        
        Worksheets("Package costs").Calculate
        
        'If package_number = 21 And vehicle_type = 5 Then Stop
        
        'If Not Application.CalculationState = xlDone Then
            'DoEvents
        'End If

        ' Get Result
        Select Case sub_type 'Generate out based on calling routine
            Case 1 'scenerio
                str2 = "B" + Mid(Str(vehicle_counter), 2) 'Tech Pack
                str2 = Sheets("Scenario Packages").Range(str2)
                str1 = "A" + Mid(Str(vehicle_counter - 21), 2) 'Populate
                Sheets("output").Range(str1) = str2
                
                'str2 = Sheets(vehicle_class).Range("A29") 'Effectiveness
                'str2 = str2 * Sheets("Control").Range("B8")
                str1 = "C" + Mid(Str(vehicle_counter - 21), 2) 'Populate
                'Sheets("output").Range(str1) = str2
                Sheets("output").Range(str1) = lpm_eff
                
                'str2 = Sheets(vehicle_class).Range("M29") 'CO2
                'str1 = "D" + Mid(Str(vehicle_counter - 21), 2) 'Populate
                'Sheets("output").Range(str1) = str2
        
                str2 = "BJ" + Mid(Str(cost_row_pointer), 2)
                str2 = Sheets(cost_sheet).Range(str2) 'Cost
                str1 = "B" + Mid(Str(vehicle_counter - 21), 2) 'Populate
                Sheets("output").Range(str1) = str2
              
            Case 2, 3 'baseline and package
                str2 = vehicle_counter 'vehicle number
                str1 = "E" + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = str2
                
                'str2 = Sheets(vehicle_class).Range("A29") 'Vehicle Effectiveness
                'str2 = str2 * Sheets("Control").Range("B8")
                str1 = ve_column + Mid(Str(vehicle_counter), 2) 'Populate
                'Sheets("output").Range(str1) = str2
                Sheets("output").Range(str1) = lpm_eff
                
                str2 = "BJ" + Mid(Str(cost_row_pointer), 2)
                str2 = Sheets(cost_sheet).Range(str2) 'Vehicle Cost
                str1 = vc_column + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = str2
                
                temp_pointer = vehicle_type 'get vehicle type
                'calc row for package cost
                temp_pointer = vehicle_type_pointer(vehicle_type) - 21 + package_number
                str2 = "B" + Mid(Str(temp_pointer), 2)
                str2 = Sheets("output").Range(str2) 'Total Cost
                str1 = tc_column + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = str2
                
                temp_pointer = vehicle_type 'get vehicle type
                'calc row for package cost
                temp_pointer = vehicle_type_pointer(vehicle_type) - 21 + package_number
                str2 = "C" + Mid(Str(temp_pointer), 2)
                str2 = Sheets("output").Range(str2) 'Total Effectiveness
                str1 = te_column + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = str2
                
                reference_package = 0 'For new ACE method - reference package is always baseline
                
                'If vehicle_counter > 265 Then Stop
                
                'calculate teb
                str1 = "AJ" + Mid(Str(vehicle_counter), 2)
                float1 = Sheets("Baseline").Range(str1) 'get baseline co2
                str1 = ve_column + Mid(Str(vehicle_counter), 2)
                float3 = Sheets("output").Range(str1) 'get current VE
                If float3 = 1 Then float3 = 0.999999
                str1 = vem(reference_package) + Mid(Str(vehicle_counter), 2)
                float4 = Sheets("output").Range(str1) 'get previous VE
                If float4 = 1 Then float4 = 0.999999
                teb = float1 / (1 - float4) * (1 - float3)
                str1 = teb_column + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = teb
                
                str1 = vc_column + Mid(Str(vehicle_counter), 2)
                float3 = Sheets("output").Range(str1) 'get current VC
                str1 = vcm(reference_package) + Mid(Str(vehicle_counter), 2)
                float4 = Sheets("output").Range(str1) 'get previous VC

                ceb = float3 - float4
                str1 = ceb_column + Mid(Str(vehicle_counter), 2) 'Populate
                Sheets("output").Range(str1) = ceb

                tech_total = "|"
                str1 = Mid(Str(cost_row_pointer), 2)
                str2 = Sheets("Package costs").Range("K" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("L" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                
                str2 = Sheets("Package costs").Range("M" + str1) 'Break EFR column into 2
                If str2 <> "" Then
                    int1 = InStr(str2, "-")
                    str3 = Left(str2, int1 - 1)
                    str4 = Mid(str2, int1 + 1)
                    tech_total = tech_total + str3 + "|" + str4 + "|"
                End If
                    
                str2 = Sheets("Package costs").Range("N" + str1) 'Only first 3 characters in VVT column
                If str2 <> "" Then
                    str3 = Left(str2, 3)
                    tech_total = tech_total + str3 + "|"
                End If
                
                str2 = Sheets("Package costs").Range("O" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("P" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("Q" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                
                str2 = Sheets("Package costs").Range("R" + str1)
                If str2 <> "" Then
                    int2 = Len(str2)
                    int1 = InStr(str2, "-")
                    If int1 = 0 Then Stop
                    str3 = Left(str2, int1 - 1)
                    tech_total = tech_total + str3 + "|"
                End If

                str2 = Sheets("Package costs").Range("S" + str1) 'Only first 2 characters in DI column
                If str2 <> "" Then
                    str3 = Left(str2, 2)
                    tech_total = tech_total + str3 + "|"
                End If
                
                str2 = Sheets("Package costs").Range("T" + str1) 'Only first 4 characters in ATK column
                If str2 <> "" Then
                    str3 = Left(str2, 4)
                    tech_total = tech_total + str3 + "|"
                End If
                
                str2 = Sheets("Package costs").Range("U" + str1) 'Only first 6 characters in Boost column
                If str2 <> "" Then
                
                    int2 = Len(str2)
                    int1 = InStr(str2, "-")
                    If int1 = 0 Then Stop
                    str3 = Left(str2, int1 - 1)
                
                
                    'str3 = Left(str2, 6)
                    tech_total = tech_total + str3 + "|"
                End If
                
                str2 = Sheets("Package costs").Range("W" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("X" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("Y" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("Z" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                
                float6 = 0 'Flag package as non-electrified
                
                str2 = Sheets("Package costs").Range("AA" + str1) 'Format hybrid column
                float1 = 0 'Reset WRnet
                If str2 <> "" Then
                    float6 = 1 'Flag as electrified
                    int2 = Len(str2)
                    int1 = InStr(str2, "-")
                    If int1 = 0 Then Stop
                    str4 = Left(str2, int1 - 1)
                    int2 = int2 - Len(str4) - 1
                    If str4 = "Stop" Then str4 = "Stop-Start"
                    tech_total = tech_total + str4 + "|" 'Output hybrid column without mass tech
                    str4 = Right(str2, int2)
                    If str4 = "Start" Then
                        str4 = "0"
                        float6 = 0 'Flag as non-electrified
                    End If
                    float1 = Val(str4) 'Remember WRnet = Float1
                End If
                
                str2 = Sheets("Package costs").Range("AB" + str1)
                If str2 <> "" Then
                    float6 = 1 'Flag as electrified
                    int2 = Len(str2)
                    int1 = InStr(str2, "-")
                    If int1 = 0 Then Stop
                    str4 = Left(str2, int1 - 1)
                    int2 = int2 - Len(str4) - 1
                    tech_total = tech_total + str4 + "|" 'Output plug-in column without mass tech
                    str4 = Right(str2, int2)
                    float1 = Val(str4) 'Remember WRnet = Float1
                End If
                
                str2 = Sheets("Package costs").Range("AC" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("AD" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("AE" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("AF" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("AG" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                str2 = Sheets("Package costs").Range("AH" + str1)
                If str2 <> "" Then tech_total = tech_total + str2 + "|"
                
                str2 = Sheets("Package costs").Range("AI" + str1)
                If str2 = "" Then
                    str2 = "0"
                    float2 = 0 'No WRtech
                Else
                    float2 = Val(str2)
                End If
                
                
                'If vehicle_counter = 24 Then Stop
                
                'str1 = "AI" + Mid(Str(vehicle_counter), 2) 'mass from baseline
                'str1 = Sheets(package_sheet).Range(str1) 'get "Weight Factors %" string
                
                'str2 = "BASE:"
                'int1 = InStr(str1, str2)
                'If int1 = 0 Then Stop 'stop if not found
                'int2 = InStr(int1 + Len(str2), str1, ",")
                'baseline_mass = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2))))
                
                'str2 = "CAP:"
                'int1 = InStr(str1, str2)
                'If int1 = 0 Then Stop 'stop if not found
                'int2 = InStr(int1 + Len(str2), str1, ",")
                'float2 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'cap% in float2
                'If Sheets("Control").Range("I12") = "True" Then 'Bypass mass caps if option selected
                '    float2 = 100
                'End If
                
                'cap_mass = float2
                
                
                'str2 = "AI" + Mid(Str(scenario_pointer), 2) 'tech mass
                'str2 = Sheets("Scenario packages").Range(str2)
                'tech_mass = Val(str2)
                
                'str2 = "AA" + Mid(Str(cost_row_pointer), 2) 'get scenario Hybrid/EV
                'str2 = Sheets("Package costs").Range(str2)
                'str3 = "AB" + Mid(Str(cost_row_pointer), 2) 'get scenario Plug-in
                'str3 = Sheets("Package costs").Range(str3)
                'str2 = str2 + str3 'combine for testing
                'str3 = "Q" + Mid(Str(cost_row_pointer), 2) 'get scenario Diesel
                'str3 = Sheets("Package costs").Range(str3)
                'str2 = str2 + str3 'combine for testing
                
                
                'If InStr(str2, "P2") <> 0 Then 'get weight penalty from "Weight Factors"
                '    str2 = "P2HEV:"
                '    mass_tech = "P2"
                'ElseIf InStr(str2, "MHEV48V") <> 0 Then
                '    str2 = "MHEV48V:"
                '    mass_tech = "MHEV48V"
                'ElseIf InStr(str2, "MHEV") <> 0 Then
                '    str2 = "MHEV:"
                '    mass_tech = "MHEV"
                'ElseIf InStr(str2, "REEV20") <> 0 Then
                '    str2 = "PHEV20:"
                '    mass_tech = "REEV20"
                'ElseIf InStr(str2, "REEV40") <> 0 Then
                '    str2 = "PHEV40:"
                '    mass_tech = "REEV40"
                'ElseIf InStr(str2, "EV75") <> 0 Then
                '    str2 = "EV75:"
                '    mass_tech = "EV75"
                'ElseIf InStr(str2, "EV100") <> 0 Then
                '    str2 = "EV100:"
                '    mass_tech = "EV100"
                'ElseIf InStr(str2, "EV150") <> 0 Then
                '    str2 = "EV150:"
                '    mass_tech = "EV150"
                'ElseIf InStr(str2, "EV200") <> 0 Then
                '    str2 = "EV200:"
                '    mass_tech = "EV200"
                'ElseIf InStr(str2, "DSL") <> 0 Then
                '    str2 = "DSL:"
                '    mass_tech = "DSL"
                'Else
                '    str2 = ""
                '    'conventional
                'End If
                
                'If str2 <> "" Then
                '    int1 = InStr(str1, str2)
                '    If int1 = 0 Then Stop 'stop if not found
                '    int2 = InStr(int1 + Len(str2), str1, ",")
                '    float3 = Val(Mid(str1, int1 + Len(str2), int2 - (int1 + Len(str2)))) 'penalty% in float3
                '    If Int(float3) = float3 Then 'Round if necessary
                '        float3 = float3
                '    Else
                '        float3 = Round(float3 + 0.5, 0) - 0.5
                '    End If
                '    If float3 < 0 Then float3 = 0
                'Else
                '    float3 = 0 'no penalty for conventional
                'End If
                
                'safety_mass = float3
                
                
                
                
                
                'wrpen = safety_mass
                
                'If vehicle_counter = 24 Then Stop
                
                'If tech_mass <= cap_mass Then
                '    If tech_mass <= baseline_mass Then
                '        wrtech = baseline_mass
                '        wrnet = wrtech - wrpen
                '    Else
                '        wrtech = tech_mass
                '        wrnet = wrtech - wrpen
                '    End If
                'Else
                '    If tech_mass <= (cap_mass + wrpen) Then
                '        wrtech = tech_mass
                '        wrnet = wrtech - wrpen
                '    Else
                '        wrnet = cap_mass
                '        wrtech = wrnet + wrpen
                '    End If
                'End If
                
                
                
                tech_total = tech_total + "WRtech-" + Str(wrtech) + "|" 'Output WRtech
                tech_total = tech_total + "WRpen-" + Str(wrpen) + "|" 'Output WRpen
                tech_total = tech_total + "WRnet-" + Str(wrnet) + "|" 'Output WRnet
                
                'If float6 = 0 Then 'No electrification
                '    tech_total = tech_total + "WRpen-" + Str(float1) + "|" 'Output WRpen
                '    tech_total = tech_total + "WRnet-" + Str(float2) + "|" 'Output WRnet
                'Else
                '    tech_total = tech_total + "WRpen-" + Str(float2 - float1) + "|" 'Output WRpen
                '    tech_total = tech_total + "WRnet-" + Str(float1) + "|" 'Output WRnet
                'End If
                
                
                
                str1 = Mid(Str(cost_row_pointer), 2)
                str2 = "K" + str1 + ":" + "AI" + str1
                
                array1 = Sheets("Package costs").Range(str2)
                
            int1 = package_number * 26 + 1  'calculate mapping column start
            int2 = Int(int1 / 702) 'high letter
            int3 = Int((int1 - (702 * int2)) / 26) 'middle letter
            int4 = int1 - (702 * int2) - (int3 * 26) 'low letter
            
            If int1 > 702 Then
                str2 = Chr(64 + int2)
                str3 = Chr(65 + int3)
                str4 = Chr(64 + int4)
                str1 = str2 + str3 + str4
            ElseIf int1 > 26 Then
                str3 = Chr(64 + int3)
                str4 = Chr(64 + int4)
                str1 = str3 + str4
            Else
                str1 = Chr(64 + int4)
            End If
            mapping_column_low = str1
            
            int1 = package_number * 26 + 25  'calculate mapping column end
            int2 = Int(int1 / 702) 'high letter
            int3 = Int((int1 - (702 * int2)) / 26) 'middle letter
            int4 = int1 - (702 * int2) - (int3 * 26) 'low letter
            
            If int1 > 702 Then
                str2 = Chr(64 + int2)
                str3 = Chr(65 + int3)
                str4 = Chr(64 + int4)
                str1 = str2 + str3 + str4
            ElseIf int1 > 26 Then
                str3 = Chr(64 + int3)
                str4 = Chr(64 + int4)
                str1 = str3 + str4
            Else
                str1 = Chr(64 + int4)
            End If
            mapping_column_high = str1
            
            str1 = Mid(Str(vehicle_counter), 2)
            str2 = mapping_column_low + str1
            str1 = mapping_column_high + str1
            str2 = str2 + ":" + str1
            
                Sheets("Mapping").Range(str2) = array1
                

                
                                                        'Start credit calculation
                str1 = Mid(Str(cost_row_pointer), 2) 'Get technologies in one string
                str2 = Sheets("Package costs").Range("K" + str1)
                str2 = str2 + Sheets("Package costs").Range("K" + str1)
                str2 = str2 + Sheets("Package costs").Range("L" + str1)
                str2 = str2 + Sheets("Package costs").Range("M" + str1)
                str2 = str2 + Sheets("Package costs").Range("N" + str1)
                str2 = str2 + Sheets("Package costs").Range("O" + str1)
                str2 = str2 + Sheets("Package costs").Range("P" + str1)
                str2 = str2 + Sheets("Package costs").Range("Q" + str1)
                str2 = str2 + Sheets("Package costs").Range("R" + str1)
                str2 = str2 + Sheets("Package costs").Range("S" + str1)
                str2 = str2 + Sheets("Package costs").Range("T" + str1)
                str2 = str2 + Sheets("Package costs").Range("U" + str1)
                str2 = str2 + Sheets("Package costs").Range("V" + str1)
                str2 = str2 + Sheets("Package costs").Range("W" + str1)
                str2 = str2 + Sheets("Package costs").Range("X" + str1)
                str2 = str2 + Sheets("Package costs").Range("Y" + str1)
                str2 = str2 + Sheets("Package costs").Range("Z" + str1)
                str2 = str2 + Sheets("Package costs").Range("AA" + str1)
                str2 = str2 + Sheets("Package costs").Range("AB" + str1)
                str2 = str2 + Sheets("Package costs").Range("AC" + str1)
                str2 = str2 + Sheets("Package costs").Range("AD" + str1)
                str2 = str2 + Sheets("Package costs").Range("AE" + str1)
                str2 = str2 + Sheets("Package costs").Range("AF" + str1)
                str2 = str2 + Sheets("Package costs").Range("AG" + str1)
                str2 = str2 + Sheets("Package costs").Range("AH" + str1)
                credit_tech = str2
                
                str1 = Mid(Str(vehicle_counter), 2) 'Get vehicle credit category
                str2 = Sheets("Baseline").Range("J" + str1)
                credit_category = str2
                
                str3 = credit_category + "-" + cost_analysis_year 'Find credit row
                For int1 = 2 To 100
                    If int1 = 100 Then Stop 'Credit row not found
                    str1 = Mid(Str(int1), 2)
                    str1 = "A" + str1
                    str1 = Sheets("Credits").Range(str1)
                    If str1 = str3 Then
                        credit_row = int1
                        Exit For
                    End If
                Next
                
                credit_total = 0
                For int1 = 66 To 80 '14 possible credits
                    str1 = Sheets("Credits").Range(Chr(int1) + "1") 'Add up credits
                    If str1 = "" Then Exit For
                    If InStr(credit_tech, str1) <> 0 Then
                        str2 = Sheets("Credits").Range(Chr(int1) + Mid(Str(credit_row), 2))
                        credit_total = credit_total + Val(str2)
                    End If
                Next
                
                If package_number > 0 Then 'Skip if OEB 0
                    int1 = 50 * 2 + package_number + 1 'calculate Credit paste cell
                    int2 = Int(int1 / 26)
                    Select Case int2
                        Case Is = 0
                            str3 = Chr(65 + int1)
                        Case Else
                            str1 = Chr(64 + int2)
                            str2 = Chr(65 + int1 - int2 * 26)
                            str3 = str1 + str2
                    End Select
                    Sheets("Output TEB CEB").Range(str3 + Mid(Str(vehicle_counter), 2)) = Str(credit_total)
                
                    If vehicle_counter = 2 Then 'Label column
                        Sheets("Output TEB CEB").Range(str3 + "1") = "OEB" + Str(package_number)
                    End If
                End If
                
                int1 = 50 * 3 + package_number + 2 'calculate TECH X paste cell
                int2 = Int(int1 / 26)
                Select Case int2
                    Case Is = 0
                        str3 = Chr(65 + int1)
                    Case Else
                        str1 = Chr(64 + int2)
                        str2 = Chr(65 + int1 - int2 * 26)
                        str3 = str1 + str2
                End Select
                Sheets("Output TEB CEB").Range(str3 + Mid(Str(vehicle_counter), 2)) = tech_total
                
                If vehicle_counter = 2 Then 'Label column
                    Sheets("Output TEB CEB").Range(str3 + "1") = "TECH" + Str(package_number)
                End If
                
                
            Case Else
                Stop
        End Select

skip_vehicle:

    Next 'Vehicle loop
    
    Exit Sub
    
error_handler_2:     'Display debug information on error
    On Error GoTo 0
    Sheets("Control").Range("A9") = "Diagnostic Results:"
    If sub_type = 1 Then
        Sheets("Control").Range("A10") = "Scenario Row = "
    Else
        Sheets("Control").Range("A10") = "Baseline Row = "
    End If
    Sheets("Control").Range("A11") = "Cost Row = "
    Sheets("Control").Range("B10") = vehicle_counter
    Sheets("Control").Range("B11") = cost_row_pointer
    Application.ScreenUpdating = True
    'Application.Cursor = xlDefault
    Resume
    
End Sub

Sub rank_transmission()
    rank_output = 0
    If InStr(rank_input, "AT") <> 0 Then rank_output = 1
    If InStr(rank_input, "TRX11") <> 0 Then rank_output = 1
    If InStr(rank_input, "TRX12") <> 0 Then rank_output = 2
    If InStr(rank_input, "TRX21") <> 0 Then rank_output = 3
    If InStr(rank_input, "TRX22") <> 0 Then rank_output = 4
    If InStr(rank_input, "DCT") <> 0 Then rank_output = 2
    If InStr(rank_input, "MT") <> 0 Then rank_output = 2
    If InStr(rank_input, "6sp AT") <> 0 Then rank_output = 1
    If InStr(rank_input, "6sp DCT-wet") <> 0 Then rank_output = 2
    If InStr(rank_input, "6sp DCT-dry") <> 0 Then rank_output = 3
    If InStr(rank_input, "8sp AT") <> 0 Then rank_output = 4
    If InStr(rank_input, "8sp DCT-wet") <> 0 Then rank_output = 5
    If InStr(rank_input, "8sp DCT-dry") <> 0 Then rank_output = 6
    If Sheets("Control").Range("I14") = "True" Then
        If InStr(rank_input, "MT") <> 0 Then rank_output = 7
    End If
End Sub

Sub rank_hybrid()
    rank_output = 0
    If InStr(rank_input, "EV") <> 0 Then rank_output = 8
    If InStr(rank_input, "Stop") <> 0 Then rank_output = 1
    If InStr(rank_input, "HighV") <> 0 Then rank_output = 2
    If InStr(rank_input, "MHEV") <> 0 Then rank_output = 3
    If InStr(rank_input, "MHEV48V") <> 0 Then rank_output = 3
    If InStr(rank_input, "IMA") <> 0 Then rank_output = 3
    If InStr(rank_input, "Power") <> 0 Then rank_output = 4
    If InStr(rank_input, "Mode") <> 0 Then rank_output = 5
    If InStr(rank_input, "P2") <> 0 Then rank_output = 6
    If InStr(rank_input, "plug") <> 0 Then rank_output = 7
End Sub

Sub output_teb_ceb()

    For package_number = 1 To 50 'number_of_packages
        int1 = package_number * 7 + 10 'calculate TEB copy columns
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        copy_output = str3
        str1 = Sheets("Output").Range(str3 + "1")
        If package_number <= number_of_packages Then
        'Select Case str1
            'Case Is <> ""
                int1 = package_number + 1 'calculate TEB paste columns
                int2 = Int(int1 / 26)
                Select Case int2
                    Case Is = 0
                        str3 = Chr(65 + int1)
                    Case Else
                        str1 = Chr(64 + int2)
                        str2 = Chr(65 + int1 - int2 * 26)
                        str3 = str1 + str2
                End Select
                paste_output = str3

                array1 = Sheets("Output").Range(copy_output + "1", copy_output + Mid(Str(vehicle_counter_high), 2))

                Sheets("Output TEB CEB").Range(paste_output + "1", paste_output + Mid(Str(vehicle_counter_high), 2)) = array1

        End If
    Next
    package_numberp = package_number
    For package_number = 1 To 50 'number_of_packages
        int1 = package_number * 7 + 9 'calculate CEB copy columns
        int2 = Int(int1 / 26)
        Select Case int2
            Case Is = 0
                str3 = Chr(65 + int1)
            Case Else
                str1 = Chr(64 + int2)
                str2 = Chr(65 + int1 - int2 * 26)
                str3 = str1 + str2
        End Select
        copy_output = str3
        str1 = Sheets("Output").Range(str3 + "1")
        If package_number <= number_of_packages Then
        'Select Case str1
            'Case Is <> ""
                int1 = package_number + 0 + package_numberp 'calculate CEB paste columns
                int2 = Int(int1 / 26)
                Select Case int2
                    Case Is = 0
                        str3 = Chr(65 + int1)
                    Case Else
                        str1 = Chr(64 + int2)
                        str2 = Chr(65 + int1 - int2 * 26)
                        str3 = str1 + str2
                End Select
                paste_output = str3
                array1 = Sheets("Output").Range(copy_output + "1", copy_output + Mid(Str(vehicle_counter_high), 2))
                Sheets("Output TEB CEB").Range(paste_output + "1", paste_output + Mid(Str(vehicle_counter_high), 2)) = array1

        End If
    Next

    str2 = "C2:AZ" + Mid(Str(vehicle_counter_high), 2)
    Sheets("Output TEB CEB").Select
    Range(str2).Select
    Selection.NumberFormat = "0.0"
    str2 = "BA2:CX" + Mid(Str(vehicle_counter_high), 2)
    Sheets("Output TEB CEB").Select
    Range(str2).Select
    Selection.NumberFormat = "$0"
    Range("A1").Select
    Sheets("Control").Select
    
End Sub

Sub initalize()

    'Application.Cursor = xlWait
    '''''Application.Cursor = xlNorthwestArrow

    Application.Calculation = xlManual
    Application.ScreenUpdating = False
    
    Sheets("Control").Select
    Range("A9:B18").Select 'clear diagnostics
    Selection.ClearContents
    Sheets("Control").Range("A1:A1").Select
    
    version_number = "4.28"
    Sheets("Control").Range("B2") = version_number
    
    cost_analysis_year = Sheets("Control").Range("B6")
    WRcap_year = Sheets("Control").Range("B7")
    Sheets("Package Costs").Range("H15") = cost_analysis_year
    
    'this section selects the appropriate %WR cap text strings & CO2 values (TSh)
    Sheets("Baseline").Select
    If WRcap_year < 2021 Then
        StoreWRcaps = Range("AL2:AL3500")
    ElseIf WRcap_year < 2025 Then
        StoreWRcaps = Range("AM2:AM3500")
    Else
        StoreWRcaps = Range("AN2:AN3500")
    End If
    
    If cost_analysis_year = 2021 Then
        StoreCO2 = Range("AO2:AO3500")
    ElseIf cost_analysis_year = 2025 Then
        StoreCO2 = Range("AP2:AP3500")
    Else
        StoreCO2 = Range("AP2:AP3500")
    End If
    Sheets("Baseline").Range("AI2:AI3500") = StoreWRcaps
    Sheets("Baseline").Range("AJ2:AJ3500") = StoreCO2
    'end of section to select the appropriate %WR cap text strings & CO2 values
    
    Sheets("Output").Select 'clear output sheet
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
    
    Sheets("Output TEB CEB").Select 'clear output teb ceb sheet
    Cells.Select
    Selection.ClearContents
    Selection.NumberFormat = "General"
    Range("A1").Select
    
    Sheets("Output TEB CEB").Range("C1:AZ1") = "TEB X" ' Fill top row
    
    Sheets("Output TEB CEB").Range("BA1:CX1") = "CEB X" ' Fill top row
    
    Sheets("Output TEB CEB").Range("CY1:EV1") = "OEB X" ' Fill top row
    
    Sheets("Output TEB CEB").Range("EW1:GU1") = "TECH X" ' Fill top row
    
    Sheets("Mapping").Select 'clear mapping sheet
    Cells.Select
    Selection.ClearContents
    Range("A1").Select
    
    Sheets("Control").Select
    
    Sheets("Control").Range("E12") = ""
    Sheets("Control").Range("E13") = ""
    
End Sub

Sub finish()
    Application.ScreenUpdating = True
    'Application.Cursor = xlDefault
End Sub
        
Sub calc_LPM()

        ' Populate LPM Sheet
        
        Sheets(vehicle_class).Range("L32", "L73") = "0"
        
        'Worksheets(vehicle_class).Calculate
        
        'if vehicle_counter=25 then stop
            
        ' Aero
        
        str1 = "AE" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "Aero1" Then
            Sheets(vehicle_class).Range("K33") = "10%"
            Sheets(vehicle_class).Range("L33") = "1"
        ElseIf str1 = "Aero2" Then
            Sheets(vehicle_class).Range("K33") = "20%"
            Sheets(vehicle_class).Range("L33") = "1"
        End If

        ' Rolling
        str1 = "AF" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "LRRT1" Then
            Sheets(vehicle_class).Range("K34") = "10%"
            Sheets(vehicle_class).Range("L34") = "1"
        ElseIf str1 = "LRRT2" Then
            Sheets(vehicle_class).Range("K34") = "20%"
            Sheets(vehicle_class).Range("L34") = "1"
        End If
        
        ' Lube
        str1 = "L" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "LUB" Then
            Sheets(vehicle_class).Range("L35") = "1"
        End If
        
        ' Engine Friction
        str1 = "M" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "EFR2") <> 0 Then
            Sheets(vehicle_class).Range("L36") = "1"
            Sheets(vehicle_class).Range("K36") = "2"
        ElseIf InStr(str1, "EFR") <> 0 Or InStr(str1, "EFR1") Then
            Sheets(vehicle_class).Range("L36") = "1"
            Sheets(vehicle_class).Range("K36") = "1"
        End If
        
        ' Convert to DOHC
        str1 = "K" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DOHC") <> 0 Then
            Sheets(vehicle_class).Range("L37") = "1"
        End If
        
        ' ICP
        str1 = "N" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "VVTI") <> 0 Then
            Sheets(vehicle_class).Range("L38") = "1"
        End If
        
        
        ' DCP
        str1 = "N" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "VVTD") <> 0 Then
            Sheets(vehicle_class).Range("L39") = "1"
        End If
        
        ' CCP
        str1 = "N" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "VVTC") <> 0 Then
            Sheets(vehicle_class).Range("L40") = "1"
        End If

        ' DEAC
        str1 = "P" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "Deac") <> 0 Then
            Sheets(vehicle_class).Range("L41") = "1"
        End If

        ' DVVL
        str1 = "O" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "VVLTD") <> 0 Then
            Sheets(vehicle_class).Range("L42") = "1"
        End If

        ' CVVL
        str1 = "O" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "VVLTC") <> 0 Then
            Sheets(vehicle_class).Range("L43") = "1"
        End If
        
        ' HEG
        'str2 = Sheets(vehicle_class).Range("A4")
        'str2 = LPM_Vehicle_Type
        'str1 = "W" + Mid(Str(cost_row_pointer), 2) 'Populate
        'str1 = Sheets(cost_sheet).Range(str1)
        'If InStr(str1, "HEG2") <> 0 And str2 <> "Truck" Then
        '    Sheets(vehicle_class).Range("L54") = "1"
        '    Sheets(vehicle_class).Range("K54") = "25%"
        'ElseIf InStr(str1, "HEG2") <> 0 And str2 = "Truck" Then
        '    Sheets(vehicle_class).Range("L54") = "1"
        '    Sheets(vehicle_class).Range("K54") = "25%"
        'ElseIf InStr(str1, "HEG1") <> 0 And str2 <> "Truck" Then
        '    Sheets(vehicle_class).Range("L54") = "1"
        '    Sheets(vehicle_class).Range("K54") = "7%"
        'ElseIf InStr(str1, "HEG1") <> 0 And str2 = "Truck" Then
        '    Sheets(vehicle_class).Range("L54") = "1"
        '    Sheets(vehicle_class).Range("K54") = "7%"
        'End If

        ' Turbo
        str1 = "U" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "TURB27") <> 0 Then
            Sheets(vehicle_class).Range("L44") = "1" '27 bar turbo
            Sheets(vehicle_class).Range("K44") = "55%"
        ElseIf InStr(str1, "TURB24") <> 0 Then
            Sheets(vehicle_class).Range("L44") = "1" '24 bar turbo
            Sheets(vehicle_class).Range("K44") = "50%"
        ElseIf InStr(str1, "TURBM") <> 0 Then
            Sheets(vehicle_class).Range("L44") = "1" '21 bar turbo Miller
            Sheets(vehicle_class).Range("K44") = "42%"
        ElseIf InStr(str1, "TURB21") <> 0 Then
            Sheets(vehicle_class).Range("L44") = "1" ' 21 bar turbo
            Sheets(vehicle_class).Range("K44") = "42%"
        ElseIf InStr(str1, "TURB") <> 0 Then
            Sheets(vehicle_class).Range("L44") = "1" ' 18 bar turbo
            Sheets(vehicle_class).Range("K44") = "33%"
        End If
        
        ' 5-Speed
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "5sp") <> 0 Then
            Sheets(vehicle_class).Range("L45") = "1"
        End If
        
        ' 6-Speed
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "6sp") <> 0 Then
            Sheets(vehicle_class).Range("L46") = "1"
        End If
        
        ' 8-Speed
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "8sp") <> 0 Then
            Sheets(vehicle_class).Range("L47") = "1"
        End If
        
        ' CVT
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "CVT") <> 0 Then
            Sheets(vehicle_class).Range("L48") = "1"
        End If
        
        ' ASL
        str1 = "X" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "ASL2") <> 0 Then
            Sheets(vehicle_class).Range("L51") = "0"
            Sheets(vehicle_class).Range("L52") = "1"
        ElseIf InStr(str1, "ASL") <> 0 Or InStr(str1, "ASL1") <> 0 Then
            Sheets(vehicle_class).Range("L51") = "1"
            Sheets(vehicle_class).Range("L52") = "0"
        End If
     
        ' Agressive TC Lockup
        str1 = "Y" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "TORQ") <> 0 Then
            Sheets(vehicle_class).Range("L53") = "1"
        End If
        
        
        ' AMT Wet or 6 speed manual
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DCT-wet") <> 0 Or InStr(str1, "6sp MT") <> 0 Then
            Sheets(vehicle_class).Range("L49") = "1"
        End If
        
        ' AMT Dry or AMT with TC
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DCT-dry") <> 0 Then
            Sheets(vehicle_class).Range("L50") = "1"
        ElseIf InStr(str1, "DCT-tc") <> 0 Then
            Sheets(vehicle_class).Range("L50") = "1"
        End If
        
        ' TRXXX
        str1 = "Z" + Mid(Str(cost_row_pointer), 2) 'Populate TRXX
        str1 = Sheets(cost_sheet).Range(str1)
        str3 = "T" + Mid(Str(cost_row_pointer), 2) 'Populate ATK
        str3 = Sheets(cost_sheet).Range(str3)
        str4 = "R" + Mid(Str(cost_row_pointer), 2) 'Populate EGR
        str4 = Sheets(cost_sheet).Range(str4)
        str2 = LPM_Vehicle_Type
        If InStr(str1, "TRX11") <> 0 Then 'TRX11
            Sheets(vehicle_class).Range("L46") = "1" '6 speed HEG1
            Sheets(vehicle_class).Range("L52") = "1" 'ASL2
            Sheets(vehicle_class).Range("L53") = "1" 'TORQ
            Sheets(vehicle_class).Range("L54") = "1"
            Sheets(vehicle_class).Range("K54") = "7%"
        ElseIf InStr(str1, "TRX12") <> 0 Then 'TRX12
            Sheets(vehicle_class).Range("L46") = "1" '6 speed HEG2
            Sheets(vehicle_class).Range("L52") = "1" 'ASL2
            Sheets(vehicle_class).Range("L53") = "1" 'TORQ
            If str2 = "LPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            ElseIf str2 = "MPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            ElseIf str2 = "HPW" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            ElseIf str2 = "LPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            ElseIf str2 = "MPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            ElseIf str2 = "Truck" Then
                Sheets(vehicle_class).Range("L54") = "1"
                Sheets(vehicle_class).Range("K54") = "25%"
            End If
        ElseIf InStr(str1, "TRX21") <> 0 Then 'TRX21
            Sheets(vehicle_class).Range("L47") = "1" '8 speed HEG1
            Sheets(vehicle_class).Range("L52") = "1" 'ASL2
            Sheets(vehicle_class).Range("L53") = "1" 'TORQ
            Sheets(vehicle_class).Range("L54") = "1"
            If str2 = "LPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "1%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            ElseIf str2 = "MPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "4%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            ElseIf str2 = "HPW" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "0%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            ElseIf str2 = "LPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "1%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            ElseIf str2 = "MPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "0%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            ElseIf str2 = "Truck" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str4, "EGR") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "5%"
                Else
                    Sheets(vehicle_class).Range("K54") = "7%"
                End If
            End If
        ElseIf InStr(str1, "TRX22") <> 0 Then 'TRX22
            Sheets(vehicle_class).Range("L47") = "1" '8 speed HEG2
            Sheets(vehicle_class).Range("L52") = "1" 'ASL2
            Sheets(vehicle_class).Range("L53") = "1" 'TORQ
            If str2 = "LPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "38%"
                Else
                    Sheets(vehicle_class).Range("K54") = "38%"
                End If
            ElseIf str2 = "MPW_LRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "55%"
                Else
                    Sheets(vehicle_class).Range("K54") = "46%"
                End If
            ElseIf str2 = "HPW" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "78%"
                Else
                    Sheets(vehicle_class).Range("K54") = "73%"
                End If
            ElseIf str2 = "LPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "46%"
                Else
                    Sheets(vehicle_class).Range("K54") = "40%"
                End If
            ElseIf str2 = "MPW_HRL" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "53%"
                Else
                    Sheets(vehicle_class).Range("K54") = "47%"
                End If
            ElseIf str2 = "Truck" Then
                Sheets(vehicle_class).Range("L54") = "1"
                If InStr(str3, "ATK2") <> 0 Then
                    Sheets(vehicle_class).Range("K54") = "50%"
                Else
                    Sheets(vehicle_class).Range("K54") = "46%"
                End If
            End If
        End If
      
        ' Start-Stop 12V and 42V
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "Stop-Start HighV" <> 0 Then
            Sheets(vehicle_class).Range("L55") = "0"
            Sheets(vehicle_class).Range("L56") = "1"
        ElseIf str1 = "Stop-Start" <> 0 Then
            Sheets(vehicle_class).Range("L55") = "1"
            Sheets(vehicle_class).Range("L56") = "0"
        End If
        
        ' Improved 12V Acc and Alternator (IACC)
        str1 = "AC" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "IACC" Or str1 = "IACC1" Then
            Sheets(vehicle_class).Range("L59") = "1" 'Accessories
            'Sheets(vehicle_class).Range("L61") = "1" 'Alternator
        End If
        
        ' Improved 12V Acc and Alternator (IACC2)
        str1 = "AC" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "IACC2" Then
            Sheets(vehicle_class).Range("L59") = "1" 'Accessories
            Sheets(vehicle_class).Range("L61") = "1" 'Alternator
            Sheets(vehicle_class).Range("L57") = "1" 'Alternator Regen
        End If
        
        ' EPS
        str1 = "AD" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "EPS" Then
            Sheets(vehicle_class).Range("L58") = "1"
            Sheets(vehicle_class).Range("K58") = "100%"
        ElseIf str1 = "EHPS" Then
            Sheets(vehicle_class).Range("L58") = "1"
            Sheets(vehicle_class).Range("K58") = "50%"
        End If
        
        ' Improved 42V Acc and Alternator
        str1 = "AC" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "IACC 42V" Then
            Sheets(vehicle_class).Range("L60") = "1"
        End If
        
        ' GDI (Stoich)
        str1 = "S" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DI") <> 0 Then
            Sheets(vehicle_class).Range("L62") = "1"
        End If

        ' GDI (Lean)
        str1 = "Q" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "LB") <> 0 Then
            Sheets(vehicle_class).Range("L64") = "1"
            Sheets(vehicle_class).Range("L62") = "0" 'Turn off plain GDI
        End If
        
        ' GDI - EGR
        str1 = "R" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "EGR") <> 0 Then
            Sheets(vehicle_class).Range("L63") = "1"
            Sheets(vehicle_class).Range("L62") = "0" 'Turn off plain GDI
        End If
        
        ' GDI - Atkinson
        str1 = "T" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        
        Select Case LPM_Vehicle_Type
        
            Case "LPW_LRL"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "1.1"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.62"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case "MPW_LRL"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "0.85"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.5"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case "HPW"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "0.45"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.35"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case "LPW_HRL"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "1.1"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.48"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case "MPW_HRL"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "0.98"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.75"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case "Truck"
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                Sheets(vehicle_class).Range("L70") = "0.95"
            ElseIf InStr(str1, "ATK2") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.35"
            ElseIf InStr(str1, "ATK1") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1"
            End If
            
            Case Else
                Stop  'LPM type not found
                        
        End Select

        Select Case LPM_Vehicle_Type
        
        Case "LPW_LRL"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1.1"
            End If
            
        Case "MPW_LRL"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1.2"
            End If
            
        Case "HPW"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1.4"
            End If
            
        Case "LPW_HRL"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.8"
            End If
            
        Case "MPW_HRL"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "1.35"
            End If
        
        Case "Truck"
            str1 = "U" + Mid(Str(cost_row_pointer), 2) 'See if TURBM to properly apply Atkinson factor
            str1 = Sheets(cost_sheet).Range(str1)
            If InStr(str1, "TURBM") <> 0 Then
                Sheets(vehicle_class).Range("L70") = "0.7"
            End If
        
        Case Else
            Stop 'LPM vehicle class not found
        
        End Select
        
        ' Diesel - LNT
        str1 = "Q" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "LNT") <> 0 Then
            Sheets(vehicle_class).Range("L65") = "1"
            Sheets(vehicle_class).Range("L35") = "0" 'LUB in engine map
            Sheets(vehicle_class).Range("L36") = "0" 'EFR in engine map
            Sheets(vehicle_class).Range("L37") = "0" 'DOHC credit in engine map
        End If
        
        ' Diesel - SCR
        str1 = "Q" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DSL-SCR") <> 0 Then
            Sheets(vehicle_class).Range("L66") = "1"
            Sheets(vehicle_class).Range("L35") = "0" 'LUB in engine map
            Sheets(vehicle_class).Range("L36") = "0" 'EFR in engine map
            Sheets(vehicle_class).Range("L37") = "0" 'DOHC credit in engine map
        End If
        
        ' Diesel - Advanced
        str1 = "Q" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "DSL-Adv") <> 0 Then
            Sheets(vehicle_class).Range("L71") = "1"
            Sheets(vehicle_class).Range("L35") = "0" 'LUB in engine map
            Sheets(vehicle_class).Range("L36") = "0" 'EFR in engine map
            Sheets(vehicle_class).Range("L37") = "0" 'DOHC credit in engine map
        End If
        
        ' Mass Reduction
        
        'str1 = "AI" + Mid(Str(cost_row_pointer), 2) 'Populate
        'str1 = Sheets(cost_sheet).Range(str1)
        str1 = Str(mass_effectiveness)
        If str1 <> "" Then str1 = str1 + "%"
        If mass_effectiveness > 0 Then
            Sheets(vehicle_class).Range("L32") = "1"
        Else
            Sheets(vehicle_class).Range("L32") = "0"
        End If
        Sheets(vehicle_class).Range("K32") = str1
        
        If mass_zero = 1 Then 'Make mass 0 when ranking engines
            Sheets(vehicle_class).Range("K32") = "0"
            Sheets(vehicle_class).Range("L32") = "0"
            mass_zero = 0
        End If
        
        ' Hybrid - IMA (2008)
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "IMA") <> 0 Then
            Sheets(vehicle_class).Range("L67") = "0.7"
            Sheets(vehicle_class).Range("K67") = "10"
        End If
            
        ' Hybrid - MHEV
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "MHEV") <> 0 Then
            Sheets(vehicle_class).Range("L67") = "1" 'Turn on Hybrid
            'Sheets(vehicle_class).Range("K67") = "15" 'Set motor size
            
            'str2 = Sheets(vehicle_class).Range("A4") 'Get vehicle type in LPM
            str2 = LPM_Vehicle_Type
            Select Case str2
            Case "LPW_LRL"
                Sheets(vehicle_class).Range("K67") = "5.45" 'Set factor
            Case "MPW_LRL"
                Sheets(vehicle_class).Range("K67") = "6.05" 'Set factor
            Case "HPW"
                Sheets(vehicle_class).Range("K67") = "8.7" 'Set factor
            Case "LPW_HRL"
                Sheets(vehicle_class).Range("K67") = "6.75" 'Set factor
            Case "MPW_HRL"
                Sheets(vehicle_class).Range("K67") = "9.7" 'Set factor
            Case "Truck"
                Sheets(vehicle_class).Range("K67") = "9.8" 'Set factor
            Case Else
                Stop 'vehicle class not found
            End Select
            
            Sheets(vehicle_class).Range("L61") = "0" 'Turn off 70% Alternator
            Sheets(vehicle_class).Range("L57") = "0" 'Turn off Alternator Regen
            Sheets(vehicle_class).Range("L60") = Sheets(vehicle_class).Range("L59") 'Move IACC1 to HV if present
            Sheets(vehicle_class).Range("L59") = "0" 'Turn off 12v Improved Accessories
        End If
        
        ' Hybrid - P2 (2020)
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "P2") <> 0 Then
            Sheets(vehicle_class).Range("L61") = "0" 'Turn off 70% Alternator
            Sheets(vehicle_class).Range("L57") = "0" 'Turn off Alternator Regen
            Sheets(vehicle_class).Range("L60") = Sheets(vehicle_class).Range("L59") 'Move IACC1 to HV if present
            Sheets(vehicle_class).Range("L59") = "0" 'Turn off 12v Improved Accessories
            'str1 = Sheets(vehicle_class).Range("A4") 'Get vehicle type in LPM
            str1 = LPM_Vehicle_Type
            Select Case str1
            Case "LPW_LRL"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "23.2" 'Factor
            Case "MPW_LRL"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "38.5" 'Factor
            Case "HPW"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "71" 'Factor
            Case "LPW_HRL"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "30.8" 'Factor
            Case "MPW_HRL"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "61.5" 'Factor
            Case "Truck"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "52" 'Factor
            Case Else
                Stop
            End Select
        End If
        
        ' Hybrid - Power Split (2008)
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "plit") <> 0 Then
            Sheets(vehicle_class).Range("L67") = "0.6"
            Sheets(vehicle_class).Range("K67") = "50"
        End If
        
        ' Hybrid - 2-Mode (2008)
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "2Mode") <> 0 Then
            Sheets(vehicle_class).Range("L67") = "0.6"
            Sheets(vehicle_class).Range("K67") = "50"
        End If
                
        ' Plug In
        str1 = "AB" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 <> "" Then
            Sheets(vehicle_class).Range("L61") = "0" 'Turn off 70% Alternator
            Sheets(vehicle_class).Range("L57") = "0" 'Turn off Alternator Regen
            Sheets(vehicle_class).Range("L73") = "1"
            If InStr(str1, "Plug-in") <> 0 Then
                Sheets(vehicle_class).Range("K73") = "50%"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "30" 'Motor size
            ElseIf InStr(str1, "REEV40") <> 0 Then
                Sheets(vehicle_class).Range("K73") = "63%"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "30" 'Motor size
            ElseIf InStr(str1, "REEV20") <> 0 Then
                Sheets(vehicle_class).Range("K73") = "40%"
                Sheets(vehicle_class).Range("L67") = "1" 'Hybrid
                Sheets(vehicle_class).Range("K67") = "30" 'Motor size
            End If
        End If
        
        ' EV
        str1 = "AA" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If InStr(str1, "EV75") <> 0 Or InStr(str1, "EV100") <> 0 Then
            Sheets(vehicle_class).Range("L73") = "1"
            Sheets(vehicle_class).Range("K73") = "100%"
        ElseIf InStr(str1, "EV150") <> 0 Or InStr(str1, "EV200") <> 0 Then
            Sheets(vehicle_class).Range("L73") = "1"
            Sheets(vehicle_class).Range("K73") = "100%"
        End If
        
        ' Low Drag Brakes
        str1 = "AG" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "LDB" Then
            Sheets(vehicle_class).Range("L69") = "1"
        End If
        
        ' Axle Disconnect
        str1 = "AH" + Mid(Str(cost_row_pointer), 2) 'Populate
        str1 = Sheets(cost_sheet).Range(str1)
        If str1 = "SAX" Then
            Sheets(vehicle_class).Range("L68") = "1"
        End If
        
        Worksheets(vehicle_class).Calculate
        
        If Not Application.CalculationState = xlDone Then
            DoEvents
        End If
        
        'If package_sheet = "Baseline" Then Stop
        
        lpm_eff = Sheets(vehicle_class).Range("A29")
        lpm_eff = lpm_eff * Sheets("Control").Range("B8")
        
        pw_type = 0
        If Sheets("Control").Range("I9") = "True" And sub_type > 1 And pw_calc = 1 Then 'adjust for pw
        
        'If vehicle_counter = 1998 Then Stop
        
            pw_calc = 0
            str1 = "T" + Mid(Str(cost_row_pointer), 2) 'get ATK
            str1 = Sheets(cost_sheet).Range(str1)
            str2 = "U" + Mid(Str(cost_row_pointer), 2) 'get TURB
            str2 = Sheets(cost_sheet).Range(str2)
            If InStr(str1, "ATK2") <> 0 And Sheets(vehicle_class).Range("L63") = "1" Then
                pw_type = 2 'future ATK
            ElseIf InStr(str1, "ATK2") <> 0 Then
                pw_type = 1 'present ATK
            ElseIf InStr(str2, "TURBM") <> 0 Then
                pw_type = 2 'future ATK
            ElseIf InStr(str2, "TURB18") <> 0 Then
                pw_type = 3 'TURB18
            ElseIf InStr(str2, "TURB24") <> 0 Then
                pw_type = 3 'TURB24
            End If
            
            str1 = "AS" + Mid(Str(vehicle_counter), 2) 'get PW
            pw_vehicle = Sheets("Baseline").Range(str1)
    
            Select Case LPM_Vehicle_Type
                Case "LPW_LRL"
                    float1 = pw_vehicle - 0.041948
                    If pw_vehicle <= 0.041948 Then
                        turbc = float1 * 1.2
                        atkfc = float1 * 1.2
                        atkpc = float1 * -0.6
                    Else
                        turbc = float1 * 1.2
                        atkfc = float1 * 1
                        atkpc = float1 * -1
                    End If
                    
                Case "MPW_LRL"
                    float1 = pw_vehicle - 0.056794
                    If pw_vehicle <= 0.056794 Then
                        turbc = float1 * 1.8
                        atkfc = float1 * 1.2
                        atkpc = float1 * -0.5
                    Else
                        turbc = float1 * 1.8
                        atkfc = float1 * 1
                        atkpc = float1 * -0.5
                    End If
                    
                Case "HPW"
                    float1 = pw_vehicle - 0.071440795
                    float2 = pw_vehicle - 0.11
                    If pw_vehicle <= 0.056794 Then
                        turbc = float1 * 2
                        atkfc = float1 * 1
                        atkpc = float1 * -1.4
                    ElseIf pw_vehicle <= 0.11 Then
                        turbc = float1 * 1.6
                        atkfc = float1 * 0.6
                        atkpc = float1 * -0.5
                    Else
                        turbc = float2 * 0.8
                        atkfc = float2 * 0.2
                        atkpc = float2 * -0.2
                    End If

                Case "LPW_HRL"
                    float1 = pw_vehicle - 0.044588046
                    If pw_vehicle <= 0.044588046 Then
                        turbc = float1 * 1.5
                        atkfc = float1 * 1.2
                        atkpc = float1 * -0.6
                    Else
                        turbc = float1 * 1.5
                        atkfc = float1 * 1
                        atkpc = float1 * -1
                    End If
                    
                Case "MPW_HRL"
                    float1 = pw_vehicle - 0.056794078
                    If pw_vehicle <= 0.056794078 Then
                        turbc = float1 * 2
                        atkfc = float1 * 1.2
                        atkpc = float1 * -0.5
                    Else
                        turbc = float1 * 2
                        atkfc = float1 * 1
                        atkpc = float1 * -0.5
                    End If
                    
                Case "Truck"
                    float1 = pw_vehicle - 0.061033804
                    If pw_vehicle <= 0.061033804 Then
                        turbc = float1 * 2
                        atkfc = float1 * 1.2
                        atkpc = float1 * -0.7
                    Else
                        turbc = float1 * 2
                        atkfc = float1 * 1
                        atkpc = float1 * -0.7
                    End If
                
                Case Else
                    Stop
            
            End Select
            
            Select Case pw_type
                Case 1
                    lpm_eff = lpm_eff + atkpc
                Case 2
                    lpm_eff = lpm_eff + atkfc
                Case 3
                    lpm_eff = lpm_eff + turbc
            End Select

        End If
        
        'If vehicle_counter = 24 Then Stop
        
End Sub

Sub eng_tech_load() 'Get baseline engine technologies

            array1 = Sheets(temp_sheet).Range("K" + Mid(Str(temp_counter), 2), "V" + Mid(Str(temp_counter), 2))
            Sheets(cost_sheet).Range("K" + Mid(Str(cost_row_pointer), 2), "V" + Mid(Str(cost_row_pointer), 2)) = array1

            Sheets(cost_sheet).Range("W" + Mid(Str(cost_row_pointer), 2), "AI" + Mid(Str(cost_row_pointer), 2)) = ""
            
End Sub

Sub replace_package1()

            array1 = Sheets(temp_sheet).Range("K" + Mid(Str(temp_counter), 2), "Z" + Mid(Str(temp_counter), 2))
            Sheets(cost_sheet).Range("K" + Mid(Str(cost_row_pointer), 2), "Z" + Mid(Str(cost_row_pointer), 2)) = array1
            
            If new_package = 0 Or ignore_penalty = 1 Then 'replace mass specific technologies
                str1 = "AA" + Mid(Str(temp_counter), 2) 'Hybrid
                str1 = Sheets(temp_sheet).Range(str1)
                str2 = "AA" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str2) = str1
                str1 = "AB" + Mid(Str(temp_counter), 2) 'Plug-In
                str1 = Sheets(temp_sheet).Range(str1)
                str2 = "AB" + Mid(Str(cost_row_pointer), 2)
                Sheets(cost_sheet).Range(str2) = str1
            End If
            
            array1 = Sheets(temp_sheet).Range("AC" + Mid(Str(temp_counter), 2), "AG" + Mid(Str(temp_counter), 2))
            Sheets(cost_sheet).Range("AC" + Mid(Str(cost_row_pointer), 2), "AG" + Mid(Str(cost_row_pointer), 2)) = array1
            
End Sub

Sub Button6_Click() 'Subroutine to Reset
    Call finish
    End
End Sub




Sub multipleRS_controller()

'this sub controls multiple RankedSet Machine runs, what inputs to use and output filenames
'note that using this sub saves Machine outputs to a separate file, not within the Machine file itself

    Application.ScreenUpdating = False
    CurrentPath = ActiveWorkbook.Path
    Machine = ActiveWorkbook.Name
    Application.Calculate 'this ensures that the multiple run count is set correctly
    Application.Calculation = xlCalculationManual
    
    Sheets("Multiple_controller").Select
    Range("A1:E17").Select
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
    ActiveWorkbook.SaveAs Filename:=CurrentPath + "\RScontroller_Inputs.xlsb", _
            FileFormat:=50, CreateBackup:=False
    ActiveWorkbook.Close
    
    Windows(Machine).Activate
    Range("A17").Select
    Count = ActiveCell.Value
    
    
    For c = 1 To Count
    
        Sheets("Multiple_controller").Select
        Range("A" & 1 + c).Select
        year_std = ActiveCell.Value
        Range("B" & 1 + c).Select
        year_MY = ActiveCell.Value
        Range("C" & 1 + c).Select
        file_input = ActiveCell.Value
        Range("D" & 1 + c).Select
        file_output = ActiveCell.Value
        
        Sheets("Scenario Packages").Select
        Range("B23:AI400000").ClearContents
        
        Workbooks.Open Filename:=CurrentPath + "\" + file_input
        Source = ActiveWorkbook.Name
        Sheets("Scenario Packages").Select
        Range("B23:AI23").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        
        'ActiveWorkbook.Close SaveChanges:=False
        
        Windows(Machine).Activate
        Sheets("Scenario Packages").Select
        Range("B23").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "=MAX(R[22]C[4]:R[1000]C[4])"
        ActiveSheet.Calculate
        MaxTP = ActiveCell.Value
        ActiveCell.ClearContents
        
        Windows(Source).Activate
        ActiveWorkbook.Close SaveChanges:=False
        
        Windows(Machine).Activate
        Sheets("Control").Select
        Range("B7").Select
        ActiveCell.Value = year_std
        Range("B6").Select
        ActiveCell.Value = year_MY
        Range("B4").Select
        ActiveCell.Value = MaxTP
        Application.Calculate 'this ensures that all cost sheets are set to the correct cost year
        Call Button3_Click
        
        Sheets("Output").Select
        Cells.Select
        Selection.Copy
        
        Workbooks.Add
        Temp = ActiveWorkbook.Name
        Sheets("Sheet1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        ActiveSheet.Name = "Output"
        Application.CutCopyMode = False
        
        Windows(Machine).Activate
        Sheets("Output TEB CEB").Select
        Cells.Select
        Selection.Copy
        
        Windows(Temp).Activate
        Sheets.Add
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        ActiveSheet.Name = "Output TEB CEB"
        Application.CutCopyMode = False
        
        Windows(Machine).Activate
        Sheets("Mapping").Select
        Cells.Select
        Selection.Copy
        
        Windows(Temp).Activate
        Sheets.Add
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        ActiveSheet.Name = "Mapping"
        Application.CutCopyMode = False
        
        ActiveWorkbook.SaveAs Filename:=CurrentPath + "\" + file_output + ".xlsb", _
            FileFormat:=50, CreateBackup:=False
        ActiveWorkbook.Close
        
        'Windows(Temp).Activate
        'ActiveWorkbook.Close
        Windows(Machine).Activate
    
    Next c
    
    Sheets("Control").Select
    ActiveWorkbook.Close SaveChanges:=False
    
End Sub

Sub multipleMS_controller()

'this sub controls multiple MasterSet Machine runs, what inputs to use and output filenames
'note that using this sub saves Machine outputs to a separate file, not within the Machine file itself

    Application.ScreenUpdating = False
    CurrentPath = ActiveWorkbook.Path
    Machine = ActiveWorkbook.Name
    Application.Calculate 'this ensures that the multiple run count is set correctly
    Application.Calculation = xlCalculationManual
    
    Sheets("Multiple_controller").Select
    Range("A1:E17").Select
    Selection.Copy
    Workbooks.Add
    Range("A1").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
    ActiveWorkbook.SaveAs Filename:=CurrentPath + "\MScontroller_Inputs.xlsb", _
            FileFormat:=50, CreateBackup:=False
    ActiveWorkbook.Close
    
    Windows(Machine).Activate
    Range("A17").Select
    Count = ActiveCell.Value
    Range("A15").Select
    year_Baseline = ActiveCell.Value
    
    For c = 1 To Count
    
        Sheets("Multiple_controller").Select
        Range("A" & 1 + c).Select
        year_std = ActiveCell.Value
        Range("B" & 1 + c).Select
        year_MY = ActiveCell.Value
        Range("C" & 1 + c).Select
        file_input = ActiveCell.Value
        Range("D" & 1 + c).Select
        file_output = ActiveCell.Value
        Range("E" & 1 + c).Select
        TPs = ActiveCell.Value
        Range("F" & 1 + c).Select
        max_VehType = ActiveCell.Value
        min_VehType = 1
'        max_VehType = 19
        
        TPs = TPs + 1 'Add 1 to TPs to accommodate the base pkg
        
        Sheets("Scenario Packages").Select
        Range("B23:AI400000").ClearContents
        
        Workbooks.Open Filename:=CurrentPath + "\" + file_input
        Source = ActiveWorkbook.Name
        'Range("A1").Select
        'ActiveCell.Formula = "=MIN(R[1]C[2]:R[400000]C[2])"
        'min_VehType = ActiveCell.Value
        'ActiveCell.Formula = "=MAX(R[1]C[2]:R[400000]C[2])"
        'max_VehType = ActiveCell.Value
        'ActiveCell.Formula = "=MAX(R[1]C[1]:R[400000]C[1])"
        'TPs = (ActiveCell.Value - max_VehType) * 100000 + 1
        Range("B2:AI2").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        
        'ActiveWorkbook.Close SaveChanges:=False
        
        Windows(Machine).Activate
        Sheets("Scenario Packages").Select
        Range("B23").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        
        'This next portion simply sets From/To values on the Scenario tab to work as TP# triggers with v3.69+ of the Machine's VBA
        Range("E23").Select
        ActiveCell.Value = 0
        Range("D24").Select
        ActiveCell.Value = 0
        Range("E24").Select
        ActiveCell.Value = 1
        Store1 = Range("D23:E24").Value
        For x = min_VehType + 1 To max_VehType
            Range("D" & 23 + (x - 1) * TPs, "E" & 23 + (x - 1) * TPs + 1) = Store1
        Next x
        Set Store1 = Nothing
        'Done setting TP# triggers
                
        Windows(Source).Activate
        Application.CutCopyMode = False
        ActiveWorkbook.Close SaveChanges:=False
        
        Windows(Machine).Activate
        Sheets("Control").Select
        Range("B7").Select
        ActiveCell.Value = year_std
        Range("B6").Select
        ActiveCell.Value = year_MY
        Range("B4").Select
        ActiveCell.Value = 0
        Application.Calculate 'this ensures that all cost sheets are set to the correct cost year
        Call Button3_Click
        
        Sheets("Output").Select
        Range("A1:C1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Copy
        
        Workbooks.Add
        Temp = ActiveWorkbook.Name
        Sheets("Sheet1").Select
        Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
                :=False, Transpose:=False
        ActiveSheet.Name = "Output"
        Application.CutCopyMode = False
        
        ActiveWorkbook.SaveAs Filename:=CurrentPath + "\" + file_output + ".xlsb", _
            FileFormat:=50, CreateBackup:=False
        ActiveWorkbook.Close
        
        Windows(Machine).Activate
    
    Next c
    
    Sheets("Control").Select
    ActiveWorkbook.Close SaveChanges:=False
    
End Sub













