+ Reply to Thread
Results 1 to 15 of 15

Thread: Sum and delete Duplicate Rows VBA

  1. #1
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Sum and delete Duplicate Rows VBA

    I need help to adapted Leith Ross codes to suit my needs. I find it in this tread (#3):
    http://www.excelforum.com/excel-prog...o-formula.html
    It working fine but I need to preserve my spreadsheet appearance for future use.
    Below codes work with item descriptions in column A and qty in B.
    My spreadsheet got item description in column D and qty in column B.
    Is possible please, to change this codes to work with my spreadsheet?
    Thank you.
    This code to be work with Excel 2003

    Sub CreateSummaryReport()
    
      Dim Cell As Range
      Dim Data() As Variant
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim I As Long
      Dim Item As Variant
      Dim Items As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SumWks As Worksheet
      Dim Wks As Worksheet
      
        On Error Resume Next
          Set SumWks = Worksheets("Summary Report")
            If Err = 9 Then
               Err.Clear
               Worksheets.Add.Name = "Summary Report"
                 Cells(1, "A") = "Model Number"
                 Cells(1, "B") = "Quantity"
                 Rows(1).Font.Bold = True
                 Columns("A:B").AutoFit
            End If
        On Error GoTo 0
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Wks In Worksheets
            If Wks.Name <> SumWks.Name Then
               Set Rng = Wks.Range("A1")
               Set RngEnd = Rng.Cells(Rows.Count, Rng.Column).End(xlUp)
               Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                 For Each Cell In Rng
                   Key = Trim(Cell.Value)
                   Item = Cell.Offset(0, 1).Value
                   If Key <> "" Then
                     If Not DSO.Exists(Key) Then
                        DSO.Add Key, Item
                     Else
                        DSO(Key) = DSO(Key) + Item
                     End If
                   End If
                 Next Cell
            End If
          Next Wks
          
          With SumWks
            .UsedRange.Offset(1, 0).ClearContents
            Keys = DSO.Keys
            Items = DSO.Items
              For I = 0 To DSO.Count - 1
                .Cells(I + 2, "A") = Keys(I)
                .Cells(I + 2, "B") = Items(I)
              Next I
            .UsedRange.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
                            Header:=xlYes, Orientation:=xlSortColumns
          End With
        
        Set DSO = Nothing
        
    End Sub
    Attached Files Attached Files
    Marek

  2. #2
    Valued Forum Contributor mohd9876's Avatar
    Join Date
    05-04-2011
    Location
    Amman, Jordan
    MS-Off Ver
    Excel 2010
    Posts
    409

    Re: Sum and delete Duplicate Rows VBA

    change the lines in bold
    Sub CreateSummaryReport()
    
      Dim Cell As Range
      Dim Data() As Variant
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim I As Long
      Dim Item As Variant
      Dim Items As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SumWks As Worksheet
      Dim Wks As Worksheet
      
        On Error Resume Next
          Set SumWks = Worksheets("Summary Report")
            If Err = 9 Then
               Err.Clear
               Worksheets.Add.Name = "Summary Report"
                 Cells(1, "A") = "Model Number"
                 Cells(1, "B") = "Quantity"
                 Rows(1).Font.Bold = True
                 Columns("A:B").AutoFit
            End If
        On Error GoTo 0
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Wks In Worksheets
            If Wks.Name <> SumWks.Name Then
               Set Rng = Wks.Range("D1")
               Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
               Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                 For Each Cell In Rng
                   Key = Trim(Cell.Value)
                   Item = Cell.Offset(0, -2).Value
                   If Key <> "" Then
                     If Not DSO.Exists(Key) Then
                        DSO.Add Key, Item
                     Else
                        DSO(Key) = DSO(Key) + Item
                     End If
                   End If
                 Next Cell
            End If
          Next Wks
          
          With SumWks
            .UsedRange.Offset(1, 0).ClearContents
            Keys = DSO.Keys
            Items = DSO.Items
              For I = 0 To DSO.Count - 1
                .Cells(I + 2, "A") = Keys(I)
                .Cells(I + 2, "B") = Items(I)
              Next I
            .UsedRange.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
                            Header:=xlYes, Orientation:=xlSortColumns
          End With
        
        Set DSO = Nothing
        
    End Sub
    Last edited by mohd9876; 09-13-2011 at 10:17 AM.

  3. #3
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    I get error:
    "Run-time error '91':
    Object variable or With block variable not set"
    Marek

  4. #4
    Valued Forum Contributor mohd9876's Avatar
    Join Date
    05-04-2011
    Location
    Amman, Jordan
    MS-Off Ver
    Excel 2010
    Posts
    409

    Re: Sum and delete Duplicate Rows VBA

    on which line?

  5. #5
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    Debuger show bold line

    Sub CreateSummaryReport()
    
      Dim Cell As Range
      Dim Data() As Variant
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim I As Long
      Dim Item As Variant
      Dim Items As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SumWks As Worksheet
      Dim Wks As Worksheet
      
        On Error Resume Next
          Set SumWks = Worksheets("Summary Report")
            If Err = 9 Then
               Err.Clear
               Worksheets.Add.Name = "Summary Report"
                 Cells(1, "A") = "Model Number"
                 Cells(1, "B") = "Quantity"
                 Rows(1).Font.Bold = True
                 Columns("A:B").AutoFit
            End If
        On Error GoTo 0
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Wks In Worksheets
            If Wks.Name <> SumWks.Name Then
               Set Rng = Wks.Range("D1")
               Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
               Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                 For Each Cell In Rng
                   Key = Trim(Cell.Value)
                   Item = Cell.Offset(0, -2).Value
                   If Key <> "" Then
                     If Not DSO.Exists(Key) Then
                        DSO.Add Key, Item
                     Else
                        DSO(Key) = DSO(Key) + Item
                     End If
                   End If
                 Next Cell
            End If
          Next Wks
          
          With SumWks
            .UsedRange.Offset(1, 0).ClearContents
            Keys = DSO.Keys
            Items = DSO.Items
              For I = 0 To DSO.Count - 1
                .Cells(I + 2, "A") = Keys(I)
                .Cells(I + 2, "B") = Items(I)
              Next I
            .UsedRange.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
                            Header:=xlYes, Orientation:=xlSortColumns
          End With
        
        Set DSO = Nothing
        
    End Sub
    Marek

  6. #6
    Valued Forum Contributor mohd9876's Avatar
    Join Date
    05-04-2011
    Location
    Amman, Jordan
    MS-Off Ver
    Excel 2010
    Posts
    409

    Re: Sum and delete Duplicate Rows VBA

    modified lines are in bold
    
    Sub CreateSummaryReport()
    
      Dim Cell As Range
      Dim Data() As Variant
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim I As Long
      Dim Item As Variant
      Dim Items As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SumWks As Worksheet
      Dim Wks As Worksheet
      
        On Error Resume Next
          Set SumWks = Worksheets("Summary Report")
            If Err = 9 Then
               Err.Clear
               Set SumWks = Worksheets.Add.Name
               SumWks.Name = "Summary Report"
                 Cells(1, "A") = "Model Number"
                 Cells(1, "B") = "Quantity"
                 Rows(1).Font.Bold = True
                 Columns("A:B").AutoFit
            End If
        On Error GoTo 0
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Wks In Worksheets
            If Wks.Name <> SumWks.Name Then
               Set Rng = Wks.Range("D1")
               Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
               Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                 For Each Cell In Rng
                   Key = Trim(Cell.Value)
                   Item = Cell.Offset(0, -2).Value
                   If Key <> "" Then
                     If Not DSO.Exists(Key) Then
                        DSO.Add Key, Item
                     Else
                        DSO(Key) = DSO(Key) + Item
                     End If
                   End If
                 Next Cell
            End If
          Next Wks
          
          With SumWks
            .UsedRange.Offset(1, 0).ClearContents
            Keys = DSO.Keys
            Items = DSO.Items
              For I = 0 To DSO.Count - 1
                .Cells(I + 2, "A") = Keys(I)
                .Cells(I + 2, "B") = Items(I)
              Next I
            .UsedRange.Sort Key1:=.Cells(2, 1), Order1:=xlAscending, _
                            Header:=xlYes, Orientation:=xlSortColumns
          End With
        
        Set DSO = Nothing
        
    End Sub

  7. #7
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    It stop again in same spot as post #5
    Marek

  8. #8
    Valued Forum Contributor mohd9876's Avatar
    Join Date
    05-04-2011
    Location
    Amman, Jordan
    MS-Off Ver
    Excel 2010
    Posts
    409

    Re: Sum and delete Duplicate Rows VBA

    sorry.. a small mistake,, remove .Name
    Set SumWks = Worksheets.Add

  9. #9
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    Now is running OK, but work only when I select range A1 instead of D1 and cell offset set to 1,offset -2 and D1 range make mess on Summary report. What I did is add another macro that sort column than CreateSummaryReport running and another macro to resort column again. Maybe from back side but work.
    Thank you very much for help.
    Last edited by mdbdesign; 09-14-2011 at 08:03 AM.
    Marek

  10. #10
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Sum and delete Duplicate Rows VBA

    Hello mdbdesign,

    Try this modified version of the macro and let me know if there are any issues.
    Sub CreateSummaryReport()
    
     ' September 15, 2011 - Modified to work with cells D and B instead of A and B
     
      Dim Cell As Range
      Dim Data() As Variant
      Dim DSO As Object
      Dim Key As Variant
      Dim Keys As Variant
      Dim I As Long
      Dim Item As Variant
      Dim Items As Variant
      Dim Rng As Range
      Dim RngEnd As Range
      Dim SumWks As Worksheet
      Dim Wks As Worksheet
      
        On Error Resume Next
          Set SumWks = Worksheets("Summary Report")
            If Err = 9 Then
               Err.Clear
               Worksheets.Add.Name = "Summary Report"
                 Cells(1, "D") = "Model Number"
                 Cells(1, "B") = "Quantity"
                 Rows(1).Font.Bold = True
                 Columns("A:D").AutoFit
            End If
        On Error GoTo 0
        
        Set DSO = CreateObject("Scripting.Dictionary")
        DSO.CompareMode = vbTextCompare
        
          For Each Wks In Worksheets
            If Wks.Name <> SumWks.Name Then
               Set Rng = Wks.Range("D1")
               Set RngEnd = Rng.Cells(Rows.Count, Rng.Column).End(xlUp)
               Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
                 For Each Cell In Rng
                   Key = Trim(Cell.Value)               ' Cell in column "D"
                   Item = Cell.Offset(0, -3).Value      ' Cell in column "A'
                   If Key <> "" Then
                     If Not DSO.Exists(Key) Then
                        DSO.Add Key, Item
                     Else
                        DSO(Key) = DSO(Key) + Item
                     End If
                   End If
                 Next Cell
            End If
          Next Wks
          
          With SumWks
           ' Clear the sheet except for the headers
            .UsedRange.Offset(1, 0).ClearContents
            
          ' Create 1-D arrays of the Model Numbers and Quantities
            Keys = DSO.Keys
            Items = DSO.Items
            
            ' Copy the Model Numbers and Quantities to the worksheet
              For I = 0 To DSO.Count - 1
                .Cells(I + 2, "D") = Keys(I)
                .Cells(I + 2, "B") = Items(I)
              Next I
              
           ' Sort the data in ascending order by Model Number
            .UsedRange.Sort Key1:=.Cells(2, "D"), Order1:=xlAscending, _
                            Header:=xlYes, Orientation:=xlSortColumns
          End With
        
        Set DSO = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  11. #11
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    Attached is file after run macro and debugger screenshot.
    Still not working...Any magic thing prevent it run properly?
    Attached Images Attached Images
    Attached Files Attached Files
    Marek

  12. #12
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Sum and delete Duplicate Rows VBA

    Hello mdbdesign,

    The code ran on the workbook you posted but it does not look like the data matches the column headers. The error you posted looks like there is a missing reference in you VBA project.

    The code is for Excel 2003 and your profile indicates you are using Excel 2010. Are you running Windows XP, 7, or Vista?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  13. #13
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    At work I run 2003. At home I got 2010.
    All run on wXP.
    About missing references in VBA project...???
    No clue. I am simple CAD guy with little knowledge of Excel macro.
    But it work same way at work and at home, I am getting same error.
    Marek

  14. #14
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & read 2007
    Posts
    15,979

    Re: Sum and delete Duplicate Rows VBA

    Hello mdbdesign,

    How to Check the References in Your VBA Project
    1. Open the workbook in Excel and press ALT+F11 this opens the Visual Basic Editor.
    2. Press the keys ALT+T this displays the Tools menu.
    3. Press the R key. This will display the references dialog.
    4. Look in the drop down for any entries marked MISSING:.
    Let me know what you find.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  15. #15
    Registered User
    Join Date
    09-22-2010
    Location
    Courtice, Ontario, Canada
    MS-Off Ver
    MS Office 2010
    Posts
    34

    Re: Sum and delete Duplicate Rows VBA

    Not any of available references carry "missing" word.
    But It is working when A and B column is in use?!?!
    Marek

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0