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!
 
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)).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
Thanks I was manually changing it. I'm getting Run-Time error 1004, Method 'Range of object'_Global failed here:
Ary = Range("B2:F" & Range("B" & Rows.Count).End(xlUp)).Value2
 
Upvote 0

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Oops, that should be
VBA Code:
   Ary = Range("B2:F" & Range("B" & Rows.Count).End(xlUp).Row).Value2
 
Upvote 0
Oops, that should be
VBA Code:
   Ary = Range("B2:F" & Range("B" & Rows.Count).End(xlUp).Row).Value2
Fixed that Thank You!
Now I get Runtime Error 438 "object doesnt support this object or method" here:
Sheets.Add.Name "Results"
 
Upvote 0
If I wanted to edit this to show the results in a specific column instead of another sheet, such as "J2" how would I do so?
 
Upvote 0
You can do that like
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
      Range("J2").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,647
Messages
6,120,722
Members
448,987
Latest member
marion_davis

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