Find repeating numbers then copy certain cells of same row to another sheet

xjpx

New Member
Joined
Jan 3, 2022
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi all, I am in need of some help. I am trying to loop through column 'N' to find for numbers that are repeated 3 times or more. Once found all these number, paste the numbers into Sheet 2 along with some other cells in the same rows. Below I have attached some images with more explanation to better elaborate.
Firstly, search column 'M', if it is 'OUT' data in column 'N' is taken into account if it is 'IN' data in column 'N' does not need to be counted. Next, loop through column 'N' and find numbers that are repeated more than 3 times. So for the image below, only number 7890 needs to be considered as repeating 3 times as it had 3 OUTs. Dont have to consider IN 7890, in this case 7890 appears 3 times.
1652403683543.png

Next, information from columns A, C, D, I and L also have to be copied into the other sheet, Sheet 2.
1652403969416.png

The final product is exactly as seen below in Sheet 2. Column A is just running numbers as more Numbers matches the conditions above.
1652404453528.png

Seems really confusing to me... I hope I have done a good job explaining. I have tried running some scripts but I have problems matching both conditions of OUT and >= 3 repeats.

Thanks!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Could you post a mini sheet via XL2BB : XL2BB - Excel Range to BBCode
so the helpers do not have to re type the data
?
Sheet 1
Test.xlsx
ABCDEFGHIJKLMN
1YearMonthDateData 1Data 2ActionNumber
21999Jan6AJIN123456
31999Jan7BKOUT7890
41999Feb8CLOUT7890
51999Feb15DMIN12345
61999Mar12ENOUT123456
72000Jan1FOIN7890
82000Apr2GPOUT7890
92000May17HQOUT12345
102000May22IROUT12345
Sheet1

Sheet 2
Test.xlsx
ABCDEFGHIJKLMNOP
1
2
3No. NumberDateData 2Data 1DateData 2Data1DateData 2Data1
4178907/1/1999KB8/2/1999LC2/4/2000PG
Sheet2

Wow this is amazing! So sorry I didn't know about this.
 
Upvote 0
Hello, sorry for late reply.
Alt-F11 to open VBA window, insert module, then paste below code to:

VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, max&, count&
Dim rng, s, r As String, cell As Range, dic As Object, arr()
Set dic = CreateObject("Scripting.dictionary")
Worksheets("Sheet1").Activate
lr = Cells(Rows.count, "A").End(xlUp).Row
rng = Range("A2:N" & lr).Value
    For Each cell In Range("N2:N" & lr)
        count = WorksheetFunction.CountIfs(Range("M2:M" & lr), "OUT", Range("N2:N" & lr), cell.Value) ' count "OUT"'s of specific number
        If count > max Then max = count ' get the maximum "OUT" within numbers
        If cell.Offset(, -1).Value = "OUT" And count > 2 Then 'loop through each number with "OUT">=3, then write "number" and "row number" into dictionary
            If Not dic.exists(cell.Value) Then
                k = k + 1
                r = count & "," & cell.Row - 1
                dic.Add cell.Value, r
            Else
                r = r & "," & cell.Row - 1
                dic(cell.Value) = r
            End If
        End If
    Next
    k = 0
    ReDim arr(1 To dic.count, 1 To max * 5 + 1) ' create variable array
Worksheets("Sheet2").Activate
    Range("A3:B3").Value = Array("No.", "Number")
    For Each key In dic.keys
        k = k + 1
        s = Split(dic(key), ",")
        For j = 1 To max * 5 + 1
            arr(k, 1) = k
            arr(k, 2) = key
            For i = 1 To UBound(s)
                Range(Cells(3, i * 5 - 1), Cells(3, i * 5 + 1)).Value = Array("Date", "Data 2", "Data 1")
                arr(k, i * 5 - 1) = rng(s(i), 4) & "/" & rng(s(i), 3) & "/" & rng(s(i), 1)
                arr(k, i * 5) = rng(s(i), 12)
                arr(k, i * 5 + 1) = rng(s(i), 9)
            Next
        Next
    Next
Range("A4").Resize(k, max * 5 + 1).Value = arr
End Sub
Book2
ABCDEFGHIJKLMN
1YearMonthDateData 1Data 2ActionNumber
21999Jan6AJIN123456
31999Jan7BKOUT7890
41999Feb8CLOUT7890
51999Feb15DMIN12345
61999Mar12ENOUT123456
72000Jan1FOIN7890
82000Apr2GPOUT7890
92000May17HQOUT12345
102000May22IROUT12345
112000May22IROUT12345
122000May22IROUT12345
Sheet1

Book2
ABCDEFGHIJKLMNOPQRSTU
1
2
3No.NumberDateData 2Data 1DateData 2Data 1DateData 2Data 1DateData 2Data 1
41789007/01/1999KB08/02/1999LC02/04/2000PG
521234517/05/2000QH22/05/2000RI22/05/2000RI22/05/2000RI
Sheet2

1652770920264.png
 
Upvote 0
Hello, sorry for late reply.
Alt-F11 to open VBA window, insert module, then paste below code to:

VBA Code:
Option Explicit
Sub test()
Dim lr&, i&, j&, k&, max&, count&
Dim rng, s, r As String, cell As Range, dic As Object, arr()
Set dic = CreateObject("Scripting.dictionary")
Worksheets("Sheet1").Activate
lr = Cells(Rows.count, "A").End(xlUp).Row
rng = Range("A2:N" & lr).Value
    For Each cell In Range("N2:N" & lr)
        count = WorksheetFunction.CountIfs(Range("M2:M" & lr), "OUT", Range("N2:N" & lr), cell.Value) ' count "OUT"'s of specific number
        If count > max Then max = count ' get the maximum "OUT" within numbers
        If cell.Offset(, -1).Value = "OUT" And count > 2 Then 'loop through each number with "OUT">=3, then write "number" and "row number" into dictionary
            If Not dic.exists(cell.Value) Then
                k = k + 1
                r = count & "," & cell.Row - 1
                dic.Add cell.Value, r
            Else
                r = r & "," & cell.Row - 1
                dic(cell.Value) = r
            End If
        End If
    Next
    k = 0
    ReDim arr(1 To dic.count, 1 To max * 5 + 1) ' create variable array
Worksheets("Sheet2").Activate
    Range("A3:B3").Value = Array("No.", "Number")
    For Each key In dic.keys
        k = k + 1
        s = Split(dic(key), ",")
        For j = 1 To max * 5 + 1
            arr(k, 1) = k
            arr(k, 2) = key
            For i = 1 To UBound(s)
                Range(Cells(3, i * 5 - 1), Cells(3, i * 5 + 1)).Value = Array("Date", "Data 2", "Data 1")
                arr(k, i * 5 - 1) = rng(s(i), 4) & "/" & rng(s(i), 3) & "/" & rng(s(i), 1)
                arr(k, i * 5) = rng(s(i), 12)
                arr(k, i * 5 + 1) = rng(s(i), 9)
            Next
        Next
    Next
Range("A4").Resize(k, max * 5 + 1).Value = arr
End Sub
Book2
ABCDEFGHIJKLMN
1YearMonthDateData 1Data 2ActionNumber
21999Jan6AJIN123456
31999Jan7BKOUT7890
41999Feb8CLOUT7890
51999Feb15DMIN12345
61999Mar12ENOUT123456
72000Jan1FOIN7890
82000Apr2GPOUT7890
92000May17HQOUT12345
102000May22IROUT12345
112000May22IROUT12345
122000May22IROUT12345
Sheet1

Book2
ABCDEFGHIJKLMNOPQRSTU
1
2
3No.NumberDateData 2Data 1DateData 2Data 1DateData 2Data 1DateData 2Data 1
41789007/01/1999KB08/02/1999LC02/04/2000PG
521234517/05/2000QH22/05/2000RI22/05/2000RI22/05/2000RI
Sheet2

View attachment 64828
Hi! Thanks for the response. I ran into a compile error with the variable key not defined.
VBA Code:
For Each key In dic.keys
I think the problem is that the word key gets auto capitalized... I tried to change it to uncapitalized it but it auto switched back to caps like that...
VBA Code:
For Each Key In dic.keys
 
Upvote 0
Hi, sorry for that, may be by chance, " key" declaration was deleted
try to add
VBA Code:
Dim key
 
Upvote 0

Forum statistics

Threads
1,215,084
Messages
6,123,021
Members
449,092
Latest member
ikke

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