VBA find missing numbers and result of missing numbers place under their corresponding header

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. And I want the result of missing number must show under their corresponding header numbers, as shown below in the Example sheet1...also image is for more detail.

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 under their corresponding header numbers
2nd starting ranges 5 rows and 5 columns = C4:G8, Missing number in the Cell I8 to right under their corresponding header numbers
3rd starting ranges 5 rows and 5 columns = C5:G9, Missing number in the Cell I9 to right under their corresponding header numbers
4th starting ranges 5 rows and 5 columns = C6:G10, Missing number in the Cell I10 to right under their corresponding header numbers... and so on till end of the data

Example sheet1

*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: 7

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
See if this macro gives you the results you want...
VBA Code:
Sub Motilulla()
  Dim R As Long, LastRow As Long, Cell As Range
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  Application.ScreenUpdating = False
  Range("I7:AL" & LastRow) = [COLUMN(1:30)]
  For R = 7 To LastRow
    For Each Cell In Range(Cells(R - 4, "C"), Cells(R, "G"))
      Cells(R, "I").Resize(, 30).Replace Cell.Value, "", xlWhole, , , , False, False
    Next
  Next
  Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
See if this macro gives you the results you want...
VBA Code:
Sub Motilulla()
  Dim R As Long, LastRow As Long, Cell As Range
  LastRow = Cells(Rows.Count, "C").End(xlUp).Row
  Application.ScreenUpdating = False
  Range("I7:AL" & LastRow) = [COLUMN(1:30)]
  For R = 7 To LastRow
    For Each Cell In Range(Cells(R - 4, "C"), Cells(R, "G"))
      Cells(R, "I").Resize(, 30).Replace Cell.Value, "", xlWhole, , , , False, False
    Next
  Next
  Application.ScreenUpdating = True
End Sub
Rick Rothstein, it worked well as I wanted just a note: after I run the macro it gets stuck on the line below

VBA Code:
Cells(R, "I").Resize(, 30).Replace Cell.Value, "", xlWhole, , , , False, False

after trying and modifying again and again it worked when I finally removed 2 , , in between the xlWhole & False as shown below...May be it is all about due to my version problem

Code:
Cells(R, "I").Resize(, 30).Replace Cell.Value, "", xlWhole, , False, False

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

Good luck

Kind Regards,
Moti :)
 
Last edited:
Upvote 0
Workbook
Code:
VBA Code:
Sub FindMissingNums()
    Dim rw&, num&, rng As Range, cell As Range, is_found As Boolean
    For rw = 3 To Cells(Rows.Count, 3).End(xlUp).Row
        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
                Cells(rw, num + 8) = num
            End If
        Next
    Next
End Sub
 
Upvote 0
Workbook
Code:
VBA Code:
Sub FindMissingNums()
    Dim rw&, num&, rng As Range, cell As Range, is_found As Boolean
    For rw = 3 To Cells(Rows.Count, 3).End(xlUp).Row
        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
                Cells(rw, num + 8) = num
            End If
        Next
    Next
End Sub
Sektor, I like your macro it is written in a simple way so any changes can be done easier

Yes it is populating the results under their corresponding header, as I wanted ?

I appreciate your help and time you take to resolve this was a complicated query.

Good luck you have a nice day

Kind Regards,
Moti :)
 
Upvote 0
@Sektor In future when somebody has been told to start a new thread, please post your solution to the new thread, not the old one.
I have merged your response to the correct thread.
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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