VBA find missing numbers in the various ranges

motilulla

Well-known Member
Joined
Feb 13, 2008
Messages
2,422
Office Version
  1. 2010
Hello,

I need a macro help, which can find the missing numbers missing from 1 to 30 in the various ranges.

When I run the macro it must find missing numbers "from 1 to 30" within 5 rows and 5 columns range but these ranges are variable till end of the data.

For example...
1st starting ranges 5 rows and 5 columns = C3:G7, Missing number in the Cell I7 to right
2nd starting ranges 5 rows and 5 columns = C4:G8, Missing number in the Cell I8 to right
3rd starting ranges 5 rows and 5 columns = C5:G9, Missing number in the Cell I9 to right
4th starting ranges 5 rows and 5 columns = C6:G10, Missing number in the Cell I10 to right... and so on till end of the data

Example sheet1 also image attached

*ABCDEFGHIJKLMNOPQRSTUVWX
1
2Rn1Rn2Rn3Rn4Rn5
3
2​
7​
28​
22​
13​
4
24​
20​
11​
23​
12​
5
11​
18​
28​
1​
29​
6
9​
6​
9​
17​
8​
7
22​
11​
24​
7​
26​
3451014151619212527302628
8
20​
21​
9​
12​
20​
2351013151927
9
30​
16​
25​
14​
4​
2351013152327
10
4​
17​
19​
14​
6​
12310131523272829
11
22​
5​
16​
25​
18​
12
21​
23​
21​
25​
25​
13
2​
2​
14​
12​
20​
14
7​
22​
23​
29​
18​
15
17​
13​
4​
22​
21​
16
4​
9​
17​
22​
5​
17
2​
9​
13​
26​
18​
18
9​
17​
7​
3​
6​
19
20
21

Thank you all.

I am using Excel 2000

Regards,
Moti
 

Attachments

  • Missing Numbers From 1 to 30.png
    Missing Numbers From 1 to 30.png
    18.9 KB · Views: 14

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
How about
VBA Code:
Sub motilulla2()
   Dim i As Long, j As Long, k As Long
   Dim Fnd As Range
   
   k = 8
   For i = 3 To Range("C" & Rows.Count).End(xlUp).Row - 4
      For j = 1 To 30
         Set Fnd = Range("C" & i).Resize(5, 5).Find(j, , , xlWhole, , , , , False)
         If Fnd Is Nothing Then
            k = k + 1
            Cells(i + 4, k) = j
         End If
      Next j
      k = 8
   Next i
End Sub
 
Upvote 0
Solution
Workbook
Code:
VBA Code:
Sub FindMissingNums()
    Dim rw&, num&, col&, rng As Range, cell As Range, is_found As Boolean
    For rw = 3 To Cells(Rows.Count, 3).End(xlUp).Row
        col = 8
        Set rng = Cells(rw, 3).Resize(5, 5)
        For num = 1 To 30
            is_found = False
            For Each cell In rng
                If cell = num Then
                    is_found = True
                    Exit For
                End If
            Next
            If Not is_found Then
                col = col + 1
                Cells(rw, col) = num
            End If
        Next
    Next
End Sub
 
Upvote 0
How about
VBA Code:
Sub motilulla2()
   Dim i As Long, j As Long, k As Long
   Dim Fnd As Range
  
   k = 8
   For i = 3 To Range("C" & Rows.Count).End(xlUp).Row - 4
      For j = 1 To 30
         Set Fnd = Range("C" & i).Resize(5, 5).Find(j, , , xlWhole, , , , , False)
         If Fnd Is Nothing Then
            k = k + 1
            Cells(i + 4, k) = j
         End If
      Next j
      k = 8
   Next i
End Sub
Fluff, after running your macro, this time also I found my mistakes in an example attached. Your macro worked flawless and answered the correct missing numbers in each range as appeal.

I appreciate your help and time you take to resolve this difficult query.

Good luck

Kind Regards,
Moti :)
 
Upvote 0
Pretty the same way :​
VBA Code:
Sub Demo1()
        Dim V, R&, C%, N%
    With [C2].CurrentRegion.Rows
      ReDim V(1 To .Count - 5, 1 To 30)
        For R = 2 To .Count - 4
            C = 0
        For N = 1 To UBound(V, 2)
            If .Item(R).Resize(5).Find(N, , , 1) Is Nothing Then C = C + 1: V(R - 1, C) = N
        Next N, R
           .Cells(6, .Columns.Count + 2).Resize(R - 2, N - 1).Value2 = V
    End With
End Sub
 
Last edited:
Upvote 0
Workbook
Code:
VBA Code:
Sub FindMissingNums()
    Dim rw&, num&, col&, rng As Range, cell As Range, is_found As Boolean
    For rw = 3 To Cells(Rows.Count, 3).End(xlUp).Row
        col = 8
        Set rng = Cells(rw, 3).Resize(5, 5)
        For num = 1 To 30
            is_found = False
            For Each cell In rng
                If cell = num Then
                    is_found = True
                    Exit For
                End If
            Next
            If Not is_found Then
                col = col + 1
                Cells(rw, col) = num
            End If
        Next
    Next
End Sub
Sektor, thank you for giving another option your macro track even last row missing numbers.

I appreciate your help and time you take to give an additional option.

Good luck

Kind Regards,
Moti :)
 
Upvote 0
Pretty the same way :​
VBA Code:
Sub Demo1()
        Dim V, R&, C%, N%
    With [C2].CurrentRegion.Rows
      ReDim V(1 To .Count - 5, 1 To 30)
        For R = 2 To .Count - 4
            C = 0
        For N = 1 To UBound(V, 2)
            If .Item(R).Resize(5).Find(N, , , 1) Is Nothing Then C = C + 1: V(R - 1, C) = N
        Next N, R
           .Cells(6, .Columns.Count + 2).Resize(R - 2, N - 1).Value2 = V
    End With
End Sub
Marc L, thank you macro worked as treat.

I am pleased about your help and time you take to give an extra option.

Good luck

Kind Regards,
Moti :)
 
Upvote 0
Hello Fluff,

I need a bit change in the result layout post#2 macro is giving the result, as I wanted.

Please can you take a look if it is possible I get the result of missing number under their corresponding header number as shown below?

Example sheet1 and the image are attached also.

*ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAGAHAIAJAKALAMAN
1
2Rn1Rn2Rn3Rn4Rn5123456789101112131415161718192021222324252627282930
3
2​
7​
28​
22​
13​
4
24​
20​
11​
23​
12​
5
11​
18​
28​
1​
29​
6
9​
6​
9​
17​
8​
7
22​
11​
24​
7​
26​
345101415161921252730
8
20​
21​
9​
12​
20​
2345101314151619252730
9
30​
16​
25​
14​
4​
235101315192327
10
4​
17​
19​
14​
6​
12351013151823272829
11
22​
5​
16​
25​
18​
123810131523272829
12
21​
23​
21​
25​
25​
12378101113152426272829
13
2​
2​
14​
12​
20​
13789101113152426272829
14
7​
22​
23​
29​
18​
1389101113152426272830
15
17​
13​
4​
22​
21​
13689101115192426272830
16
4​
9​
17​
22​
5​
136810111516192426272830
17
2​
9​
13​
26​
18​
136810111516192425272830
18
9​
17​
7​
3​
6​
1810111214151619202425272830
19
20
21

Thank you all.

I am using Excel 2000

Regards,
Moti
 

Attachments

  • Missing Numbers Under Their Header.png
    Missing Numbers Under Their Header.png
    35.1 KB · Views: 3
Upvote 0

Forum statistics

Threads
1,224,591
Messages
6,179,768
Members
452,940
Latest member
rootytrip

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