VBA require to find missing numbers

motilulla

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

I need VBA for following task numbers are in column “A” if I select prior or code provide selection option after it is run for example I select the range A11:A23 Highlighted in yellow VBA find Min & Max range in selection and list missing number in column C as shown.

In given example in the selected range lowest number is 2 and highest number is 15 find missing between (2-15) in column C


Book1
ABC
1NumbersMissing Numbers
2165
378
41410
5412
61513
733
83
94
101
1114
126
132
1415
157
1615
176
189
192
2011
213
224
232
2424
257
269
275
2820
Sheet1


Thanks In Advance

Regards,
Moti
 
Try this:-
Select range in column "A" and run code !!!!
Code:
[COLOR=Navy]Sub[/COLOR] MG17Jan41
[COLOR=Navy]Dim[/COLOR] Rng [COLOR=Navy]As[/COLOR] Range, Dn [COLOR=Navy]As[/COLOR] Range, n [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Dic [COLOR=Navy]As[/COLOR] Object
[COLOR=Navy]Dim[/COLOR] oMax [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] oMin [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] c [COLOR=Navy]As[/COLOR] [COLOR=Navy]Long,[/COLOR] Tem [COLOR=Navy]As[/COLOR] [COLOR=Navy]String[/COLOR]
c = 1
[COLOR=Navy]Set[/COLOR] Rng = Selection
[COLOR=Navy]If[/COLOR] Not Selection.Column = 1 [COLOR=Navy]Then[/COLOR]
    MsgBox "Please [COLOR=Navy]Select[/COLOR] range in column ""A""" & Chr(10) & "and Re-run this code"
[COLOR=Navy]Exit[/COLOR] [COLOR=Navy]Sub[/COLOR]
[COLOR=Navy]End[/COLOR] If
[COLOR=Navy]Set[/COLOR] Dic = CreateObject("scripting.dictionary")
 Dic.CompareMode = vbTextCompare
    [COLOR=Navy]For[/COLOR] [COLOR=Navy]Each[/COLOR] Dn [COLOR=Navy]In[/COLOR] Selection: Dic(Dn.Value) = Empty: [COLOR=Navy]Next[/COLOR]
     oMax = Application.Max(Dic.keys)
      oMin = Application.Min(Dic.keys)
    
Tem = Range("C1").Value: Range("C:C").ClearContents: Range("C1").Value = Tem
    [COLOR=Navy]For[/COLOR] n = oMin To oMax
        [COLOR=Navy]If[/COLOR] Not Dic.exists(n) [COLOR=Navy]Then[/COLOR]
            c = c + 1
            Cells(c, "C") = n
          [COLOR=Navy]End[/COLOR] If
    [COLOR=Navy]Next[/COLOR] n
[COLOR=Navy]End[/COLOR] [COLOR=Navy]Sub[/COLOR]
Regards Mick
 
Last edited:
Upvote 0

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