VBA SUM and Subtotal

freeveil

New Member
Joined
Jun 12, 2024
Messages
4
Office Version
  1. 365
Platform
  1. Windows
Hi, Struggling with VBA. Managed to do the basics of what I am trying to achieve, which is gather distances for my logistics company. I used google API to determine road mileage between locations, to determine the distance travelled on a job and then the subsequent empty running to get to the next job.

What I would like to do next is SUM the loaded mileage and the empty mileage together to correctly price the job, and then Subtotal each week's loaded, empty and total miles respectively to build an average earning report.

What is the best way to achieve this? I'm guessing there is a way to do this using VBA, but I was thinking a simpler solution would be extrapolating the dynamic data generated from the API into a static excel cell?

Below is an example of one weeks data, I have been doing one month per sheet so either 4 or 5 tables per sheet to accumulate this data from. Row 5 is a manual entry an example of what I am trying to achieve, the #values are just errors because i donbt have accurate address information further along the table.

Thanks,

Cell Formulas
RangeFormula
S4,S6:S13S4=GetDistance([@[From GetDistance Ref]],[@[To GetDistance Ref]])
T4,T6:T13T4=GetDistance([@[To GetDistance Ref]], X5)
U4:U13U4=[@[Job Miles]]+[@[Empty Running]]
S14S14=SUBTOTAL(109,[Job Miles])
T14T14=SUBTOTAL(109,[Empty Running])
U14U14=SUBTOTAL(109,[Total Miles])
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Please help me understand, you gather data with the Google API (how does that work?) and you need to sum up the data, in simple words.
I'm a little bit confused. It seems that you already found the answer when I look at your table.

You could add an IFERROR function to bypass the errors and a SUMPRODUCT function to sum the data per each week.

Maybe you could give another example showing a table with sample data.
Plus, color the cells with "static" data or Google API data let's say green and the cells that you need to be calculated in let's say yellow.
But that's just an idea...
 
Upvote 0
RTK Work 2024.xlsm
BCDEFG
2
3FromToJob MilesEmpty RunningTotal MilesInformation
4A Made up Place, BirminghamA Made up Place, Glasgow29047337This row has been made up manually, with static information.
5EdinburghLiverpool221 mi34.4 mi#VALUE!These rows are dynamic, the data pulled from google maps API.
6ManchesterInverness374 mi1 ft#VALUE!
7InvernessDroitwich462 miError#VALUE!
829047#VALUE!
9Notice these totals only recognise the static information
TRIAL (2)
Cell Formulas
RangeFormula
F4:F7F4=[@[Job Miles]]+[@[Empty Running]]
D5:D7D5=GetDistance([@From],[@To])
E5:E7E5=GetDistance([@To],B6)
D8D8=SUBTOTAL(109,[Job Miles])
E8E8=SUBTOTAL(109,[Empty Running])
F8F8=SUBTOTAL(109,[Total Miles])


Google API code
Function
GetDistance(origin As String, destination As String) As String
Dim apiKey As String
Dim url As String
Dim responseText As String
Dim objHTTP As Object
Dim distance As String

' Replace "YOUR_API_KEY" with your actual API key
apiKey = "YOUR_API_KEY"
url = "https://maps.googleapis.com/maps/api/distancematrix/json?origins=" & origin & "&destinations=" & destination & "&units=imperial&key=" & apiKey
' Send HTTP request to the Distance Matrix API
Set objHTTP = CreateObject("MSXML2.XMLHTTP")
objHTTP.Open "GET", url, False
objHTTP.send
responseText = objHTTP.responseText
' Parse the JSON response to extract distance
If InStr(responseText, "OK") > 0 Then
distance = Split(Split(responseText, """text"" : """)(1), """")(0)
Else
distance = "Error"
End If

' Return the distance
GetDistance = distance
End Function
 
Upvote 0
Thanks for the reply, I have attempted to clarify what I am looking for with my last post.

I enter location details into the FROM and TO columns. Google Maps API automatically finds the distances between those locations and then the subsequent travelling from TO to the next row's FROM.
These added together give me the total miles travelled on that Job.

On occassion I do need to enter manually static information, so I need to be able to add both static and dynamic information to find out all distances travelled over a week/month/year.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,177
Members
452,615
Latest member
bogeys2birdies

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