ScottyBee
09-30-2021, 01:29 PM
I am training a class and was showing students how to use the "Format" function in VBA. Everyone's code worked by returning one decimal place for the MPG except for one student. His function is returning multiple decimal places to the worksheet. I checked character for character to see how his code differed from mine and cannot see any differences.
We have created a function called MPG that calculates gas mileage and used the Format function so that the values returned in the worksheet are formatted with one decimal place. Below is the code and how the returned value is displayed in the worksheet.
Public Function MPG(BegMiles, EndMiles, FuelConsumed) As Single
Dim Mileage As Single
MPG = Format(((EndMiles - BegMiles) / FuelConsumed), "#,##0.0")
End Function
29025
Any ideas on why the format function isn't returning one decimal place? Thanks
We have created a function called MPG that calculates gas mileage and used the Format function so that the values returned in the worksheet are formatted with one decimal place. Below is the code and how the returned value is displayed in the worksheet.
Public Function MPG(BegMiles, EndMiles, FuelConsumed) As Single
Dim Mileage As Single
MPG = Format(((EndMiles - BegMiles) / FuelConsumed), "#,##0.0")
End Function
29025
Any ideas on why the format function isn't returning one decimal place? Thanks