Copy Values to an Adjacent Column per unique Customer

Plokimu77

Board Regular
Joined
Oct 1, 2014
Messages
138
Good Morning,

Could a VBA Macro possibly copy the total purchases of a customer found in Column C to Column D for each unique Customer.

Below are two tables, the first table with the raw data and the second table with the final result.

Any help is appreciated.

Thank you




Raw Data Table:
RowCustomerPurchase
1BobApple
2BobBanana
3BobApple
4
Total​
3
5JoeyOrange
6JoeyOrange
7
Total​
2
8MikeCherry
9MikeStrawberry
10MikeLemon
11MikeApple
Total​
4


Final Result Table:
RowCustomerPurchaseTotal
1BobApple3
2BobBanana
3BobApple
4
Total​
3
5JoeyOrange2
6JoeyOrange
7
Total​
2
8MikeCherry4
9MikeStrawberry
10MikeLemon
11MikeApple
Total​
4
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Try this code:
VBA Code:
Sub MyAddTotals()

    Dim c As String
    Dim r As Long
    Dim lr As Long
    Dim tot As Long
    
    Application.ScreenUpdating = False
    
'   Set which column Customer appears in
    c = "B"
    
'   Find last row in Customer column with data
    lr = Cells(Rows.Count, c).End(xlUp).Row
    
'   Loop through all rows from top tp bottom
    For r = lr To 1 Step -1
'       Check to see value in Customer column
        Select Case Cells(r, c)
        
            Case "Total"
'               See if not last row
                If r <> lr Then
'                   Populate row below with previous total
                    Cells(r, c).Offset(1, 2).Value = tot
                End If
'               Grab next total
                tot = Cells(r, c).Offset(0, 1).Value
                
            Case "Customer"
'               Populate row below with previous total
                Cells(r, c).Offset(1, 2).Value = tot
'               Exit loop
                Exit For
    
        End Select
    Next r
    
    Application.ScreenUpdating = True
    
    MsgBox "Macro complete!"
        
End Sub
As long as your structure you posted is accurate, it should work. The only thing you might need to change is this line here:
VBA Code:
    c = "B"
If the "Customer" column is anything other than column "B". Just change it to the correct column letter.
 
Upvote 0
Solution

Forum statistics

Threads
1,215,077
Messages
6,122,992
Members
449,094
Latest member
masterms

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