Creating a Transit Time Function

TMackie

New Member
Joined
Dec 13, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
I am looking at creating a custom function to calculate transit time which goes by =roundup(miles/44,0) but then +10 for every 500 miles i.e.
0-500 is the base function
501-1000 is =roundup(miles/44,0)+10 and so forth to a cap of 5000 miles.

I know the base formula would be like this
Function TransitTime(Miles As Single) As Single

TransitTime = RoundUp(Miles / 44, 0)

End Function

But what is the process of creating an if statement for the mileage bands because I tried this below and it didn't work. Miles is what the cell will base the formula off of.

Function TransitTime(Miles As Single) As Single
If Miles < 500 Then
TransitTime = RoundUp(Miles / 44, 0)
Else
If Miles > 500 And Miles < 1000 Then
TransitTime = Round(RoundUp(Miles / 44, 0) + 10, 2)
Else
End Function
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
This can be worked into a function, but for the moment, do the results look like what you're after?

Book1
AB
1MilesTransit Time
22506
350022
475028
5100043
6125049
7150065
8175070
9200086
10225092
112500107
122750113
133000129
143250134
153500150
163750156
174000171
184250177
194500193
204750198
215000214
225250220
235500225
Sheet3
Cell Formulas
RangeFormula
B2:B23B2=ROUNDUP(A2/44,0)+(MIN(INT(A2/500),10)*10)
 
Upvote 0
This can be worked into a function, but for the moment, do the results look like what you're after?

Book1
AB
1MilesTransit Time
22506
350022
475028
5100043
6125049
7150065
8175070
9200086
10225092
112500107
122750113
133000129
143250134
153500150
163750156
174000171
184250177
194500193
204750198
215000214
225250220
235500225
Sheet3
Cell Formulas
RangeFormula
B2:B23B2=ROUNDUP(A2/44,0)+(MIN(INT(A2/500),10)*10)

Yes, that is how I want it to look, but what is the best way to build it as a function with VBA? Instead of creating a loop use ROUNDUP(A2/44,0)+(MIN(INT(A2/500),10)*10) instead?
 
Upvote 0
I'm not quite sure what you're asking. It works just fine as a worksheet formula, what is the need to make it a function? And why the thought of needing to loop?
 
Upvote 0
I'm not quite sure what you're asking. It works just fine as a worksheet formula, what is the need to make it a function? And why the thought of needing to loop?
So using your formula am I wanted to create a formula in VBA that allows me to store the formula you provided within TransitTime.

This formula will work with different people, but the more that I have to build into the function the more likely it gets confusing for others.
 
Upvote 0
A UDF will require VBA.
You can use Lambda to build your own function.
Are the results what you expect?
What happens after 5000 miles?

see detail in second example
T202312a.xlsm
ABC
1Miles100043
2
6f
Cell Formulas
RangeFormula
C1C1=TransitTime(B1)
Lambda Functions
NameFormula
TransitTime=LAMBDA(Miles,LET(m,Miles,IF(m<5001,ROUNDUP(m/44,0)+IF(m>500,(MIN(INT(m/500),10)*10),0),"")))


With name Manager, I named a new function.
I named the function TransitTime; name the function something appropriate.

T202312a.xlsm
ABC
1MilesTransit Time
225066
35001212
47502828
510004343
612504949
715006565
817507070
920008686
1022509292
112500107107
122750113113
133000129129
143250134134
153500150150
163750156156
174000171171
184250177177
194500193193
204750198198
215000214214
225250  
235500  
6f
Cell Formulas
RangeFormula
B2B2=IF(A2<5000,ROUNDUP(A2/44,0)+(MIN(INT(A2/500),10)*10),"")
C2:C23C2=TransitTime(A2)
B3:B19B3=IF(A3<5000,ROUNDUP(A3/44,0)+IF(A3>500,(MIN(INT(A3/500),10)*10),0),"")
B20:B23B20=IF(A20<5001,ROUNDUP(A20/44,0)+IF(A20>500,(MIN(INT(A20/500),10)*10),0),"")
Lambda Functions
NameFormula
TransitTime=LAMBDA(Miles,LET(m,Miles,IF(m<5001,ROUNDUP(m/44,0)+IF(m>500,(MIN(INT(m/500),10)*10),0),"")))
 
Last edited:
Upvote 0
For the UDF you can use this:

VBA Code:
Function TransitTime(ByVal Miles As Long)
'=ROUNDUP(A2/44,0)+(MIN(INT(A2/500),10)*10)
TransitTime = Application.WorksheetFunction.RoundUp(Miles / 44, 0) + (Application.WorksheetFunction.Min(Int(Miles / 500), 10) * 10)
End Function
 
Upvote 1

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top