generate the next sequential number

kelvin_9

Active Member
Joined
Mar 6, 2015
Messages
444
Office Version
  1. 2019
Hello All,
Previously, I use this code to start record my customer inquiry daily, and now I want to amend it and easier for analysis monthly records if my superior needs. However I get stuck on sequential number generation.
VBA Code:
Sub Macro1()

Range("A1000").End(xlUp).Offset(1, 0).Select
Selection.Value = Selection.Offset(-1, 0).Value + 1

End Sub
I created a calendar in VBA at first, select the inquiry date when someone inquired, and secondly, I wonder can it be generate the next new sequential number automatic like below table.

Book1
AC
1BASIC INFORMATION
2DATE (MM-DD-YYYY)CASE #
3Apr-05-20201
4Apr-05-20202
5Apr-06-20201
6Apr-06-20202
7Apr-06-20203
8Apr-06-20204
9Apr-06-20205
10Apr-06-20206
11Apr-07-20201
12Apr-07-20202
13Apr-07-20203
14Apr-07-20204
15Apr-07-20205
16Apr-07-20206
17Apr-07-20207
18Apr-07-20208
19Apr-08-20201
20Apr-08-20202
21Apr-08-20203
22Apr-08-20204
DATABASE


remarks:
case# in column C should be using 001, 002, 003, 004...... instead of single numbers 1, 2, 3, 4
(I curious why the outcome was single. I'm sorry)

Thanks All for the great guidance.
kelvin
 
The users needs to supply the date at first. Just like they received email/call-in from guest, they start to open this VBA form and record it...
They have to select current date(e.g. APR 17), and case# text box will be provide a new case# for this case, so it can be easier for admin/guest if they need to follow up, just quoting this reference#.

1.jpg


VBA Code:
Private Sub CMD_CREATE_Click()
    Dim nextrow As Range
    'error handler
    'On Error GoTo errHandler:
    'set the next row in the database
    Set nextrow = Sheet2.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0)

    'number of controls to loop through
    cNum = 22
    'add the data to the database
    For X = 1 To cNum
        nextrow = Me.Controls("Reg" & X).Value
        Set nextrow = nextrow.Offset(0, 1)
    Next
    'clear the controls
    For X = 1 To cNum
        Me.Controls("Reg" & X).Value = ""
    Next
    'sort the database
    'Sortit
    'error block
    On Error GoTo 0
    Exit Sub
End Sub

Here is my form template and code on "CREATE" button
Step 1: select current date
Step 2: source from a pull down menu(e.g. email, call-in...etc)
Step 3: the next sequential number populate
ps. please ignore the fourth text box "ws reference"

In contrast, I'm also planning to add "search" function in my template, so that when guest second call-in want a follow up, admin can easily locate it by that reference#(i.e. 2004-001, 2004-002... etc)
 

Attachments

  • 1.jpg
    1.jpg
    142.1 KB · Views: 3
Upvote 0

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
The users needs to supply the date at first. Just like they received email/call-in from guest, they start to open this VBA form and record it...
They have to select current date(e.g. APR 17)
Would they ever be entering a date that is NOT the current date?
Would they ever be entering a date that is earlier than the latest date already in the table (for example, the latest currently in the table is APR 19 and they are selecing APR 17)?

What I am trying to figure out here is whether the data needs to be re-sorted when they make an entry, or will it always be in the correct order, so there is no need to sort?
 
Upvote 0
Both not.
Admin can just select the current date while they are recording a on-time cases so they go for current. Secondly, this form is planning to use on coming Jun 1 2020, if they need to locate any prior of this date
Would they ever be entering a date that is NOT the current date?
Would they ever be entering a date that is earlier than the latest date already in the table (for example, the latest currently in the table is APR 19 and they are selecing APR 17)?

What I am trying to figure out here is whether the data needs to be re-sorted when they make an entry, or will it always be in the correct order, so there is no need to sort?

, they simply open old workbook and ctrl F.

Just in case, another code for button/textboxes
VBA Code:
Private Sub CMD_VIEW_Click()
    Sheet2.Select
End Sub
Private Sub CMD_EDIT_Click()

End Sub
Private Sub CMD_RESET_Click()
'clear the Reg controls
cNum = 22
    For X = 1 To cNum
        Me.Controls("Reg" & X).Value = ""
    Next
    'enable adding new staff
    Me.CMDCREATE.Enabled = True
    'enable adding new payroll number
    'Me.reg4.Enabled = True
    'clear the listbox
    'lstlookup.Clear
    'clear the textbox
    'Me.txtlookup.Value = ""
End Sub

Private Sub CMD_DELETE_Click()

End Sub
Private Sub CMD_CLOSE_Click()
    Unload Me
End Sub

Private Sub Frame2_Click()

End Sub

Private Sub Image1_Click()
Call CALENDAR.SelectedDate(Me.REG1)
End Sub

Private Sub Image2_Click()
Call CALENDAR.SelectedDate(Me.REG10)
End Sub

Private Sub Image3_Click()
Call CALENDAR.SelectedDate(Me.REG12)
End Sub

Private Sub REG1_Change()
If IsDate(Me.REG1.Text) Then
Me.REG1.Text = Format(Me.REG1.Text, "MMM-DD-YYYY")
End If
End Sub

Private Sub REG10_Change()
If IsDate(Me.REG10.Text) Then
Me.REG10.Text = Format(Me.REG10.Text, "MMM-DD-YYYY")
End If
End Sub

Private Sub REG12_Change()
If IsDate(Me.REG12.Text) Then
Me.REG12.Text = Format(Me.REG12.Text, "MMM-DD-YYYY")
End If
End Sub

Private Sub REG18_Change()

  Dim f As Range
  
  REG19 = ""
  REG20 = ""
  If REG18.Value = "" Then Exit Sub
  
  Set f = Sheet4.Range("A:A").Find(REG18, , xlValues, xlWhole)
  If Not f Is Nothing Then
    REG19 = f.Offset(, 1)
    REG20 = f.Offset(, 2)
  End If
  
Me.REG20.Text = Format(Me.REG20.Text, "#HK$##,##.00")

End Sub
 
Upvote 0
OK, here is basically what you want to do.

You will want to have code that finds the last record in your database (last cell in column A) with data. You can find that row number like this:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

You will then want to grab the values in column A and column C, like this:
LastDate = Cells(LastRow,"A")
LastSeq = Cells(LastRow, "C").Value


Then, you will want to check to see if the "LastDate" is today, i.e.
If LastDate = Date() Then
and if it is, add one to LastSeq and populate the sequence number on your form with this,
otherwise, populate it with 1.

You will probably want to put all this code either in:
- the VBA code that is opening Form
- VBA code that automatically gets run once they populate the date on the Form

That should give you a good roadmap of what you need to do.
 
Upvote 0

Forum statistics

Threads
1,215,048
Messages
6,122,862
Members
449,097
Latest member
dbomb1414

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