Using VBA to find duplicate or non-duplicate rows (common or non-common) in sheet 1 and move to sheet 2 in a row

bahrami

New Member
Joined
Feb 26, 2022
Messages
14
Office Version
  1. 2007
Platform
  1. Windows
Hello, please check my file if possible. Thank you for your efforts.
Example file-01.xlsm
ABCDEFGHI
1RowNational Code namelast nameUnion nameBoard codeBoard codeSide Election date
21181-842934-9SasanBahramiTools Unionfirst round1First Vice President1391/03/27
32181-842934-9SasanBahramiTools Union second period2Second Vice President1395/02/26
43181-842934-9SasanBahramiTools Union second period4Secretary1399/02/26
54181-987654-3AmirBahramiElectric Trade UnionFourth period6Inspector1400/02/30
Sheet1


Example file-01.xlsm
ABCDEFGHIJKLMN
1RowNational Codenamelast nameUnion nameSide of the first periodDate of the first periodSide of the second periodDate of the first periodSide of the ThirdperiodDate of the ThirdperiodSide of the Fourth periodDate of the Fourth periodNumber of company courses in the board of directors
21181-842934-9SasanBahramiTools UnionFirst Vice President1391/03/27Second Vice President1395/02/26Secretary1399/02/26003
32181-987654-3AmirBahramiElectric Trade Union000000Inspector1400/02/301
4
Sheet2
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
VBA Code:
Option Explicit
Sub test()
Dim Lr&, k&, i&, cell As Range, s As String, key
Dim dic As Object
Set dic = CreateObject("Scripting.dictionary")
With Worksheets("Sheet1")
    Lr = .Cells(Rows.Count, "B").End(xlUp).Row
    For Each cell In .Range("B2:B" & Lr)
        If Not dic.exists(cell.Value) Then
            s = cell.Offset(0, 1) & "|" & cell.Offset(0, 2) & "|" & cell.Offset(0, 3) & "|" & cell.Offset(0, 6) & "|" & cell.Offset(0, 7)
            dic.Add cell.Value, s
        Else
            s = s & "|" & cell.Offset(0, 6) & "|" & cell.Offset(0, 7)
            dic(cell.Value) = s
        End If
    Next
End With
With Worksheets("Sheet2")
    .Range("A1:G1").Value = Array("Row", "National Code", "Name", "Last name", "Union name", "Side Election Date", "Election Date")
    For Each key In dic.keys
        k = k + 1
        .Cells(k + 1, 1).Value = k
        .Cells(k + 1, 2).Value = key
        For i = 0 To UBound(Split(dic(key), "|"))
            If i >= 5 Then
                If i Mod 2 > 0 Then
                    .Cells(1, i + 3).Value = "Side Election Date"
                Else
                    .Cells(1, i + 3).Value = "Election Date"
                End If
            End If
            .Cells(k + 1, i + 3).Value = Split(dic(key), "|")(i)
        Next
    Next
End With
End Sub
Before code running:
Book1
ABCDEFGHI
1RowNational Code namelast nameUnion nameBoard codeBoard codeSide Election date
211818429349SasanBahramiTools Unionfirst round1First Vice President1391/03/27
321818429349SasanBahramiTools Union second period2Second Vice President1395/02/26
431818429349SasanBahramiTools Union second period4Secretary1399/02/26
541819876543AmirBahramiElectric Trade UnionFourth period6Inspector1400/02/30
Sheet1


After running code:
Book1
ABCDEFGHIJK
1RowNational CodeNameLast nameUnion nameSide Election DateElection DateSide Election DateElection DateSide Election DateElection Date
211818429349SasanBahramiTools UnionFirst Vice President1391/03/27Second Vice President1395/02/26Secretary1399/02/26
321819876543AmirBahramiElectric Trade UnionInspector1400/02/30
Sheet2
 
Upvote 0
Hello for your efforts, thank you for this

If the values of the election period are replaced in the specified columns and the number of participants in the elections is added in the last column. It will be great. Thank you again
 
Upvote 0
Hello, considering that the national code is transferred non-duplicate in Sheet 2, but the names (first and last name) are duplicated in Sheet 2.Thank you for your help
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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