Sum column based on criteria vba

Shaikh Aziz

New Member
Joined
Dec 18, 2020
Messages
35
Office Version
  1. 2007
Platform
  1. Windows
Please find below samples, where I want to do coding for rest of my actual data set,
ColumnA has multiple states name, ColumnB has unique values, and in columnC we want a sumif of columnB & Merging cells results based on states name (columnA).

ColumnAColumnBColumnC
Maharashtra
5​
25
Maharashtra
10​
Maharashtra
10​
Gujarat
30​
180
Gujarat
40​
Gujarat
50​
Gujarat
60​
Odisha
80​
313
Odisha
50​
Odisha
70​
Odisha
68​
Odisha
45​
Our result would be come in columnC after sum of ColumnB with mergecells based on columnA.

I have tried below codings,

Dim ColumnA as long
Dim ColumnC as long

ColumnsC = 0
ColumnA = Worksheets("Sheet1").Cells(2, 1).value

Lastrow = Worksheets("sheet1").cells(Rows.Count, 1).End(xlUp).Row

For i = 2 to lastrow
IF Worksheets("Sheet1").Cells(i, 1).value = ColumnA Then
ColumnC = ColumnC + Worksheets("Sheet1").cells(i, 3).value
End if
Next

worksheets("sheet1").cells(2, 3).value = ColumnC

But it's calcuating for columnA (first state only).

Please help me to finding perfect coding.
Thankyou !!!
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi & welcome to MrExcel.
Why not just put this formula in col C?
+Fluff v2.xlsm
ABC
1ColumnAColumnBColumnC
2Maharashtra525
3Maharashtra10 
4Maharashtra10 
5Gujarat30180
6Gujarat40 
7Gujarat50 
8Gujarat60 
9Odisha80313
10Odisha50 
11Odisha70 
12Odisha68 
13Odisha45 
Master
Cell Formulas
RangeFormula
C2:C13C2=IF(A2<>A1,SUMIFS(B:B,A:A,A2),"")
 
Upvote 0
Dear Fluff,

I have huge data around with 96,000 rows and 58 columns, that was just a part of it for understanding purpose, I need to append that code into my actual data set code file. That's why I need macro coding.

Thanks
 
Upvote 0
Are you still looking at cols A & B with the output in C?
 
Upvote 0
Ok, how about
VBA Code:
Sub ShaikhAziz()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, Rw As Long
   
   With Sheets("Sheet1")
      Ary = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .exists(Ary(r, 1)) Then
            .Add Ary(r, 1), r
            Nary(r, 1) = Ary(r, 2)
         Else
            Rw = .Item(Ary(r, 1))
            Nary(Rw, 1) = Nary(Rw, 1) + Ary(r, 2)
         End If
      Next r
   End With
   Sheets("sheet1").Range("C2").Resize(UBound(Nary)).Value = Nary
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Sub ShaikhAziz()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, Rw As Long
  
   With Sheets("Sheet1")
      Ary = .Range("A2:B" & .Range("A" & Rows.Count).End(xlUp).Row).Value2
   End With
   ReDim Nary(1 To UBound(Ary), 1 To 1)
   With CreateObject("scripting.dictionary")
      For r = 1 To UBound(Ary)
         If Not .exists(Ary(r, 1)) Then
            .Add Ary(r, 1), r
            Nary(r, 1) = Ary(r, 2)
         Else
            Rw = .Item(Ary(r, 1))
            Nary(Rw, 1) = Nary(Rw, 1) + Ary(r, 2)
         End If
      Next r
   End With
   Sheets("sheet1").Range("C2").Resize(UBound(Nary)).Value = Nary
End Sub
Thankyou so much, But Output is not getting merged into ColumnC based on ColumnA.
 
Upvote 0
If you mean you want to use merged cells DON'T, they are an abomination & should be avoided like the plague.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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