Userform textbox -used next smallest unused number from column in textbox.

dellehurley

Board Regular
Joined
Sep 26, 2009
Messages
171
Office Version
  1. 365
Platform
  1. Windows
Hi
This is a little hard to explain but here goes.
I have a large database which contains details about associated files. The database generates the new file name and part of that file name is a number. Currently there are a lot of missing numbers and gaps. I want excel to use VBA to find the smallest number in the sequence that has not already been used and place that in a textbox on a userform.
I have found this Youtube video and it is sort of doing what I want too, except I need the answer to populate a text box on a userform and be dynamic. This is a link to it Mr Excel & excelisfun Trick 103: Find Missing Numbers In A Series

File NameFile No.File TypeEventExtRINFull NameDate of EventDescription
FBi0243.pdf0243FileBirthpdf42568Doe, Jane01/05/2019Birth Transcript
PMa0452.jpg0452PictureMarriagejpg4857Smith, John02/03/2018Wedding
PMa0452.jpg0452PictureMarriagejpg5638Jones, Ann02/03/2018Wedding

This is an example of the database. The File No. column is the number I want to generate. The same file name maybe duplicated several times if the file relates to several people eg PMa0452.jpg in table above. But the number should only be used once otherwise. The file number is always 4 digits.
Many Thanks for your help.
Dannielle
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What code have you written already?
 
Upvote 0
Mr Excel Link
This is a link to the file.
Sorry I don't have any code at the moment I use a dynamic combo box but it does not work very well. I have to update it manually all the time.
 
Upvote 0
If FileNo. is in column B, you could use code like


VBA Code:
Dim i as Long

With Range("B:B")
    For i = Application.Min(.Cells) To Application.Max(.Cells)
        If IsError(Application.Match(i, .Cells, 0) Then Exit For
    Next i

    TextBox1.Text = Format(i, "0000")
End With
 
Upvote 0
If FileNo. is in column B, you could use code like


VBA Code:
Dim i as Long

With Range("B:B")
    For i = Application.Min(.Cells) To Application.Max(.Cells)
        If IsError(Application.Match(i, .Cells, 0) Then Exit For
    Next i

    TextBox1.Text = Format(i, "0000")
End With
Thanks for the quick reply. I get a syntax error on the line
VBA Code:
If IsError(Application.Match(i, .Cells, 0) Then Exit For
I quite new to VBA so it is not immediately obvious to me what is wrong.
Dannielle
 
Upvote 0

Forum statistics

Threads
1,215,704
Messages
6,126,321
Members
449,308
Latest member
Ronaldj

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