VBA - sum of Miles in Column B = to Name in Column A

smythcounty

New Member
Joined
Jul 29, 2021
Messages
42
Office Version
  1. 365
Platform
  1. Windows
Hello,

I know this is going to be simple for you all but I've fried my brain trying to get this two work.

I have names of people in column A and miles traveled in column B. How can I get the total sum of miles traveled by each person in VBA?

A (Name) B (Miles)
Jim Smith 20
Nancy Cook 21
John Daly 25
Jim Acosta 26
John Daly 35
Nancy Cook 84
Steve Durst 86
Steve Durst 44
John Daly 55

Jim Smith Total =?
Nancy Cook Total =?
John Daly Total =?
Jim Acosta Total = ?

I hope that makes sense.

Thanks!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Hopefully this helps out
 

Attachments

  • Capture.PNG
    Capture.PNG
    8.3 KB · Views: 3
Upvote 0
Why VBA? Are you processing it further? if so, what is the end goal?
 
Upvote 0
Can you post a sample of the data using the XL2BB tool (instructions in signature)? This way I can see column and rows and assist with a vba solution.
 
Upvote 0
How about
VBA Code:
Sub smythcounty()
   Dim Ary As Variant
   Dim Dic As Object
   Dim i As Long
   
   Ary = Range("A2:B" & Range("A" & Rows.Count).End(xlUp)).Value2
   
   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(Ary)
         .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + Ary(i, 2)
      Next i
      Sheets.Add.Name "Results"
      Sheets("Results").Range("A2").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
 
Upvote 0
VBA Upload.xlsx
ABCDEFGHI
1TruckDriverDateStart TimeEnd TimeMilesTotal MilesDEPARTURE TIMEARRIVAL TIME
2101Jsmith7/28/202106:27:09-0.436:27:09 AM6:37:00 AM
3101Jsmith7/28/2021--0.126:47:00 AM6:49:00 AM
4101Jsmith7/28/2021--0.127:15:17 AM7:19:07 AM
5101Jsmith7/28/2021--0.067:25:18 AM7:29:08 AM
6101Jsmith7/28/2021--6.217:37:18 AM8:00:52 AM
7101Jsmith7/28/2021--17.038:09:19 AM8:26:20 AM
8101Jsmith7/28/2021--56.368:31:20 AM9:41:22 AM
9101Jsmith7/28/2021--3.299:49:23 AM10:29:24 AM
10101Jsmith7/28/2021--0.1210:57:25 AM10:59:25 AM
11101Jsmith7/28/2021--51.4511:09:25 AM12:39:14 PM
12101Jsmith7/28/2021--30.0112:47:29 PM1:37:30 PM
13101Jsmith7/28/2021--6.282:03:33 PM2:25:27 PM
14101Jsmith7/28/2021--45.052:35:27 PM3:23:29 PM
15101Jsmith7/28/2021--0.193:31:34 PM3:37:34 PM
16101Jsmith7/28/2021-17:25:3151.514:11:36 PM5:19:38 PM
17102Ljohnson7/28/202103:03:10-142.983:35:11 AM6:05:16 AM
18102Ljohnson7/28/2021--5.536:15:16 AM6:31:17 AM
19102Ljohnson7/28/2021--0.437:01:18 AM7:09:18 AM
20102Ljohnson7/28/2021--0.197:17:18 AM7:21:19 AM
21102Ljohnson7/28/2021--0.127:43:19 AM7:47:19 AM
22102Ljohnson7/28/2021--0.817:57:31 AM8:13:20 AM
23102Ljohnson7/28/2021--49.968:19:21 AM9:35:32 AM
24102Ljohnson7/28/2021--0.379:41:32 AM9:47:24 AM
25102Ljohnson7/28/2021--0.1210:47:26 AM10:49:33 AM
26102Ljohnson7/28/2021--0.5611:21:27 AM11:33:27 AM
27102Ljohnson7/28/2021--160.2511:39:27 AM2:47:34 PM
28102Ljohnson7/28/2021--0.252:57:34 PM3:01:34 PM
29102Ljohnson7/28/2021--0.433:15:35 PM3:29:35 PM
30102Ljohnson7/28/2021--0.123:43:35 PM3:43:45 PM
31102Ljohnson7/28/2021--0.313:49:36 PM3:51:47 PM
32102Ljohnson7/28/2021--0.433:59:36 PM4:03:47 PM
33102Ljohnson7/28/2021--0.54:13:37 PM4:29:37 PM
34102Ljohnson7/28/2021--04:35:46 PM4:37:37 PM
35102Ljohnson7/28/2021--0.064:43:38 PM4:45:38 PM
36102Ljohnson7/28/2021-17:18:340.315:09:38 PM5:18:34 PM
manifest-by-vehicle-active-07-2

Can you post a sample of the data using the XL2BB tool (instructions in signature)? This way I can see column and rows and assist with a vba solution.
Can you post a sample of the data using the XL2BB tool (instructions in signature)? This way I can see column and rows and assist with a vba solution.
 
Upvote 0
As your data is not in columns A & B (as per your op) my code is now
VBA Code:
Sub smythcounty()
   Dim Ary As Variant
   Dim Dic As Object
   Dim i As Long
  
   Ary = Range("B2:F" & Range("B" & Rows.Count).End(xlUp).Row).Value2
  
   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(Ary)
         .Item(Ary(i, 1)) = .Item(Ary(i, 1)) + Ary(i, 5)
      Next i
      Sheets.Add.Name ="Results"
      Sheets("Results").Range("A2").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
Edited to correct code
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,214,983
Messages
6,122,595
Members
449,089
Latest member
Motoracer88

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