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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
How about
VBA Code:
Sub ShaikhAziz()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, Rw As Long
   Dim Txt As String
   
   With Sheets("Sheet1")
      Ary = .Range("C2:N" & .Range("C" & 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)
         Txt = Ary(r, 1) & "|" & Ary(r, 2)
         If Not .exists(Txt) Then
            .Add Txt, r
            Nary(r, 1) = Ary(r, 12)
         Else
            Rw = .Item(Txt)
            Nary(Rw, 1) = Nary(Rw, 1) + Ary(r, 12)
         End If
      Next r
   End With
   Sheets("sheet1").Range("O2").Resize(UBound(Nary)).Value = Nary
End Sub
 
Upvote 0
Solution
How about
VBA Code:
Sub ShaikhAziz()
   Dim Ary As Variant, Nary As Variant
   Dim r As Long, Rw As Long
   Dim Txt As String
  
   With Sheets("Sheet1")
      Ary = .Range("C2:N" & .Range("C" & 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)
         Txt = Ary(r, 1) & "|" & Ary(r, 2)
         If Not .exists(Txt) Then
            .Add Txt, r
            Nary(r, 1) = Ary(r, 12)
         Else
            Rw = .Item(Txt)
            Nary(Rw, 1) = Nary(Rw, 1) + Ary(r, 12)
         End If
      Next r
   End With
   Sheets("sheet1").Range("O2").Resize(UBound(Nary)).Value = Nary
End Sub
Thankyou so much fluff,

Next time i will keep remember below 3 things,
1. i have to post exact columns
2. do not cross posting
3. i will asking only this board and you.

one day i wish i could be a coder like you...thankyou so much co-operating with me after all my stupid activities.

Have a good day sir !!! :) ?
 
Upvote 0
Glad to help & thanks for the feedback.
Dear Fluff, Hiii, Good morning,
Hope you doing well,

Below is one query, how to substract merged cells from ColumnO - ColumnK and result would be in ColumnP.

ColumnKColumnOColumnP
2567
180200
120150
150170

and result should be come in columnP as merged,

Thankyou in advance for help, :)
 
Upvote 0
I do not work with merged cells as they are an abomination.
You will need to start a new thread.
 
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