Vba to sort the count items

I am looking for VBA code that will:

Sort by:
Site
Product Group
Product Line

Then Count by Product line so that I have one row per Product line with the count of items

As shown in the attached file.  

The data is on the consolidated sheet, I then need to end up with the count [per Product Line] on the Summary Sheet.

I hope I have explained myself well enough for an expert to understand the requirement.

Thanks
get-Count.xlsx
JagwarmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Anthony BerenguelCommented:
Does this have to be VBA? Why not just use a countif?
0
Saurabh Singh TeotiaCommented:
Jagwarman,

If i look in your example of consolidated worksheet..Row-2,3,4 are 2 unique entries just because in L Column their Day range is different..

However when you show this data in your summary you consolidate them as 1 ignoring the day range..So thats how you want to see as in when you consolidate these entries ignore the day range or its a miss it should be 2 entries in your summary sheet rather one??

Saurabh...
0
Anthony BerenguelCommented:
you must have filters on row 1 on the Summary tab in order for this to run. Give it a shot.  (Run the Get_Count() procedure)
get-Count.xlsm
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

JagwarmanAuthor Commented:
Hi Saurabh Singh Teotia

for site 123 Product Group B there are 3 W under Product Line

for site 123 Product Group C there are 2 J under Product Line

for site 123 Product Group D there are 2 K under Product Line

I am ignoring the day range
0
Anthony BerenguelCommented:
fyi, the code i provided finds matches based on site, group, and line.
0
JagwarmanAuthor Commented:
Hi Anthony Berenguel

Not sure I am understanding that. When I open your file although it says xlsm there is no macro
0
JagwarmanAuthor Commented:
Hi Anthony Berenguel

Ignore my last post
0
Saurabh Singh TeotiaCommented:
John,

You can run this code..and enclosed is your workbook post you run the code which does what you are looking for..

Sub movedata()
    Dim rng As Range, r As Range
    Dim lrow As Long, lr As Long
    Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("Consolidated")
    Set ws1 = Sheets("Summary")

    ws1.Cells.Clear

    lrow = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    ws.Range("A1:N" & lrow).Copy ws1.Range("A1")


    lr = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = ws1.Range("A1:N" & lr)

    rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 14), Header:=xlYes

    lr = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = ws1.Range("A1:N" & lr)

    With ws1

        .Sort.SortFields.Add Key:=Range("B2:B" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("E2:E" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("F2:F" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

        With .Sort
            .SetRange rng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply

        End With

    End With

    ws1.Range("O1").Value = "Value"
    ws1.Range("O2:O" & lr).Formula = "=SUMPRODUCT((Consolidated!$B$2:$B$" & lrow & "=Summary!B2)*(Consolidated!$E$2:$E$" & lrow & "=Summary!E2)*(Consolidated!$F$2:$F$" & lrow & "=Summary!F2))"
    ws1.Range("O2:O" & lr).Value = ws1.Range("O2:O" & lr).Value
    MsgBox "Done"

End Sub

Open in new window


Saurabh...
Copy-of-get-Count.xlsm
0
JagwarmanAuthor Commented:
Hi Anthony Berenguel

The only thing about that it duplicates the rows
0
Anthony BerenguelCommented:
It does? When I run the code it doesn't seem to duplicate anything. Can you provide a screen shot of what you're getting?
0
JagwarmanAuthor Commented:
Hi Anthony Berenguel

ABCD      123      123      A      B      W      D      E      F      G      H      0-5 days      GBP      X      1      3
ABCD      123      123      A      B      W      D      E      F      G      H      6-30 days      GBP      X      1      3
ABCD      123      123      A      B      W      D      E      F      G      H      6-30 days      GBP      X      1      3
ABCD      123      123      A      C      J      D      E      F      G      H      6-30 days      GBP      X      1      2
ABCD      123      123      A      C      J      D      E      F      G      H      >90 days      GBP      X      1      2

should look like:

ABCD      123      123      A      B      W      D      E      F      G      H      0-5 days      GBP      X      1      3
ABCD      123      123      A      C      J      D      E      F      G      H      6-30 days      GBP      X      1      2
0
Saurabh Singh TeotiaCommented:
Did you try the code which I provided.??
0
JagwarmanAuthor Commented:
Saurabh Singh Teotia do you ever sleep?

Yes just checking it out and I have to say, excellent. I notice it takes into account the currency column which I don't want to include. When it does this it puts out a row for each occurrence of a currency so like Anthony Berenguel solution it kind of duplicates rows.

i.e.

EFGH      845      845      A      B      K      D      E      F      G      H      31-60 days      USD      X      4
EFGH      845      845      A      B      K      D      E      F      G      H      0-5 days              EUR      X      4
EFGH      845      845      A      B      K      D      E      F      G      H      0-5 days              AUD      X      4
EFGH      845      845      A      B      L      D      E      F      G      H      31-60 days      EUR      X      3
EFGH      845      845      A      B      L      D      E      F      G      H      61-90 days      GBP      X      3
EFGH      845      845      A      B      L      D      E      F      G      H      6-30 days        USD      X      3

instead of

EFGH      845      845      A      B      K      D      E      F      G      H      31-60 days      USD      X      4
EFGH      845      845      A      B      L      D      E      F      G      H      31-60 days      EUR      X      3
0
Saurabh Singh TeotiaCommented:
Jagwarman,

That's a excellent question..Yeah i do manage to get some hours of sleep but even though that's not a priority.. Anyways use this code...

Sub movedata()
    Dim rng As Range, r As Range
    Dim lrow As Long, lr As Long
    Dim ws As Worksheet, ws1 As Worksheet

    Set ws = Sheets("Consolidated")
    Set ws1 = Sheets("Summary")

    ws1.Cells.Clear

    lrow = ws.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    ws.Range("A1:N" & lrow).Copy ws1.Range("A1")


    lr = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = ws1.Range("A1:N" & lr)

    rng.RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 14), Header:=xlYes

    lr = ws1.Cells(Cells.Rows.Count, "A").End(xlUp).Row

    Set rng = ws1.Range("A1:N" & lr)

    With ws1

        .Sort.SortFields.Add Key:=Range("B2:B" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("E2:E" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .Sort.SortFields.Add Key:=Range("F2:F" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal

        With .Sort
            .SetRange rng
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply

        End With

    End With

    ws1.Range("O1").Value = "Value"
    ws1.Range("O2:O" & lr).Formula = "=SUMPRODUCT((Consolidated!$B$2:$B$" & lrow & "=Summary!B2)*(Consolidated!$E$2:$E$" & lrow & "=Summary!E2)*(Consolidated!$F$2:$F$" & lrow & "=Summary!F2))"
    ws1.Range("O2:O" & lr).Value = ws1.Range("O2:O" & lr).Value
    MsgBox "Done"

End Sub

Open in new window


Your workbook...

Saurabh...
Copy-of-get-Count.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JagwarmanAuthor Commented:
Saurabh Singh Teotia that is great many thanks. I'm going to give Anthony Berenguel some points as well because I also like his solution
0
JagwarmanAuthor Commented:
Excellent Experts very helpful
0
Saurabh Singh TeotiaCommented:
You are welcome and always happy to help..:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.