vba sumRange as per Criteria

Status
Not open for further replies.

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
843
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to sum scores of few playear as per Criteria List.
Column A and B are my Data.Criteria list is in Column E. List may Increase.

Is there any direct excel formula? to get the same

looking in excel formula /Dictionary/loop



NameScoreCriteria ListExpected sum
Dhoni
10642​
Sehwag
Virat
11498​
Dhoni
Rohit
10857​
Total Scores Made by Sachin and Sehwag
22424​
Sachin
12657​
Sehwag
11782​


My attempted code which is not working. getting stuck second loop.

VBA Code:
Option Explicit
Sub Sum_Range_as_per_Criteria()

    Dim i As Long, Arr_Cr As Variant
    Dim int_total As Long
    
    'Criteria Value
    Arr_Cr = WorksheetFunction.Transpose(Range("E2:E3").Value)
    
    For i = 2 To Range("A1000").End(xlUp).Row
        If IsNumeric(WorksheetFunction.Match(Cells(i, 1).Value, Arr_Cr, 0)) Then
            int_total = int_total + Cells(i, 2).Value
        End If
    
    Next i


End Sub


Thanks
mg
 

Attachments

  • SumRange.PNG
    SumRange.PNG
    9.1 KB · Views: 4

Some videos you may like

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.

alansidman

Well-known Member
Joined
Feb 26, 2007
Messages
6,069
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Maybe:

VBA Code:
Option Explicit

Sub Scores()
    Dim i As Long, j As Long, lr As Long, lrE As Long
    Dim x As Integer
    Application.ScreenUpdating = False
    x = 0
    lr = Range("A" & Rows.Count).End(xlUp).Row
    lrE = Range("E" & Rows.Count).End(xlUp).Row
    For i = 3 To lr
        For j = 3 To lrE
            If Range("A" & i) = Range("E" & j) Then
                x = x + Range("B" & i).Value2
            End If
        Next j
    Next i
    Range("G5") = x
    Application.ScreenUpdating = True
    MsgBox "Completed"

End Sub
 

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
843
Office Version
  1. 2010
Platform
  1. Windows
Hi AlanSidman,

Superb ! Thank you so much for your help, got as expected result. 🕺

is there excel built in formula to get same result.

also how to get same result via dictionary , I am trying to learn dictionary here.


Thanks
mg
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
53,259
Office Version
  1. 365
Platform
  1. Windows
Duplicate vba Dictionary - Sum ranges as per criteria

Please do not post the same question multiple times. All clarifications, follow-ups, and bumps should be posted back to the original thread.
Per forum rules, posts of a duplicate nature will be locked or deleted (rule 12 here: Forum Rules).
 
Status
Not open for further replies.

Watch MrExcel Video

Forum statistics

Threads
1,123,231
Messages
5,600,427
Members
414,384
Latest member
joehalks

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
Top