+ Reply to Thread
Results 1 to 7 of 7

count the nos and sum the same no and create summry based on datash pp.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-12-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    134

    count the nos and sum the same no and create summry based on datash pp.

    hi
    i have this module but it too slow is there way to do it speedily do this .

    Sub count_nsum()
    
        Dim ws3 As Worksheet, ws2 As Worksheet
        Dim dic As Object, w, y
        Dim A, i, ii As Long
        Set dic = CreateObject("Scripting.Dictionary")
        Set ws3 = Sheets("pp")    ' alter if needed
        With ws3.Range("G5:G500").CurrentRegion
            A = .Value
        End With
        For i = LBound(A, 1) To UBound(A, 1)
            If Not IsEmpty(A(i, 6)) Then
                If Not dic.exists(A(i, 6)) Then
                    ReDim w(6 To 7)    'check instance in column F and sum column G
                    For ii = 6 To 7
                        w(ii) = A(i, ii)
                    Next
                    dic.Add A(i, 6), w
                Else
                    w = dic(A(i, 6)): w(7) = Val(w(7)) + Val(A(i, 7))
                    dic(A(i, 6)) = w
                End If
            End If
        Next
        y = dic.items: Set dic = Nothing
        On Error Resume Next
        Set ws2 = Sheets("Summary")
        If ws2 Is Nothing Then
            Set ws2 = Sheets.Add
            ws2.Name = ("Summary")
        End If
        On Error GoTo 0
        With ws2.Range("a1")
            .CurrentRegion.ClearContents
            With .Range("a1")
                For i = LBound(y) To UBound(y)
                    .Offset(i).Resize(, UBound(y(i))) = y(i)
                Next
            End With
        End With
        Dim ws1 As Worksheet
        Set ws1 = Nothing: Set ws2 = Nothing
        Erase A, y, w
        Sheets("Summary").Select
        [C:H].ClearContents
        [A1:B2].ClearContents
        Columns("A:B").Select
        Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
                       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                       DataOption1:=xlSortNormal
        Sheets("pp").Select
        ' # code of CountNos_2 data
        Dim RNG1 As Range, aA, B, c1(), i2 As Long, ii2 As Long, myNum As Double, N As Long
        Set RNG1 = Range("F:F")    'Application.InputBox("Select Per Day column F data range", Type:=8)
        If RNG1 Is Nothing Then Exit Sub
        aA = RNG1.Value: Set Rng = Nothing
        Set RNG1 = Range("G:G")    'Application.InputBox("Select Rm. Rate column G data range", Type:=8)
        If RNG1 Is Nothing Then Exit Sub
        B = RNG1.Resize(UBound(aA, 1), 4).Value
        myNum = 60    'Application.InputBox("Enter the Rate to be less then to count usually 50", Type:=1)
        ReDim c1(1 To UBound(aA, 1), 1 To 5)
        With CreateObject("Scripting.Dictionary")
            .CompareMode = vbTextCompare
            For i2 = 1 To UBound(aA, 1)
                If Not .exists(aA(i2, 1)) Then
                    N = N + 1: c1(N, 1) = aA(i2, 1): .Item(aA(i2, 1)) = N
                End If
                For ii2 = 1 To 4
                    If (B(i2, ii2) >= 0) * (B(i2, ii2) < myNum) Then    'use this for num range >0 to input num
                        c1(.Item(aA(i2, 1)), ii2 + 1) = c1(.Item(aA(i2, 1)), ii2 + 1) + 1
                    End If
                Next
            Next
        End With
        With Sheets("Summary").Cells(3)    'starting column nos on respected sheet name
            ' #       2 for 2 column data 3 for 3 column data
            .Resize(, 2).Value = Array("Per Day Rate", "Rate less then" & myNum)    ', _
                                                                                    '"Score2_less_" & myNum, "Score3_less_" & myNum, "Score4_less_" & myNum)
            With .Offset(1).Resize(N, 2)
                .Value = c1
                On Error Resume Next
                .SpecialCells(4).Value = 0
            End With
        End With
    
    
        Sheets("Summary").Select
        [C1:D2].ClearContents
        [C:D].Activate
        Selection.Sort Key1:=Range("C1"), Order1:=xlAscending, Header:=xlGuess, _
                       OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
                       DataOption1:=xlSortNormal
        [E1].Activate
        Set r = Nothing
        Application.ScreenUpdating = True
    
    End Sub
    also here is the sample wb
    Attached Files Attached Files
    Last edited by jay11; 07-27-2015 at 11:53 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: count the nos and sum the same no and create summry based on datash pp.

    Hi,

    Can you explain in a narrative with reference to your pp data how you are summarising it. i.e. what rules are you using.

    I can't help thinking that standard Excel functionality might achieve the task in a much quicker way.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    02-12-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    134

    Re: count the nos and sum the same no and create summry based on datash pp.

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Can you explain in a narrative with reference to your pp data how you are summarising it. i.e. what rules are you using.

    I can't help thinking that standard Excel functionality might achieve the task in a much quicker way.
    hi Richard Buttrey
    in sh "pp" code should look in Column F find 1st instance says value 25.00 ,then in same column find next instance of value 25.00 count that instance and also sum that same ,then goto next value say 30.00 find value instance in column F and count and sum
    so the summery sheet should look like following manner
    value| sum|nos of occurrence |
    25 |125 |5
    30 |240 |8
    50 |100 |2
    .
    .
    .
    so on all value in column G
    hope i explain it
    thanks
    Last edited by jay11; 07-27-2015 at 01:46 PM.

  4. #4
    Forum Contributor
    Join Date
    02-12-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    134

    Re: count the nos and sum the same no and create summry based on datash pp.

    i found this code in forum and it some what work but the sorting the number in summary sh
    not working any suggestion or any better solution are most appreciated .
    thanks.

    Sub C_Sum()
        Dim LastRow1 As Integer, LastRow2 As Integer, i As Integer
        Dim Cnt1 As Integer, Cnt2 As Integer
        Dim ws2 As Worksheet, ws3
    
        Set ws2 = Sheets("rr")    ' alter if needed
    
        On Error Resume Next
        Set ws3 = Sheets("Summary")
        If ws3 Is Nothing Then
            Set ws3 = Sheets.Add
            ws3.Name = ("Summary")
        End If
    
        'get lastrow of sheet1
        LastRow1 = ws2.Cells(Rows.count, "F").End(xlUp).Row
        For i = 1 To LastRow1
            'get lastrow of sheet2
            LastRow2 = ws3.Cells(Rows.count, "A").End(xlUp).Row
            'count duplicate in sheet1
            Cnt1 = Application.WorksheetFunction.CountIf _
                   (ws2.Range("F5:F" & LastRow1), ws2.Cells(i, "F"))
            'count duplicate in sheet2
            ' Cnt2 = Application.WorksheetFunction.CountIf _
              (ws3.Range("A1:A" & LastRow2), ws2.Cells(i, "A"))
            If Cnt1 > 1 Then    'And Cnt2 = 0 Then 'sheet1 dups > 1 and sheet2 dups = 0
                ws3.Cells(LastRow2 + 1, "A") = ws2.Cells(i, "F")
                ws3.Cells(LastRow2 + 1, "B") = Cnt1
                ws3.Cells(LastRow2 + 1, "C") = ws2.Cells(i, "F") * Cnt1
            End If
        Next
        ws3.Range("a1") = "per day"
        ws3.Range("B1") = "# of time"
        ws3.Range("C1") = "total"
    
        Dim x As Long
        Dim LastRow As Long
    
        LastRow = ws3.Cells(Rows.count, "A").End(xlUp).Row
    
        For x = LastRow To 1 Step -1
            If Application.WorksheetFunction.CountIf(Range("A1:A" & x), Range("A" & x).Text) > 1 Then
                Range("A" & x).EntireRow.Delete
            End If
        Next x
    
        With ws3.Range("A2:C" & LastRow1)
            .Sort.SetRange
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
    
    End Sub

  5. #5
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: count the nos and sum the same no and create summry based on datash pp.

    Hi,

    I don't see values for 25 & 30 (A1&A2 in Summary)

    Neither do I understand where values like 161.45 (B3) come from. I see that there are 5 occurencies of 37.14 (A3) but 5 x 37.14 = 185.7.
    Please explain the calculations in column B

  6. #6
    Forum Contributor
    Join Date
    02-12-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    134

    Re: count the nos and sum the same no and create summry based on datash pp.

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    I don't see values for 25 & 30 (A1&A2 in Summary)

    Neither do I understand where values like 161.45 (B3) come from. I see that there are 5 occurrences of 37.14 (A3) but 5 x 37.14 = 185.7.
    Please explain the calculations in column B
    25 and 30 was just an example i was explaining how the number could came.
    and i know column B need to have total but i can not code accordingly so i just multiply both nos. hear i attache new workbook u can find what i need to achieve by macro .i copy past two diff macro code from search,i do not know how to sort that from A2 to last row of sh"summary" so i did manually record macro and try to incorporate in to code.i know it's not perfect solution but that only i can came up with my proficiency of VBA.
    PHP Code: 
    per day    # of time    total
    0        52            0
    37.14       5            185.7
    37.15        2            74.3
    41.13        6            246.78
    41.14        13            534.82
    .
    .
    .

    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    02-12-2010
    Location
    india
    MS-Off Ver
    Excel 2007
    Posts
    134

    Re: count the nos and sum the same no and create summry based on datash pp.

    hi Richard Buttrey
    in sh "pp" code should look in Column f find 1st instance says value 25.00 ,then in same column find next instance of value 25.00 count that instance and also sum that same ,then goto next value say 30.00 find value instance in column F and count and sum
    so the summery sheet should look like following manner
    value| sum|nos of occurrence |
    25 |125 |5
    30 |240 |8
    50 |100 |2
    .
    .
    .
    so on all value in column G
    hope i explain it
    thanks
    Last edited by jay11; 07-27-2015 at 01:46 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2014, 06:35 PM
  2. Create a new column based on count of two other columns
    By ftcnt in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-22-2012, 09:10 AM
  3. Replies: 6
    Last Post: 10-19-2012, 04:55 PM
  4. summry sheet result regarding area and tier
    By cu525 in forum Excel General
    Replies: 2
    Last Post: 11-05-2008, 04:27 PM
  5. [SOLVED] Count unique values and create list based on these values
    By Alan Beban in forum Excel Formulas & Functions
    Replies: 42
    Last Post: 09-06-2005, 07:05 PM
  6. [SOLVED] Count unique values and create list based on these values
    By vipa2000 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 10:05 AM
  7. [SOLVED] Count unique values and create list based on these values
    By vipa2000 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-05-2005, 10:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1