Repeating Numbers in Tabled Data

cotech_10

Board Regular
Joined
Sep 11, 2010
Messages
135
Office Version
  1. 2016
Platform
  1. Windows
Hi All,


I have created a Table that has 11 Columns and 18 Rows, contained in this Table is number data in each cell. What I would like to do is to create a macro that can calculate
and display the number of repeated numbers in any given grouping of lines and produce the results to the right of the Tabled Data as per Attachment 1.

The macro would require input for any line combinations

I have given (3) such examples to the right of this Table:

1. In this first example I have selected Lines 1 to 3 of this table for analysis.

2. In the second example I have selected Lines 3 to 7 for analysis

3. In the third example I have selected two non consecutive lines being
Lines 8 and 17.


As mentioned the "output data" can be written to the right of the Table as per the attachment with automatic calculations on the following:



1. "No of Repeating Numbers" = This total is simply the total of all numbers appearing in the given line

So for this analysis there are 9 Numbers that have repeated in the line grouping "Lines 1 to 3"

2. "No of offset numbers" = This is the total of the repeated numbers in this line that appear in the "Offsets" Line at E2..K2

If we refer to Lines 1 to 3 analysis out of the 9 repeating numbers there are (4)numbers that appear in the "Offset" range located at E2..K2. they are 45,100,103 & 108

These numbers can be colour coded as per the attachment output.



I look forward in hearing back from someone soon.



Kind Regards
 

Attachments

  • Remove Duplicates.JPG
    Remove Duplicates.JPG
    208.3 KB · Views: 12

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
I look forward in hearing back from someone soon.
In most cases it would be sooner if you provided your sample data with XL2BB to make it easier for helpers by not having to manually type out sample data to test with. ;)

I also suggest that you update your Account details (click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

See if this is any use.

VBA Code:
Sub Repeats()
  Dim d1 As Object, d2 As Object
  Dim a As Variant, b As Variant, bits As Variant, rws As Variant, cols As Variant, ky As Variant
  Dim r As Range
  Dim i As Long, j As Long, k As Long
  
  Set d1 = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  For Each r In Range("E2:K2")
    d2(r.Value) = 1
  Next r
  a = Range("B7", Range("B7").End(xlToRight).End(xlDown)).Value
  cols = Application.Transpose(Application.Transpose(Evaluate("row(1:" & UBound(a, 2) & ")")))
  For Each r In Range("N7", Range("N" & Rows.Count).End(xlUp))
    d1.RemoveAll
    ReDim b(1 To 1, 1 To UBound(a, 2))
    ReDim rws(0 To 0)
    k = 0
    bits = Split(r.Value, ",")
    For i = 0 To UBound(bits)
      For j = Split(bits(i) & "-" & bits(i), "-")(0) To Split(bits(i) & "-" & bits(i), "-")(1)
        ReDim Preserve rws(1 To UBound(rws) + 1)
        rws(UBound(rws)) = j
      Next j
    Next i
    b = Application.Index(a, rws, cols)
    For i = 1 To UBound(b)
      For j = 1 To UBound(b, 2)
        d1(b(i, j)) = d1(b(i, j)) + 1
      Next j
    Next i
    For Each ky In d1.keys
      If d1(ky) = 1 Then
        d1.Remove ky
      ElseIf d2.exists(ky) Then
        d1(ky & "|") = 1
        d1.Remove ky
        k = k + 1
      End If
    Next ky
    If d1.Count > 0 Then
      With r.Offset(, 4).Resize(, d1.Count)
        .Value = d1.keys
        If k > 0 Then
          .SpecialCells(xlConstants, xlTextValues).Interior.Color = vbYellow
          .Replace What:="|", Replacement:="", LookAt:=xlPart
        End If
        .Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo, Orientation:=xlLeftToRight
      End With
    End If
  Next r
End Sub


My sample data and results. Note format of data in column N.

cotech_10 2020-08-12 1.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
255542850237116
3
4
5
6Lines
799345041691842548154431-3235468
851236249688837302915533-78121928364043717480
923583687481968746178178,189095
108021957879714612121810-11
115715356224774754090742,5-8,12,158111928374041434950515364717490
1296371243402898806942
13732836439413643511198
144395544997903840285014
15529656916644697757050
161213141516171819202122
172324252627282930313233
181751411128648550198937
193377224524876138398659
20935233862626183517484
21287041817444841347131
228016599525941298636090
23572586844762538405327
24582188912659023958444
25
Sheet1
 
Upvote 0
Hi Peter,

Thank you for your quick response... it is greatly appreciated. I have installed the add-in "XL2BB"as suggested.

I have attempted to run the code and encountered a run-time error as per attachment.

Look forward in hearing back from you..


Thanks and Regards
 

Attachments

  • runtime_error_repeats.JPG
    runtime_error_repeats.JPG
    99.1 KB · Views: 3
Upvote 0
encountered a run-time error as per attachment.
It is good that you showed what line the error occurred on but always best to also give the whole error message.

Do you have an entry something like one of these in column N? That is, a comma or dash without a number following it?
4-
3--7
6,


If still unresolved, could you post with XL2BB the sample data that you were using when the error occurred?
 
Upvote 0
Hi Peter,

Thanks for your reply... I am sending you the sample data that I had created for this analysis.. as follows:

PMacros.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMANAOAPAQARASATAU
1
2Offsets:3401036310845392100
3
4
5Col 1Col 2Col 3Col 4Col 5Col 6Col 7Col 8Col 9Col 10Col 11Output Analysis :No of Repeating NumbersNo of offset numbersOutput Data
6Line No:
711510151082510635354550Lines: 1 - 39410153545100103106108240
8243610010110210310410510645108Lines: 3 - 7182678910111214151721334099100102103240
93992402408910100121031415Lines: 8 & 1753103108231392454
104683499721114010210517etc
1155936100676883333etc
12643103810111103154810678etc
13721114010211121314151617etc
14810080039210310210323139210639299
1591011344104676883333
16101021446105369121518105
17111031548106721114010210517
1812104165010716171819202122
1913105175210813105175210823
20141061854109190191190192141063
2115107195611011103154810671
22161082058111676883333
231710910860112231232234454466108454
2418110226211316171819202122
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
Sheet1



When I run the macro there is no output produced to the right hand side of the Table.
I immediately receive the run-time error 13 Type mismatch error and option to either
End or Debug. When I select the option to debug it navigates to the vba script and that is
the line causing the issue is highlighted in yellow.

I have attempted to send the whole of the error message but in this there is only one line
marked yellow...


Once again thanking you in your help.



Regards


the line that is marked yellow
 

Attachments

  • runtime_error_repeats.JPG
    runtime_error_repeats.JPG
    99.1 KB · Views: 2
  • runtime_error_repeats_1.JPG
    runtime_error_repeats_1.JPG
    151.9 KB · Views: 3
Upvote 0
Hi Peter,

Also I thought I would bring to your attention if you could look at the sum totals for columns P & Q :
1. Sum total of the Number of repeating numbers
2. Sum total of Numbers in a given line matching the Offset numbers

Thanks and Regards
 
Upvote 0
Also I thought I would bring to your attention if you could look at the sum totals for columns P & Q :
1. Sum total of the Number of repeating numbers
2. Sum total of Numbers in a given line matching the Offset numbers
:oops: Sorry, I had intended to return to those & forgot. I will have a look at that now.
 
Upvote 0
if you could look at the sum totals for columns P & Q :
1. Sum total of the Number of repeating numbers
2. Sum total of Numbers in a given line matching the Offset numbers
Try this version

VBA Code:
Sub Repeats_v2()
  Dim d1 As Object, d2 As Object
  Dim a As Variant, b As Variant, bits As Variant, rws As Variant, cols As Variant, ky As Variant
  Dim r As Range
  Dim i As Long, j As Long, k As Long
  
  Set d1 = CreateObject("Scripting.Dictionary")
  Set d2 = CreateObject("Scripting.Dictionary")
  For Each r In Range("E2:K2")
    d2(r.Value) = 1
  Next r
  a = Range("B7", Range("B7").End(xlToRight).End(xlDown)).Value
  cols = Application.Transpose(Application.Transpose(Evaluate("row(1:" & UBound(a, 2) & ")")))
  For Each r In Range("N7", Range("N" & Rows.Count).End(xlUp))
    If Len(r.Value) > 0 Then
      d1.RemoveAll
      ReDim b(1 To 1, 1 To UBound(a, 2))
      ReDim rws(0 To 0)
      k = 0
      bits = Split(r.Value, ",")
      For i = 0 To UBound(bits)
        For j = Split(bits(i) & "-" & bits(i), "-")(0) To Split(bits(i) & "-" & bits(i), "-")(1)
          ReDim Preserve rws(1 To UBound(rws) + 1)
          rws(UBound(rws)) = j
        Next j
      Next i
      b = Application.Index(a, rws, cols)
      For i = 1 To UBound(b)
        For j = 1 To UBound(b, 2)
          d1(b(i, j)) = d1(b(i, j)) + 1
        Next j
      Next i
      For Each ky In d1.keys
        If d1(ky) = 1 Then
          d1.Remove ky
        ElseIf d2.exists(ky) Then
          d1(ky & "|") = 1
          d1.Remove ky
          k = k + 1
        End If
      Next ky
      r.Offset(, 2).Value = d1.Count
      r.Offset(, 3).Value = k
      If d1.Count > 0 Then
        With r.Offset(, 4).Resize(, d1.Count)
          .Value = d1.keys
          If k > 0 Then
            .SpecialCells(xlConstants, xlTextValues).Interior.Color = vbYellow
            .Replace What:="|", Replacement:="", LookAt:=xlPart
          End If
          .Sort Key1:=.Cells(1), Order1:=xlAscending, Header:=xlNo, Orientation:=xlLeftToRight
        End With
      End If
    End If
  Next r
End Sub

cotech_10 2020-08-12 1.xlsm
BCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAH
1
255542850237116
3
4
5
6LinesRepeatsOffsets
799345041691842548154431-332235468
851236249688837302915533-71028121928364043717480
923583687481968746178178,18209095
1080219578797146121218
115715356224774754090742 , 5 - 8,12,151638111928374041434950515364717490
129637124340289880694210-1100
13732836439413643511198
144395544997903840285014
15529656916644697757050
161213141516171819202122
172324252627282930313233
181751411128648550198937
193377224524876138398659
20935233862626183517484
21287041817444841347131
228016599525941298636090
23572586844762538405327
24582188912659023958444
25
Sheet1
 
Upvote 0
Hi Peter,

Amazing effort, it works exactly as specified and I took into consideration the formatting in Column "N" ..
Thanking you in your generosity and assistance...

Regards
 
Upvote 0

Forum statistics

Threads
1,215,614
Messages
6,125,848
Members
449,266
Latest member
davinroach

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