VBA or Formula to SUM multiple cells into one and delete rows of remaining

Prasad K

Board Regular
Joined
Aug 4, 2021
Messages
189
Office Version
  1. 2016
  2. 2007
Platform
  1. Windows
Hi
Have data with multiple columns with

col-A Cus-Name
col-B InVoice-No
col-C First Bill-Amount
col-D Second Bill-Amount

i have applied this below VBA to Sum and Delete Rows here my problem is it's not giving result has i expect


This is data i have

MMM.xlsm
ABCD
1Cus-NameInVoice-NoFirst Bill-AmountSecond Bill-Amount
2Raju.MBVSI-001250.75425.98
3Raju.MBVSI-001450.25284.65
4Raju.MBVSI-001321.45674.25
5Raju.MBVSI-001458.32954.65
6Swathi.PBVSI-002250.75742.65
7Swathi.PBVSI-002450.25248.65
8Swathi.PBVSI-002321.45485.39
9Swathi.PBVSI-002458.32147.58
Sheet2



i want like this

MMM.xlsm
ABCD
1Cus-NameInVoice-NoFirst Bill-AmountSecond Bill-Amount
2Raju.MBVSI-0011480.772339.53
3Swathi.PBVSI-0021480.771624.27
Sheet2






VBA Code:
Sub CombineRows()
Dim WorkRng As Range
Dim Dic As Variant
Dim arr As Variant
On Error Resume Next
xTitleId = "Excel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set Dic = CreateObject("Scripting.Dictionary")
arr = WorkRng.Value
For i = 1 To UBound(arr, 1)
    Dic(arr(i, 1)) = Dic(arr(i, 1)) + arr(i, 2)
Next
Application.ScreenUpdating = False
WorkRng.ClearContents
WorkRng.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
WorkRng.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
Application.ScreenUpdating = True
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Hi, just removing the useless according to Excel basics a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
         Dim Rg(1) As Range
         Set Rg(1) = [Sheet2!B1]
         Application.ScreenUpdating = False
    With Rg(1).CurrentRegion.Columns
        While Rg(1).Row < .Rows.Count
            Set Rg(0) = Rg(1)(2)
            Set Rg(1) = .Item(2).Find(Rg(0), , xlValues, 1, , 2)
             If Rg(1).Row > Rg(0).Row Then Rg(0)(1, 2).Resize(, 2).Value2 = Array(Application.SumIf(.Item(2), Rg(0), .Item(3)), _
                                                                                  Application.SumIf(.Item(2), Rg(0), .Item(4)))
        Wend
            .RemoveDuplicates 2, 1
    End With
         Application.ScreenUpdating = True
         Erase Rg
End Sub
 
Last edited:
Upvote 0
Solution
Hi, just removing the useless according to Excel basics a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
         Dim Rg(1) As Range
         Set Rg(1) = [Sheet2!B1]
         Application.ScreenUpdating = False
    With Rg(1).CurrentRegion.Columns
        While Rg(1).Row < .Rows.Count
            Set Rg(0) = Rg(1)(2)
            Set Rg(1) = .Item(2).Find(Rg(0), , xlValues, 1, , 2)
             If Rg(1).Row > Rg(0).Row Then Rg(0)(1, 2).Resize(, 2).Value2 = Array(Application.SumIf(.Item(2), Rg(0), .Item(3)), _
                                                                                  Application.SumIf(.Item(2), Rg(0), .Item(4)))
        Wend
            .RemoveDuplicates 2, 1
    End With
         Application.ScreenUpdating = True
         Erase Rg
End Sub
i have getting error of (compile error variable not defined)
 
Upvote 0
No compile error on my side as it works a treat so erase the procedure and try again to copy/paste …​
 
Upvote 0
No compile error on my side as it works a treat so erase the procedure and try again to copy/paste …​
Yes Marc L
it's my mistake i have placed code in wrong way

Now it's working perfectly has i expected

Thank you so much for your help
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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