Sumif in macro

boobirajt

New Member
Joined
Sep 7, 2014
Messages
3
Hi all

Iam new to macro. i need a help in sumif function. my input is table one

table 1
SnoNameqty
1aa10
2bb50
3aa60
4cc30
5aa10
6cc20
7cc50
8dd20

<tbody>
</tbody>
the out put i need is like this

table 2
SnoNameqty
1aa80
2bb50
3cc100
4dd20

<tbody>
</tbody>

i can do this in formule. but i need in VBA. pls help.
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
This code picks up on row 2 ("A2") value "SNo".
Results start F1.
Code:
[COLOR="Navy"]Sub[/COLOR] MG07Sep38
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Offset(, 1).Value) [COLOR="Navy"]Then[/COLOR]
        n = n + 1
        Rows(n).Range("F1:H1").Value = Dn.Resize(, 3).Value
        Range("F" & n) = IIf(n > 1, n - 1, Dn.Value)
        .Add Dn.Offset(, 1).Value, n
    [COLOR="Navy"]Else[/COLOR]
        Range("H" & .Item(Dn.Offset(, 1).Value)) = Range("H" & .Item(Dn.Offset(, 1).Value)) + Dn.Offset(, 2).Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
This code picks up on row 2 ("A2") value "SNo".
Results start F1.
Code:
[COLOR=Navy]Sub[/COLOR] MG07Sep38
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
[COLOR=Navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
    [COLOR=Navy]If[/COLOR] Not .Exists(Dn.Offset(, 1).Value) [COLOR=Navy]Then[/COLOR]
        n = n + 1
        Rows(n).Range("F1:H1").Value = Dn.Resize(, 3).Value
        Range("F" & n) = IIf(n > 1, n - 1, Dn.Value)
        .Add Dn.Offset(, 1).Value, n
    [COLOR=Navy]Else[/COLOR]
        Range("H" & .Item(Dn.Offset(, 1).Value)) = Range("H" & .Item(Dn.Offset(, 1).Value)) + Dn.Offset(, 2).Value
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


wow this what i am looking for and works great. Thankyou for the Help sir. Additionally i need further information.
table 1
snonameid noqty
1aa125610
2bb158950
3aa125660
4cc147830
5aa125610
6cc147820
7cc147850
8dd114520

<tbody>
</tbody>

in the above table. each name has unique ID NO, which i ant in the result as like below

table 2
snonameid noqty
1aa125680
2bb158950
3cc1478100
4dd114520

<tbody>
</tbody>

And please Note that the result is start from F1 Cell but I need to start in F3. which is in row 3.
 
Upvote 0
Try this:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG07Sep32
[COLOR="Navy"]Dim[/COLOR] Rng [COLOR="Navy"]As[/COLOR] Range, Dn [COLOR="Navy"]As[/COLOR] Range, n [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Long[/COLOR]
[COLOR="Navy"]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
n = 2
[COLOR="Navy"]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR="Navy"]For[/COLOR] [COLOR="Navy"]Each[/COLOR] Dn [COLOR="Navy"]In[/COLOR] Rng
    [COLOR="Navy"]If[/COLOR] Not .Exists(Dn.Offset(, 1).Value) [COLOR="Navy"]Then[/COLOR]
        n = n + 1
        Rows(n).Range("F1:I1").Value = Dn.Resize(, 4).Value
        Range("F" & n) = IIf(n > 3, n - 3, Dn.Value)
        .Add Dn.Offset(, 1).Value, n
    [COLOR="Navy"]Else[/COLOR]
        Range("I" & .Item(Dn.Offset(, 1).Value)) = Range("I" & .Item(Dn.Offset(, 1).Value)) + Dn.Offset(, 3).Value
    [COLOR="Navy"]End[/COLOR] If
[COLOR="Navy"]Next[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]With[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Try this:-
Code:
[COLOR=Navy]Sub[/COLOR] MG07Sep32
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long[/COLOR]
[COLOR=Navy]Set[/COLOR] Rng = Range(Range("A2"), Range("A" & Rows.Count).End(xlUp))
n = 2
[COLOR=Navy]With[/COLOR] CreateObject("scripting.dictionary")
.CompareMode = vbTextCompare
[COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Rng
    [COLOR=Navy]If[/COLOR] Not .Exists(Dn.Offset(, 1).Value) [COLOR=Navy]Then[/COLOR]
        n = n + 1
        Rows(n).Range("F1:I1").Value = Dn.Resize(, 4).Value
        Range("F" & n) = IIf(n > 3, n - 3, Dn.Value)
        .Add Dn.Offset(, 1).Value, n
    [COLOR=Navy]Else[/COLOR]
        Range("I" & .Item(Dn.Offset(, 1).Value)) = Range("I" & .Item(Dn.Offset(, 1).Value)) + Dn.Offset(, 3).Value
    [COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Next[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]With[/COLOR]
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick


wow you r super mick. thanks for your help. Then i need another help, can you explain me code you have given to me. because in future i will modify for my requirements.

Regards,
Boopathiraj
 
Upvote 0

Forum statistics

Threads
1,214,601
Messages
6,120,465
Members
448,965
Latest member
grijken

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