help to correct error code duplicate data

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,429
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi, gays
i have data in sheet1 i would transfer data just non duplicated to sheet 2 i try with this code but show me error " object required"
and this is my code

Code:
Public Sub SumDuplicateData()


Dim Srch As Range
Sheet2.Range("A2:G100").Value = Empty

For I = 2 To Sheet1.Range("F100").End(xlUp).Row
Set Srch = Sheet2.Range("F2:F100").Find(Sheet1.Range("F" & I).Value)
If Srch Is Nothing Then
For j = 1 To 6
Sheet2.Range(Cells(Sheet2.Range("F100").End(xlUp).Offset(1, 0).Row, j).Address).Value = Sheet1.Range(Cells(I, j).Address).Value
Next j
Sheet2.Range("G" & Sheet2.Range("F100").End(xlUp).Row).Value = Sheet2.Range("F100").End(xlUp).Row - 1
Else
For j = 1 To 3
Sheet2.Range(Cells(Srch.Row, j).Address).Value = Sheet1.Range(Cells(I, j).Address).Value + Sheet2.Range(Cells(Srch.Row, j).Address).Value
Next j
End If
Next I


End Sub
 
Last edited by a moderator:
Try the following macro. It works according to the example you set, where the data on sheet1 starts in cell A1.

SHEET1:
a b c d e f g
ITEM BRAND TYPE ORIGIN IMPORT EXPORT BALANCE
1 1200R20 G580 THI 200 50 150

Code:
Sub SumDuplicates()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim r As Range, f As Range, cell As String
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    For i = 2 To sh1.Range("B" & Rows.Count).End(xlUp).Row
        Set r = sh2.Range("B2", sh2.Range("B" & Rows.Count).End(xlUp))
        Set f = r.Find(sh1.Cells(i, "B").Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            cell = f.Address
            Do
                If sh2.Cells(f.Row, "C").Value = sh1.Cells(i, "C").Value And _
                   sh2.Cells(f.Row, "D").Value = sh1.Cells(i, "D").Value Then
                    sh2.Cells(f.Row, "E").Value = sh2.Cells(f.Row, "E").Value + sh1.Cells(i, "E").Value
                    sh2.Cells(f.Row, "F").Value = sh2.Cells(f.Row, "F").Value + sh1.Cells(i, "F").Value
                    sh2.Cells(f.Row, "G").Value = sh2.Cells(f.Row, "G").Value + sh1.Cells(i, "G").Value
                    Exit Do
                End If
                Set f = r.FindNext(f)
            Loop While Not f Is Nothing And f.Address <> cell
        Else
            sh2.Range("A" & Rows.Count).End(xlUp)(2).Resize(1, 7).Value = sh1.Range("A" & i).Resize(1, 7).Value
        End If
    Next
    MsgBox "Done"
End Sub
 
Upvote 0

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Try the following macro. It works according to the example you set, where the data on sheet1 starts in cell A1.



Code:
Sub SumDuplicates()
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim r As Range, f As Range, cell As String
    
    Set sh1 = Sheets("Sheet1")
    Set sh2 = Sheets("Sheet2")
    For i = 2 To sh1.Range("B" & Rows.Count).End(xlUp).Row
        Set r = sh2.Range("B2", sh2.Range("B" & Rows.Count).End(xlUp))
        Set f = r.Find(sh1.Cells(i, "B").Value, LookIn:=xlValues, lookat:=xlWhole)
        If Not f Is Nothing Then
            cell = f.Address
            Do
                If sh2.Cells(f.Row, "C").Value = sh1.Cells(i, "C").Value And _
                   sh2.Cells(f.Row, "D").Value = sh1.Cells(i, "D").Value Then
                    sh2.Cells(f.Row, "E").Value = sh2.Cells(f.Row, "E").Value + sh1.Cells(i, "E").Value
                    sh2.Cells(f.Row, "F").Value = sh2.Cells(f.Row, "F").Value + sh1.Cells(i, "F").Value
                    sh2.Cells(f.Row, "G").Value = sh2.Cells(f.Row, "G").Value + sh1.Cells(i, "G").Value
                    Exit Do
                End If
                Set f = r.FindNext(f)
            Loop While Not f Is Nothing And f.Address <> cell
        Else
            sh2.Range("A" & Rows.Count).End(xlUp)(2).Resize(1, 7).Value = sh1.Range("A" & i).Resize(1, 7).Value
        End If
    Next
    MsgBox "Done"
End Sub


thank's so much DanteAmor it's perfectly works code
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

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