VBA Sum Multiple Columns and Duplicate Rows using specific sheets

Noobiee

New Member
Joined
May 14, 2022
Messages
11
Office Version
  1. 2021
  2. 2019
  3. 2016
Platform
  1. Windows
Hey,
My first post sorry if being unclear,

I am complete newbie to VBA , I have a large workbook with multiple sheets. My goal is to have a macro that would Remove duplicate from Sheet (XXXX) from column A and Sum the respected value from column B as well as C.

So the data would have 3 columns that looks like this:

A B C
Name hrs units
Sam 2 1
Joe 1 4
Sam 2 3
Joe 1 1

A B C
Name hrs units
Sam 4 4
Joe 2 5


The problem with my code is that I cant have it SUM column C correctly but Column B works fine . Column C has some correct data but not fully as its miss matching the values in ( units) with ( name).
Here's the original code that works on 2 columns correctly but wont work 100% correctly on column C , I have alot of different macros running after this marco runs they are based on the having my data being in the same sheet and same offsets for columns. One thing worth mentioning that my data is being pulled from a url that the number of rows is changing every time the user decides to pull data but the column area a constant . All the help would be greatly appreciated. Thanks!

VBA Code:
Sub PPA()


With Worksheets("Induct") '


    With .Range("A1:B3500").Resize(.Cells(.Rows.Count, 3).End(xlUp).Row)

        .Copy

        With .Offset(, .Columns.Count + 1)

            .PasteSpecial xlPasteValues '

             .Columns(2).Offset(1).Resize(.Rows.Count - 1, 1).FormulaR1C1 = "=SUMIF(C1,RC1,C[-" & .Columns.Count + 1 & "])"

            .Value = .Value

            .RemoveDuplicates 1, xlYes

           .Columns(3).Offset(1).Resize(.Rows.Count - 1, 1).FormulaR1C1 = "=SUMIF(C1,RC1,C[-" & .Columns.Count + 1 & "])"

            .Value = .Value

            .RemoveDuplicates 1, xlYes

          

             Sheets("Induct").Select

    Columns("F:F").Select

    ActiveSheet.Range("$F$1:$F$240").RemoveDuplicates Columns:=1, Header:=xlYes

    

    

        End With

      

        End With


End With
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
VBA Code:
Sub test()
    Dim Wks As Worksheet
    Dim i&
    Dim a, w
    With CreateObject("Scripting.Dictionary")
                a = Cells(1, 1).Resize(Cells(Rows.Count, 1).End(xlUp).Row, 3)
                For i = 2 To UBound(a)
                    If a(i, 1) <> "" Then
                        If Not .Exists(a(i, 1)) Then
                            .Add a(i, 1), Array(a(i, 1), a(i, 2), a(i, 3))
                        Else
                            w = .Item(a(i, 1))
                            w(1) = w(1) + a(i, 2):  w(2) = w(2) + a(i, 3)
                            .Item(a(i, 1)) = w
                        End If
                    End If
                Next
        
       Cells(2, 5).Resize(.Count, 3) = Application.Index(.Items, 0, 0)
    End With
End Sub
 
Upvote 0
Solution
Thank you so much for your help this works! appreciate it I coded alot in C++ I am comfortable there only started 4 days ago with VBA its interesting but in a good way
 
Upvote 0
You are very welcome
And thank you for the feedback
Be happy and safe
 
Upvote 0

Forum statistics

Threads
1,214,793
Messages
6,121,617
Members
449,039
Latest member
Mbone Mathonsi

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