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!
 
Hi, sorry for that, may be by chance, " key" declaration was deleted
try to add
VBA Code:
Dim key
That fixed it! Now I ran into a Run-time error '9' : Subscript out of range for the line,
VBA Code:
arr(k, i * 5 -1) = rng(s(i), 4) & "/" & rng(s(i), 3) & "/" & rng(s(i), 1)
Is this due to my month being fully caps in my data? I just realized that the sample data I provided have the month uncapitalized.
 
Upvote 0

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
That fixed it! Now I ran into a Run-time error '9' : Subscript out of range for the line,
VBA Code:
arr(k, i * 5 -1) = rng(s(i), 4) & "/" & rng(s(i), 3) & "/" & rng(s(i), 1)
Is this due to my month being fully caps in my data? I just realized that the sample data I provided have the month uncapitalized.
I don't think so.
I wished you could post a sheet screenshot of dummy data
(or attach it via google drive, may be? remember to delete sensitive data)
 
Upvote 0
I don't think so.
I wished you could post a sheet screenshot of dummy data
(or attach it via google drive, may be? remember to delete sensitive data)
Hi, I created more data I hope this helps. The other columns are redundant that's why I did not include it in my dummy data. My bad if that is the reason that caused the issue.
Test.xlsx
ABCDEFGHIJKLMNO
1YearWeekMonthDateStackPartTeamTeam No.Data 1TypeDescribeData 2ActionNumberNew Number
219991JAN64012343CU-1AVBLION2ACPOWERJIN1234569875
319992JAN7401828300-9-000TIGER2BCELECTRICKOUT78907865
419992FEB84013233CU12345MOUSE2CCBATTERYLOUT78907865
519992FEB15401828300-AAA-123CAT2XCPOWERMIN1234575321
619992MAR124018143145OASPCAR1ECENERGYNOUT1234569875
720001JAN14018213-ACU-456LION1FCBATTERYOIN78907865
820001APR2401712RBU-AAA-010HOUSE3GCPOWERPOUT78907865
920003MAY1740134531455OALAND2BCELECTRICQOUT1234575321
1020004MAY224018885679O0ACAT3ICENERGYROUT1234575321
1120001JUN1401001828146DCAT1XCBATTERYZOUT1234575321
1220002JUN9401222488934ARULION3YCPOWERHIIN3628023722
1320011JAN3401999ZXU-A-345CAR1YUCELECTRICRUOUT7303256556
Sheet1
 
Upvote 0
It still works for me. Its so weird!
I wonder, with this line:
Code:
lr = Cells(Rows.count, "A").End(xlUp).Row
witl lr (last row) is last row of column A, sheet 1
Is there any redundant data in column A sheet1, from end of table downwards?
 
Upvote 0
It still works for me. Its so weird!
I wonder, with this line:
Code:
lr = Cells(Rows.count, "A").End(xlUp).Row
witl lr (last row) is last row of column A, sheet 1
Is there any redundant data in column A sheet1, from end of table downwards?
Nope there is no redundant data in that column but I have many hidden columns in-between if that affects anything?
 
Upvote 0
Nope there is no redundant data in that column but I have many hidden columns in-between if that affects anything?
The code works for column A-N only. Other columns, did not effect to the results.
Try to attach sample file via google drive (with un-sensitive data).
 
Upvote 0
The code works for column A-N only. Other columns, did not effect to the results.
Try to attach sample file via google drive (with un-sensitive data).
Okay I will attach a sample via gdrive. Will let you know tomorrow! Thanks!
 
Upvote 0
Correct it:
Old:
Code:
r = r & "," & cell.Row - 1
New:
Code:
r = dic(cell.Value) & "," & cell.Row - 1

Now it should works:
VBA Code:
Option Explicit
Sub FindCondemn()
Dim lr&, i&, j&, k&, max&, count&, key
Dim rng, s, r As String, cell As Range, dic As Object, arr()
Set dic = CreateObject("Scripting.dictionary")
Worksheets("Daily Reports").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), "Changed Out", Range("N2:N" & lr), cell.Value) 'count "Changed Out"'s of specific number
        If count > max Then max = count ' get the maximum "Changed Out" within numbers
        If cell.Offset(, -1).Value = "Changed Out" And count > 2 Then 'loop through each number with "Changed Out">=3, then write "Serial No." 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 = dic(cell.Value) & "," & 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("Eqp Changeout Tracking").Activate
    Range("A3:B3").Value = Array("No.", "Serial No.")
    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", "Fault Symtom of Train / Component", "Train No.")
                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
 
Upvote 0
Solution
Correct it:
Old:
Code:
r = r & "," & cell.Row - 1
New:
Code:
r = dic(cell.Value) & "," & cell.Row - 1

Now it should works:
VBA Code:
Option Explicit
Sub FindCondemn()
Dim lr&, i&, j&, k&, max&, count&, key
Dim rng, s, r As String, cell As Range, dic As Object, arr()
Set dic = CreateObject("Scripting.dictionary")
Worksheets("Daily Reports").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), "Changed Out", Range("N2:N" & lr), cell.Value) 'count "Changed Out"'s of specific number
        If count > max Then max = count ' get the maximum "Changed Out" within numbers
        If cell.Offset(, -1).Value = "Changed Out" And count > 2 Then 'loop through each number with "Changed Out">=3, then write "Serial No." 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 = dic(cell.Value) & "," & 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("Eqp Changeout Tracking").Activate
    Range("A3:B3").Value = Array("No.", "Serial No.")
    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", "Fault Symtom of Train / Component", "Train No.")
                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
Thanks so much for your help! I really appreciate this!
 
Upvote 0

Forum statistics

Threads
1,224,853
Messages
6,181,410
Members
453,037
Latest member
krim

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