Auto numbering problem

leeagall

New Member
Joined
Aug 11, 2010
Messages
30
Hi after days and days of research i have hit a stumbling block with my code and i hope you can spare a minute to help me.

i have a workbook with two worksheets, Documents and Workings. The user can fill in the userform i have created so that it fills in the worksheet in documents accordingly.

My code is as folows:

Code:
Private Sub cmdAdd_Click()
Dim iRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Documents")
 
'find first empty row in database
iRow = ws.Cells(Rows.Count, 1) _
.End(xlUp).Offset(1, 0).Row
 
'check for a Doc Number
If Trim(Me.TextBoxDocNo.Value) = "" Then
Me.TextBoxDocNo.SetFocus
MsgBox "Please Create a Document Number by the using the Create Button"
Exit Sub
 
End If 'check for a Date
If Trim(Me.txtCal.Value) = "" Then
Me.txtCal.SetFocus
MsgBox "Please enter a Date"
Exit Sub
 
End If
 
'check for a Description
 
If Trim(Me.txtDescription.Value) = "" Then
Me.txtDescription.SetFocus
MsgBox "Please enter a Description"
Exit Sub
End If
 
'check for a Location
 
If Trim(Me.txtLocation.Value) = "" Then
Me.txtLocation.SetFocus
MsgBox "Please enter a Location"
Exit Sub
End If
 
'copy the data to the database
ws.Cells(iRow, 1).Value = Me.txtCal.Value
ws.Cells(iRow, 2).Value = Me.TextBoxDocNo.Value
ws.Cells(iRow, 3).Value = Me.txtDescription.Value
ws.Cells(iRow, 4).Value = Me.txtLocation.Value
ws.Cells(iRow, 5).Value = Me.ClassDoc.Value
ws.Cells(iRow, 6).Value = Me.ScheduleDoc.Value
ws.Cells(iRow, 10).Value = Me.edited.Value
ws.Cells(iRow, 11).Value = Me.unedited.Value
ws.Cells(iRow, 14).Value = Me.txtNotes.Value
  
'ws.Cells(iRow, 7).Value = Me.ClassDoc.Value
'ws.Cells(iRow, 5).Value = Me.ClassDoc.Value
 
If edited.Value = True Then Range("H" & iRow) = "*EDITED*"
If unedited.Value = True Then Range("I" & iRow) = "*UNEDITED*"
If TCPS.Value = True Then Range("G" & iRow) = "Test"
If Attached.Value = True Then Range("L" & iRow) = "*"
 
'If EditedMG6C = True Then [G4] = "MG6E"
'If EditedMG6C = False Then [G4] = ""
 
Me.TextBoxDocNo.SetFocus
Me.txtCal.SetFocus
Me.txtLocation.SetFocus
Me.txtDescription.SetFocus
 
Unload Me
frmDataEntry.Show
 
End Sub
 
Private Sub cmdClose_Click()
Unload Me
End Sub
 
Private Sub CommandButton3_Click()
Dim iRow As Long
Dim ws As Worksheet
 
Dim x As Integer
Dim bIncrement As Boolean
Set ws = Worksheets("Workings")
 
'find last data row from database
iRow = ws.Cells(Rows.Count, 12) _
.End(xlUp).Row
 
x = 0
Sheets("Workings").Select
Do
If IsEmpty(Range("L1").Offset(x + 1, 0).Value) Then
Range("L1").Offset(x + 1, 0).Value = Range("L1").Offset(x, 0) + 1
bIncrement = True
End If
x = x + 1
 
Loop Until bIncrement = True
 
Me.TextBoxDocNo.Text = "D" & ws.Cells(iRow, 12).Value + 1
Sheets("Documents").Select
 
End Sub
 
Private Sub UserForm_QueryClose(Cancel As Integer, _
CloseMode As Integer)
If CloseMode = vbFormControlMenu Then
Cancel = True
MsgBox "Please use the Close Input Form Button!"
End If
End Sub
 
Private Sub cmd1_Click()
CalendarForm.Show
End Sub
 
Private Sub UserForm_Activate()
Dim iRow As Long
 
Dim ws As Worksheet
 
Dim x As Integer
Dim bIncrement As Boolean
Set ws = Worksheets("Workings")
 
'find last data row from database
 
iRow = ws.Cells(Rows.Count, 12) _
.End(xlUp).Row
 
x = 0
 
Sheets("Workings").Select
Do
If IsEmpty(Range("L1").Offset(x + 1, 0).Value) Then
Range("L1").Offset(x + 1, 0).Value = Range("L1").Offset(x, 0) + 1
bIncrement = True
End If
x = x + 1
 
Loop Until bIncrement = True
 
Me.TextBoxDocNo.Text = "D" & ws.Cells(iRow, 12).Value + 1
Sheets("Documents").Select
 
End Sub

What i am trying to achieve is auto numbering in column B in Documents with D1, D2, etc.

Each time the userform is opened or restarted or populated by a the command button it populates the D1, D2 etc textbox on the userform and then the form submits the alll the data to the next free row in the worksheet.

As you will see i have with my very limited knowledge created a work round utilising the workings worksheet to populate column L and then transfers this to the userform. This code relys on the cell above A2 to be a number.

Is it possible to dispense with my workround and get the userform to populate the textbox automatically as it reads from column B in the documents worksheet, collects the last D number used and allocates the next one, if the form is blank then it should allocate D1 in the first instance.

I do hope i have made some sense and that this last hurdle can be overcome, thank you in anticipation,

regards

Lee
 
Last edited by a moderator:

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi,

try this one!
Use the function like this:
Me.MyTextbox = NextNumber(Worksheets("Documents").Range("B:B"))

Copy into a modul:
<pre style='border:thin solid #9B9B9B; padding:12px 24px; margin-left:12px; color:#1B3636; overflow:auto; '><span style='color:#0000EE'>Option</span> <span style='color:#0000EE'>Explicit</span>

<span style='color:#0000EE'>Public</span> <span style='color:#0000EE'>Function</span> NextNumber(<span style='color:#0000EE'>ByVal</span> rCells <span style='color:#0000EE'>As</span> Range) <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>
<span style='color:#0000EE'>Dim</span> vArray <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Variant</span>
<span style='color:#0000EE'>Dim</span> lRow <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>
<span style='color:#0000EE'>Dim</span> lColumn <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>
<span style='color:#0000EE'>Dim</span> vEntry <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Variant</span>

vArray = rCells.Value

<span style='color:#0000EE'>For</span> lRow = <span style='color:#0000EE'>Lbound</span>(vArray, <span style='color:#DDAA00'>1</span>) <span style='color:#0000EE'>To</span> <span style='color:#0000EE'>Ubound</span>(vArray, <span style='color:#DDAA00'>1</span>)
<span style='color:#0000EE'>For</span> lColumn = <span style='color:#0000EE'>Lbound</span>(vArray, <span style='color:#DDAA00'>2</span>) <span style='color:#0000EE'>To</span> <span style='color:#0000EE'>Ubound</span>(vArray, <span style='color:#DDAA00'>2</span>)
vEntry = vArray(lRow, lColumn)
<span style='color:#0000EE'>If</span> vEntry <> <span style='color:#FF3E3E'>""</span> <span style='color:#0000EE'>Then</span>
vEntry = OnlyFigures(vEntry)
<span style='color:#0000EE'>If</span> NextNumber < vEntry <span style='color:#0000EE'>Then</span>
NextNumber = vEntry
<span style='color:#0000EE'>End</span> <span style='color:#0000EE'>If</span>
<span style='color:#0000EE'>End</span> <span style='color:#0000EE'>If</span>
<span style='color:#0000EE'>Next</span> lColumn
<span style='color:#0000EE'>Next</span> lRow

NextNumber = NextNumber + <span style='color:#DDAA00'>1</span>
<span style='color:#0000EE'>End</span> <span style='color:#0000EE'>Function</span>

<span style='color:#0000EE'>Private</span> <span style='color:#0000EE'>Function</span> OnlyFigures(<span style='color:#0000EE'>ByVal</span> Text <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span>) <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Long</span>
<span style='color:#0000EE'>Dim</span> objRegExp <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>Object</span>
<span style='color:#0000EE'>Dim</span> strResult <span style='color:#0000EE'>As</span> <span style='color:#0000EE'>String</span>
<span style='color:#0000EE'>Set</span> objRegExp = CreateObject(<span style='color:#FF3E3E'>"VBScript.Regexp"</span>)

objRegExp.Pattern = <span style='color:#FF3E3E'>"[^0-9]"</span>
objRegExp.Global = <span style='color:#0000EE'>True</span>

strResult = objRegExp.Replace(Text, <span style='color:#FF3E3E'>""</span>)
<span style='color:#0000EE'>If</span> strResult <> <span style='color:#FF3E3E'>""</span> <span style='color:#0000EE'>Then</span>
OnlyFigures = strResult
<span style='color:#0000EE'>End</span> <span style='color:#0000EE'>If</span>

<span style='color:#0000EE'>Set</span> objRegExp = <span style='color:#0000EE'>Nothing</span>
<span style='color:#0000EE'>End</span> <span style='color:#0000EE'>Function</span></pre>

Best wishes
Gerd
 
Upvote 0
Hi lee,

Im not sure. I think you read the next number here ....

Me.TextBoxDocNo.Text = "D" & ws.Cells(iRow, 12).Value + 1
Sheets("Documents").Select

So you can use it like this ...
Me.TextBoxDocNo.Text = "D" & NextNumber(Worksheets("Documents").Range("B:B"))

Best wishes
Gerd
 
Upvote 0
Gerd

thanks for that i have taken out all my code and used the line you suggested with the code inserted into a module and it works great thank you, however the numbering starts at D2 and not D1 as i need can you assist any further at all?

regards

lee
 
Upvote 0
Hi Lee,

do you have any entries in column B? Headlines or else with a digit in it?
So you have to change the codeline ... without headlines...
Me.TextBoxDocNo.Text = "D" & NextNumber(Worksheets("Documents").Range("B2:B65536"))

Best wishes
Gerd
 
Upvote 0
Gerd

thanks for that, in column B, B1 is empty as is B2 and B3 contains text, does this enlighten you ?

regards

lee
 
Upvote 0
Hi Lee,

If there is a digit in the cell value B2 or B3 we should modulate the range address.
Try ...
Me.TextBoxDocNo.Text = "D" & NextNumber(Worksheets("Documents").Range("B4:B65536"))

Best wishes
Gerd
 
Upvote 0
Gerd thank you for your patience, i did alter the code but it still starts numbering at "D2" in cell B4??? I need it to start at number "D1" please.

regards

lee
 
Last edited:
Upvote 0
Hi Lee,

The code looks in all cells you pretend. If there's anywhere a digit the code will find it and starts with counting. So if you've got a "2" there must be a "1" in your range area.
Otherwise it could be the function was not correct in use. In this case I need your code.

Best wishes
Gerd
 
Upvote 0

Forum statistics

Threads
1,216,579
Messages
6,131,531
Members
449,654
Latest member
andz

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