+ Reply to Thread
Results 1 to 5 of 5

Custom VBA Function to Calculate Fuel Consumption

Hybrid View

  1. #1
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Custom VBA Function to Calculate Fuel Consumption

    I want some help in creating a UDF for calculation Mileage of a Car.


    Variables that will change - (Examples given in Bracket)

    Mileage/Liter (20 Kms/Liter)
    Fuel Rate/Liter (Rs. 80/Liter)

    The Calculation goes like -

    Mileage = (Fuel Rate/Liter) / (Mileage/Liter)

    That it -

    Mileage = 80/20

    Therefore Mileage is Rs. 4/Km



    Thank You,

    Deep
    Cheers!
    Deep Dave

  2. #2
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Custom VBA Function to Calculate Fuel Consumption

    Does this work?

    Function FUELRATE(rngFRperL As Range, rngMperL As Range) As Long
    
    FUELRATE = (rngMperL / rngFRperL)
    
    End Function
    Perhaps it was the Noid who should have avoided me...
    If you are satisfied with my solution click the small star icon on the left. Thanks
    1. Make a copy of your workbook and run the following code on your copy (just in case)
    2. With excel open, press ALT+F11 to open the Visual Basic Editor (VBE). From the "Insert" menu, select "Module".
    3. Paste the code from above into the empty white space. Close the VBE.
    4. From the developer tab, choose "Macros", select the Sub Name, and click "Run".

  3. #3
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: Custom VBA Function to Calculate Fuel Consumption

    Hey thank you for the reply..

    It does not work..

  4. #4
    Valued Forum Contributor AlvaroSiza's Avatar
    Join Date
    09-19-2007
    Location
    Staffordshire
    MS-Off Ver
    2007
    Posts
    591

    Re: Custom VBA Function to Calculate Fuel Consumption

    Assume:

    "A1" contains Fuel Rate / Liter (80), and
    "A2" contains Mileage/Liter (20)

    When I enter
    =FUELRATE(A1,A2)
    in Cell "A3" I get a value of 4, which is what your original post developed. If this is still in error, I am obviously misinterpreting your request.

    Public Function FUELRATE(rngFRperL As Range, rngMperL As Range) As Long
    
    FUELRATE = (rngFRperL / rngMperL)
    
    End Function

  5. #5
    Forum Expert NeedForExcel's Avatar
    Join Date
    03-16-2013
    Location
    Pune, India
    MS-Off Ver
    Excel 2016:2019, MS 365
    Posts
    3,876

    Re: Custom VBA Function to Calculate Fuel Consumption

    What you said is perfectly right.

    However those 80 & 20 will not be in any cell. I want to put that directly in a formula.

    I tried that, but it just wudnt work..

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1